【问题标题】:Convert Epoch to Datetime in AWS Glue / pyspark在 AWS Glue / pyspark 中将纪元转换为日期时间
【发布时间】:2024-04-29 10:55:02
【问题描述】:

我有一个使用 pyspark 在 AWS Glue 中处理的数据框,我的 df 中的一条消息如下所示:

{  
   "version":"0.0.1",
   "device_id":"df4c13ddddb1bb8fea9fe762",
   "session_id":"721cf47cf8408b30bc57743717",
   "session_start":1508450739332,
   "source_id":"5",
   "body":{  
      "message_id":"2e6d83c677a7b4f3683366d",
      "message_type":1,
      "message_time":1508450739332,
      "url":"http://sample_url/"
   },
   "partition_0":"2017",
   "partition_1":"10",
   "partition_2":"19",
   "partition_3":"22"
}

我正在尝试将 body.message_time(以毫秒为单位的纪元)转换为日期时间字段。我试过 from_unixtime 函数:

unnested_df.withColumn("messages_datetime", from_unixtime(unnested_df.body.message_time.divide(1000)).show()

给我这个错误:

TypeError: 'Column' object is not callable

我只是将该列传递给 from_unixtime 函数。知道如何解决这个问题吗?

谢谢

【问题讨论】:

    标签: python apache-spark pyspark spark-dataframe aws-glue


    【解决方案1】:

    你得到一个异常,因为Column 没有divide 方法。使用/ 运算符:

    unnested_df.withColumn("messages_datetime", from_unixtime(unnested_df.body.message_time / 1000)
    

    【讨论】: