我有2列:time_stamp和time_offset。两者都是STRING
数据类型。
如何在UTC的第二列的帮助下将一个列值转换为UTC?他们的任何配置单元或unix解决方案是否将time_stamp列转换为UTC?
hive> select time_stamp from table1 limit 2;
OK
20170717-22:31:57.348
20170719-21:10:15.393
[yyyymmdd-hh:mm:ss.msc] this column is in local time
hive> select time_offset from table1 limit 2;
OK
-05:00
+05:00
[‘+hh:mm’ or ‘-hh:mm’ ] this column is in UTC
答案 0 :(得分:1)
您可以使用配置单Date Functions unix_timestamp
和from_unixtime
执行转换。
WITH table1 AS (
SELECT '20170717-22:31:57.348' AS time_stamp, '-05:00' AS time_offset UNION ALL
SELECT '20170719-21:10:15.393' AS time_stamp, '+05:00' AS time_offset
)
SELECT
time_stamp,
time_offset,
unix_timestamp(concat(time_stamp, ' ', time_offset), 'yyyyMMdd-HH:mm:ss.SSS X') AS unix_timestamp_with_offset,
from_unixtime(unix_timestamp(concat(time_stamp, ' ', time_offset), 'yyyyMMdd-HH:mm:ss.SSS X'), 'yyyyMMdd-HH:mm:ss.SSS') AS string_timestamp_with_offset
FROM table1
;
+------------------------+--------------+-----------------------------+-------------------------------+--+
| time_stamp | time_offset | unix_timestamp_with_offset | string_timestamp_with_offset |
+------------------------+--------------+-----------------------------+-------------------------------+--+
| 20170717-22:31:57.348 | -05:00 | 1500348717 | 20170717-20:31:57.000 |
| 20170719-21:10:15.393 | +05:00 | 1500480615 | 20170719-09:10:15.000 |
+------------------------+--------------+-----------------------------+-------------------------------+--+
unix_timestamp
可以使用与Java SimpleDateFormat
相同的语法接受可选格式字符串。我猜你的偏移使用的是ISO 8601语法,所以让我们使用X
格式说明符。然后,在传递给concat
之前,我们可以使用time_stamp
String Operator来合并time_offset
和unix_timestamp
。
unix_timestamp
函数导致数字时间戳指定为自纪元以来的秒数。要将其转换回字符串表示形式,我们可以将从unix_timestamp
获得的结果传递给from_unixtime
,这次指定我们的原始格式说明符。
(请彻底测试以确保结果在您的环境中有意义。时区数学可能会非常棘手。)