Har*_*rsh 5 datetime-format apache-spark apache-spark-sql pyspark
如何在spark中将字符串存储2018-03-21 08:15:00 +03:00为a timestamptype,保留UTC偏移量?
下面尝试过
from pyspark.sql.functions import *
df = spark.createDataFrame([("2018-03-21 08:15:00 +03:00",)], ["timestamp"])
newDf= df.withColumn("newtimestamp", to_timestamp(col('timestamp'), "yyyy-MM-dd HH:mm:ss XXX")
)
Run Code Online (Sandbox Code Playgroud)
此打印newtimestamp列的值转换为 UTC 时间,即2018-03-21 05:15:00
我如何将此字符串存储为数据帧中的时间戳列,保留偏移量,即将相同的字符串存储为时间戳或类似存储2018-03-21 08:15:00 +3000
您需要使用以下命令将从转换中获得的时间戳格式化为所需的模式date_format:
newDf = df.withColumn(
"newtimestamp",
to_timestamp(col('timestamp'), "yyyy-MM-dd HH:mm:ss XXX")
).withColumn(
"newtimestamp_formatted",
date_format("newtimestamp", "yyyy-MM-dd HH:mm:ss Z")
)
newDf.show(truncate=False)
#+--------------------------+-------------------+-------------------------+
#|timestamp |newtimestamp |newtimestamp_formatted |
#+--------------------------+-------------------+-------------------------+
#|2018-03-21 08:15:00 +03:00|2018-03-21 06:15:00|2018-03-21 06:15:00 +0100|
#+--------------------------+-------------------+-------------------------+
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4701 次 |
| 最近记录: |