如何将parquet文件的int64数据类型列转换为sparksqlDataframe中的时间戳?

ulmd4ohb  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(498)

这里我的数据框如下所示:

  1. +----------------+-------------+
  2. | Business_Date| Code|
  3. +----------------+-------------+
  4. |1539129600000000| BSD|
  5. |1539129600000000| BTN|
  6. |1539129600000000| BVI|
  7. |1539129600000000| BWP|
  8. |1539129600000000| BYB|
  9. +----------------+-------------+

我想把 Business_Date 列自 biginttimestamp 将数据加载到配置单元表时的值。
我该怎么做?

5w9g7ksd

5w9g7ksd1#

你可以用 pyspark.sql.functions.from_unixtime() 哪个会
将unix epoch(1970-01-01 00:00:00 utc)中的秒数转换为表示当前系统时区中该时刻的时间戳的字符串,格式为给定格式。
看来你的 Business_Date 需要除以1米才能转换为秒。
例如:

  1. from pyspark.sql.functions import from_unixtime, col
  2. df = df.withColumn(
  3. "Business_Date",
  4. from_unixtime(col("Business_Date")/1000000).cast("timestamp")
  5. )
  6. df.show()
  7. # +---------------------+----+
  8. # |Business_Date |Code|
  9. # +---------------------+----+
  10. # |2018-10-09 20:00:00.0|BSD |
  11. # |2018-10-09 20:00:00.0|BTN |
  12. # |2018-10-09 20:00:00.0|BVI |
  13. # |2018-10-09 20:00:00.0|BWP |
  14. # |2018-10-09 20:00:00.0|BYB |
  15. # +---------------------+----+
  16. ``` `from_unixtime` 返回一个字符串,以便将结果强制转换为 `timestamp` .
  17. 现在是新模式:

df.printSchema()

root

|-- Business_Date: timestamp (nullable = true)

|-- Code: string (nullable = true)

展开查看全部

相关问题