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

时间:2019-02-03 19:38:30

标签: amazon-web-services aws-glue

我正在尝试在csv中的一个字段上创建一个分区,并使用Glue ETL(python)将其存储为拼花地板。问题是,该字段是一个时间戳,因此在创建分区之前,我想从该时间戳中提取日期并将其存储在字段中,然后使用此新字段创建分区。

这是我下面的代码。我想将日期从verificationdatetime字段提取到名为verificationdate的字段中,然后在写入DynamicFrame时将其添加为partitionkey。有什么想法吗?

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()

1 个答案:

答案 0 :(得分:0)

  

df = dropnullfields3.toDF()

     

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

使用verificationdatetime

中指定的日期格式