apache-spark - PySpark to_utc_timestamp 返回相同时间

标签 apache-spark pyspark hive timestamp timezone

我有一个表,其中有字符串类型的日期时间。我想将其转换为 UTC 时间戳。我本地的时区是 CDT。我首先将日期时间转换为时间戳。

table = table.withColumn('datetime_dt', unix_timestamp(col('datetime'), "yyyy-MM-dd HH:mm:ss").cast("timestamp"))

然后,我尝试将此时间戳列转换为 UTC 时间。

table = table.withColumn('datetime_UTC',  to_utc_timestamp(table.datetime_dt, 'CDT'))

我也试试这个

table = table.withColumn('datetime_UTC',  to_utc_timestamp(col('datetime_dt'), 'CDT'))

但它返回相同的结果。以下是一些示例。

------------------------------------------------------------------
|   datetime         |     datetime_dt     |    datetime_UTC     |
------------------------------------------------------------------
|2019-01-01 00:49:00 | 2019-01-01 00:49:00 | 2019-01-01 00:49:00 |
------------------------------------------------------------------
|2019-01-01 02:06:00 | 2019-01-01 02:06:00 | 2019-01-01 02:06:00 |
------------------------------------------------------------------
|2019-01-02 05:15:00 | 2019-01-02 05:15:00 | 2019-01-02 05:15:00 |
------------------------------------------------------------------

为什么它给出的时间相同而没有任何转换?我从 pyspark.sql.functions 导入 to_utc_timestamp

最佳答案

将时区指定为 CST(或)America/Chicago 而不是 CDT ,我们不必明确提及夏令时。

  • Spark 在内部根据日期计算并添加 +5:00/+6:00

示例:

df.show()
#+-------------------+
#|                 dt|
#+-------------------+
#|2019-01-01 00:49:00|
#|2019-11-01 00:49:00|
#+-------------------+

df.withColumn('datetime_UTC',  to_utc_timestamp(col('dt'), 'CST')).show(10,False)

#or

df.withColumn('datetime_UTC',  to_utc_timestamp(col('dt'), 'America/Chicago')).show(10,False)

#+-------------------+---------------------+
#|dt                 |datetime_UTC         |
#+-------------------+---------------------+
#|2019-01-01 00:49:00|2019-01-01 06:49:00.0|
#|2019-11-01 00:49:00|2019-11-01 05:49:00.0|
#+-------------------+---------------------+

关于apache-spark - PySpark to_utc_timestamp 返回相同时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60725934/

相关文章:

java - 线程中的流式 Spark 异常 "main"

python - Window.rowsBetween - 只考虑满足特定条件的行(例如不为空)

python - Pyspark 数据框将多列转换为 float

python - 如何在jupyter PySpark session 中更改SparkContext属性spark.sql.pivotMaxValues

hive - Presto 集群无法对配置单元定义的表运行查询 - "No nodes available to run query"

apache-spark - 如何在 pyspark 流应用程序中使用具有不同主题的两个不同流将数据从 Kafka 存储到 Redis?

arrays - Spark 错误 :expected zero arguments for construction of ClassDict (for numpy. core.multiarray._reconstruct)

python - PySpark 中等效的 Scala 案例类是什么?

hadoop - 当使用beeline连接到启用了Kerberos的EMR群集上的Hive时,为什么要使用Hive服务主体?

mysql - 如何将 RDBMS DDL 转换为 Hive DDL 脚本