在 Pyspark 中将纪元时间转换为时间戳
Converting Epoch Time to Timestamp in Pyspark
我有一个 df,其中有一列有纪元时间。纪元时间列的变量类型为字符串。我希望它转换成时间戳。我正在使用以下命令
from pyspark.sql.functions import from_utc_timestamp
df = df.withColumn('start_time',from_unixtime(df.recognition_start_time,'UTC'))
df.select('recognition_start_time').show(10,False)
但命令结果为 null。我犯了什么错误?
Epoch Time : 1583471040000
Output expected :2020-03-06 05:04:00
在 from_unixtime
中,我们需要指定预期的输出格式 (yyyy-MM-dd hh:mm:ss
) 并使用 from_utc_timestamp
我们可以将输出从 utc 转换为给定时区。
Example:
spark.sql("""select from_utc_timestamp(from_unixtime("1583471040000"/1000,"yyyy-MM-dd hh:mm:ss"),"America/Chicago")""").show(false)
+---------------------+
|_c0 |
+---------------------+
|2020-03-05 05:04:00.0|
+---------------------+
我有一个 df,其中有一列有纪元时间。纪元时间列的变量类型为字符串。我希望它转换成时间戳。我正在使用以下命令
from pyspark.sql.functions import from_utc_timestamp
df = df.withColumn('start_time',from_unixtime(df.recognition_start_time,'UTC'))
df.select('recognition_start_time').show(10,False)
但命令结果为 null。我犯了什么错误?
Epoch Time : 1583471040000
Output expected :2020-03-06 05:04:00
在 from_unixtime
中,我们需要指定预期的输出格式 (yyyy-MM-dd hh:mm:ss
) 并使用 from_utc_timestamp
我们可以将输出从 utc 转换为给定时区。
Example:
spark.sql("""select from_utc_timestamp(from_unixtime("1583471040000"/1000,"yyyy-MM-dd hh:mm:ss"),"America/Chicago")""").show(false)
+---------------------+
|_c0 |
+---------------------+
|2020-03-05 05:04:00.0|
+---------------------+