【发布时间】:2018-06-16 19:54:01
【问题描述】:
我在 Spark 中有一个数据框,其中包含 Unix(Epoch) 时间和时区名称。我希望根据不同的tz名称将epochtime转换为本地时间。 这是我的数据的样子:
data = [
(1420088400, 'America/New_York'),
(1420088400, 'America/Los_Angeles'),
(1510401180, 'America/New_York'),
(1510401180, 'America/Los_Angeles')]
df = spark.createDataFrame(data, ["epoch_time", "tz_name"])
df.createOrReplaceTempView("df")
df1 = spark.sql("""select *, from_unixtime(epoch_time) as gmt_time,"
from_utc_timestamp(from_unixtime(epoch_time), tz_name) as local_time"
from df""")
df1.show(truncate= False)
结果如下:
+----------+-------------------+-------------------+---------------------+
|epoch_time|tz_name |gmt_time |local_time |
+----------+-------------------+-------------------+---------------------+
|1420088400|America/New_York |2015-01-01 05:00:00|2015-01-01 00:00:00.0|
|1420088400|America/Los_Angeles|2015-01-01 05:00:00|2014-12-31 21:00:00.0|
|1510401180|America/New_York |2017-11-11 11:53:00|2017-11-11 06:53:00.0|
|1510401180|America/Los_Angeles|2017-11-11 11:53:00|2017-11-11 03:53:00.0|
+----------+-------------------+-------------------+---------------------+
- 我不太确定这种转移是否正确,但似乎夏令时已经解决了。
-
我是否应该先使用from_unixtime将epochtime更改为时间字符串,然后使用to_utc_timestamp将其更改为utc时间戳,最后使用tz_name将此UTC时间戳更改为本地时间?试过了,但是出错了
df2 = spark.sql("""select *, from_unixtime(epoch_time) as gmt_time, from_utc_timestamp(from_unixtime(epoch_time), tz_name) as local_time, from_utc_timestamp(to_utc_timestamp(from_unixtime(epoch_time),from_unixtime(unix_timestamp(), 'z')), tz_name) as newtime from df""") 如何查看我的 EMR 服务器时区?
-
试过了,这是服务器时区吗?
spark.sql("select from_unixtime(unix_timestamp(), 'z')").show()这给了我:
+--------------------------------------------------------------------------+ |from_unixtime(unix_timestamp(current_timestamp(), yyyy-MM-dd HH:mm:ss), z)| +--------------------------------------------------------------------------+ | UTC| +--------------------------------------------------------------------------+
感谢您的澄清。
【问题讨论】:
-
错误是什么?对我来说很好。
-
您使用的是什么版本的 Spark?
-
Spark 2.0 。我在 EMR 控制台console.aws.amazon.com/console/home?region=us-east-1 上检查了它,其中区域是 us-east-1。如何检查服务器时区名称?
-
你知道 spark.sql("select from_unixtime(unix_timestamp(), 'z')") 'z' 代表for吗?
标签: apache-spark timezone pyspark pyspark-sql epoch