json - 从 AWS Glue 表到 RedShift Spectrum 外部表的日期字段转换

标签 json aws-glue isodate amazon-redshift-spectrum

我正在尝试将 JSON 数据集从 S3 到 Glue 表模式转换为 Redshift 频谱以进行数据分析。创建外部表时,如何转换 DATE 字段?

需要突出显示源数据来自 MongoDB 的 ISODate 格式。这里是 Glue 表格式。

  struct $date:string

在外部表中尝试了以下格式
startDate:struct<$date:varchar(40)>
startDate:struct<date:varchar(40)>
startDate:struct<date:timestamp>

在 Redshift Spectrum 或 Glue 中是否有解决方法来处理 ISODate 格式?还是建议回源转换ISOdate格式?

最佳答案

假设您在胶水中使用 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"

关于json - 从 AWS Glue 表到 RedShift Spectrum 外部表的日期字段转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55250618/

相关文章:

amazon-web-services - 使用 AWS Glue Crawler 指定 SerDe 序列化库

date - golang 中的 ISOWeek() 没有返回一年中一周的期望值

date - 如何在Hive中将ISO日期转换为UTC日期

amazon-web-services - AWS Glue ETL 作业和 AWS EMR 有什么区别?

java - 解析简单的 Json 响应

javascript - json 选择一个名为 "item"的项目

javascript - 使用 Mongoose 的 .populate() 返回某些字段

python - 对 AWS Athena 查询结果进行分页时如何跳过 header

mongodb - 比较mongodb中的日期

ios - 从 Openweathermap IOS 4 Xcode 10 获取 json 数据后 UI 不会更新