繁体   English   中英

如何使用 spark 读取时间戳、保存到 hive 并正确显示它们?

[英]How can I read in timestamps using spark, save to hive, and have them display correctly?

我有一堆时间戳列作为字符串的记录,如下所示:

+---------------------+
|         raw         |
+---------------------+
| 2019-07-14 00:00:00 |
| 2019-07-04 00:00:00 |
| 2019-01-26 00:00:00 |
+---------------------+

如果我使用.withColumn("timestamp", to_timestamp(Col("raw"))) ,并使用STRING, TIMESTAMP架构写入配置单元,那么我们会看到:

+---------------------+---------------------+
|         raw         |      timestamp      |
+---------------------+---------------------+
| 2019-07-14 00:00:00 | 2019-07-14 00:00:00 |
| 2019-07-04 00:00:00 | 2019-07-04 00:00:00 |
| 2019-01-26 00:00:00 | 2019-01-26 00:00:00 |
+---------------------+---------------------+

但是我们想要指定源时区的选项,所以如果我们用 to_utc_timestamp 替换 to_timestamp 就像.withColumn("timestamp", to_utc_timestamp(Col("raw"), "America/New_York")) ,那么我们会得到一个这样的表时差 5 小时:

+---------------------+---------------------+
|         raw         |      timestamp      |
+---------------------+---------------------+
| 2019-07-14 00:00:00 | 2019-07-14 05:00:00 |
| 2019-07-04 00:00:00 | 2019-07-04 05:00:00 |
| 2019-01-26 00:00:00 | 2019-01-26 05:00:00 |
+---------------------+---------------------+

但是有数据的服务器和hive服务器在东部时区,所以我们不应该看到有0小时差异的时间戳吗?
我的问题是,如何将时间戳从字符串加载到具有特定时区的配置单元,并在配置单元中正确显示?

代码不会引用您的数据/蜂巢服务器所在的任何位置。 在您制作的示例中:

 .withColumn("timestamp", to_utc_timestamp(Col("raw"), "America/New_York"))

这实际上意味着我有时间戳列,我想将其转换为 UTC,然后您指定时间戳列应引用的时区。

所以让我们以 2019-07-14 00:00:00 为例,如果那个时间戳列 areazone 是 America/New_York,你实际上是说给我这个 UTC 的时间戳列,所以如果现在纽约的时间是 00 :00,UTC 时间是 05:00(因为纽约是 UTC -5)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM