假设您在胶水中使用 Python,并假设 Python 将您的字段理解为日期,您可以执行以下操作:
from pyspark.sql.functions import date_format
from awsglue.dynamicframe import DynamicFrame
from awsglue.context import GlueContext
def out_date_format(to_format):
"""formats the passed date into MM/dd/yyyy format"""
return date_format(to_format,"MM/dd/yyyy")
#if you have a dynamic frame you will need to convert it to a dataframe first:
#dataframe = dynamic_frame.toDF()
dataframe.withColumn("new_column_name", out_date_format("your_old_date_column_name"))
#assuming you are outputting via glue, you will need to convert the dataframe back into a dynamic frame:
#glue_context = GlueContext(spark_context)
#final = DynamicFrame.fromDF(dataframe, glue_context,"final")
根据您获取数据的方式,可能还有其他选项可以使用映射或格式化。如果 python 不将您的字段理解为日期对象,则需要先对其进行解析,例如:
import dateutil.parser
#and the convert would change to:
def out_date_format(to_format):
"""formats the passed date into MM/dd/yyyy format"""
yourdate = dateutil.parser.parse(to_format)
return date_format(yourdate,"MM/dd/yyyy")
请注意,如果 dateutil 没有内置在胶水中,则需要使用如下语法将其添加到作业参数中:“--additional-python-modules”=“python-dateutil==2.8.1”