I have multiple JSON files (10 TB ~) on a S3 bucket, and I need to organize these files by a date element present in every json document.
What I think that my code needs to do
- Read all json files in the s3 bucket.
- Keep all documents which have the element "creation_date" between 2022-01-01 and 2022-04-01
- Save them in another bucket in a parquet format.
I'm not sure that's the right thing to do, considering the size that I'm dealing it.
Here's an example of a json document. Each file has multiple of these documents.
{
"id": 123456,
"creation_date": "2022-01-01T23:35:16",
"params": {
"doc_info": "AXBD",
"return_date": "20/05/2021",
"user_name": "XXXXXXXX",
"value": "40,00"
},
"user_id": "1234567",
"type": "TEST"
}
]
Here's what I already tried on a DB notebook, but in fact, I can't use the code directly on a notebook. I necessarily need to write a spark code and run on an airflow dag, because I don't have write access on the bucket using directly from the notebook.
# Trying to read all the json files
df_test = spark.read.json("s3://my-bucket/**/**" + "/*.json")
# Filtering all documents that has the creation_date period that I want
df_test_filter = df_test.filter(F.col("creation_date").between('2022-01-01','2022-04-01'))
# Write parquet on another bucket
# In this test, I'm saving on a local bucket that I have write access.
df_test_filter.write.mode('overwrite').parquet("s3://my-local-test-bucket/")
That seems to work fine on a single json file that I use to test, but my questions are:
- How can I do this without a databricks notebook, and using an airflow dag with pyspark?
- Thinking in performance issues, there is a better way to do this?