Forum Discussion
Pyspark Create a ProcessedFiles. Update the flag but I then can't resave OR then view the original P
Hi DebbieE
I was interested in your question and reproduced your error. I am not entirely sure but I assume that the behaviour has something to do with the way spark processes data and keeps track of the schema of each dataframe and where it comes from. In your case I would assume that there is some kind of circular dependency of the schema you are overwritting to the original schema of the file and that is why spark runs into that error - but as I said, pretty unsure about that and actually just an assumption.
Nevertheless I was able to at least come up with a solution/ workaround:
For the exact reason of different versions (in terms of schema-versions) of the parquet-file it seems obvious to use delta instead of parquet. So spark can keep track of the originally read schema while creating a newer version of the file since the "older" version is still kept in the lakehouse.
dflog_delta = spark.read.format('delta').load("Tables/" + delta_table_name)
from pyspark.sql.functions import lit
df_Processed_delta = dflog_delta.select("filename","processedTime").withColumn("fullyProcessedFlag",lit(1))
df_Processed_delta.write.mode("overwrite").option("overwriteSchema", "true")\
.format('delta').saveAsTable(delta_table_name)
print("Delta file overwritten successfully.")
Hope this solution works for you. 🙂
BR
Martin
- DebbieE2 years ago
Community Champion
I dont want to do that. I want to have everything in the one file. Because otherwise it will get really confusing.
I still want to see the old data in the latest file with 0 as processed in the latest.
- frithjof_v2 years ago
Community Champion
I think you could still use a delta table.
If you don't want to overwrite the information in the table, perhaps you could update or upsert instead?
https://docs.delta.io/latest/delta-update.html#update-a-table
I'm not sure I fully understand the functionality you are wanting to achieve. Could you show some pictures of what you're wanting to achieve?
Please remember don't show any sensitive or internal information.
- frithjof_v2 years ago
Community Champion
Here is a Reddit thread which discusses a similar topic: