简体   繁体   English

将时间戳字段转换为日期类型并将其用作 AWS Glue ETL 中的分区

[英]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).我正在尝试在 csv 中的一个字段上创建一个分区,并使用 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.我想将verificationdatetime日期时间字段中的日期提取到名为verificationdate日期的字段中,然后在写入 DynamicFrame 时将其添加为partitionkey键。 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()

df = dropnullfields3.toDF() df = dropnullfields3.toDF()

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

Use date-format as specified in verificationdatetime使用verificationdatetime日期时间中指定的日期格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM