在Databricks中,to_timestamp()函数无法在PySpark中将字符串转换为时间戳。解决方案是使用to_utc_timestamp()函数并将时区设置为GMT。
示例代码:
from pyspark.sql.functions import to_utc_timestamp
from pyspark.sql.functions import from_unixtime
df = spark.createDataFrame([('2020-01-01T12:00:00.000Z',)], ['timestamp_str'])
df1 = df.withColumn('timestamp_utc', to_utc_timestamp(df.timestamp_str, 'GMT'))
df2 = df1.withColumn('timestamp', from_unixtime(df1.timestamp_utc.cast('long')))
df2.show()
上一篇:ApacheSpark中的requiredChildDistribution和outputPartitioning有什么区别?
下一篇:ApacheSpark中关于SparkAccumulatorV2的问题。我是一个Spark的新手,不知道为什么会出现问题。