在读取MongoDB数据时需要将时间戳字段进行转换,将其转换为UTC格式的时间戳。可以使用以下代码示例解决这个
from pyspark.sql.functions import from_utc_timestamp, to_utc_timestamp
# 读取MongoDB数据
df = spark.read.format("mongo").load()
# 将时间戳字段转换为UTC时间戳
df = df.withColumn("timestamp", from_utc_timestamp("timestamp", "UTC"))
在读取完数据后,将时间戳字段使用from_utc_timestamp
函数进行转换即可。如果需要将本地时间戳转换为UTC时间戳,也可以使用to_utc_timestamp
函数进行转换。