user3606138
user3606138

Reputation: 75

Converting timestamp field to date type and using it as partition in AWS Glue ETL

I'm trying to create a partition on one of the fields in csv and store it as parquet using Glue ETL (python). Problem is, this field is a timestamp so before creating a partition, I want to extract date from this timestamp and store it in a field and then use this new field to create a partition.

Here is my code below. I want to extract date from verificationdatetime field into a field called verificationdate and then add it as partitionkey while writing to the DynamicFrame. Any ideas?

args = getResolvedOptions(sys.argv, ['JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)

datasource0 = glueContext.create_dynamic_frame.from_catalog(database 
= "sample_database", table_name = "sample_table", transformation_ctx 
= "datasource0")

applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = 
[("personid", "string", "personid", "string"), 
("verificationdatetime", "string", "verificationdatetime"), ], transformation_ctx = "applymapping1")

resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = 
"make_struct", transformation_ctx = "resolvechoice2")

dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, 
transformation_ctx = "dropnullfields3")

datasink4 = glueContext.write_dynamic_frame.from_options(frame = 
dropnullfields3, connection_type = "s3", connection_options = 
{"path": "s3://samplebucket/samplefile/","partitionKeys":["type"]}, 
format = "parquet", transformation_ctx = "datasink4")

job.commit()

Upvotes: 4

Views: 4693

Answers (1)

Sandeep Fatangare
Sandeep Fatangare

Reputation: 2144

df = dropnullfields3.toDF()

df = df.withColumn('verificationdate', to_date('verificationdatetime', 'MM/dd/yyyy HH:mm:ss.SSSSSS'))

Use date-format as specified in verificationdatetime

Upvotes: 2

Related Questions