将具有utc偏移量的字符串转换为spark时间戳

wgmfuz8q  于 2021-07-13  发布在  Spark
关注(0)|答案(1)|浏览(317)

如何存储字符串 2018-03-21 08:15:00 +03:00 作为一个 timestamptype ,保留utc偏移量,在spark中?
在下面试用

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")
)

这个指纹 newtimestamp 值转换为utc时间的列,即 2018-03-21 05:15:00 如何将这个字符串存储为dataframe中的timestamp列,即将相同的字符串存储为timestamp或类似的存储 2018-03-21 08:15:00 +3000

ovfsdjhp

ovfsdjhp1#

您需要使用将从转换中获得的时间戳格式化为所需的模式 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|

# +--------------------------+-------------------+-------------------------+

相关问题