hive time_stamp使用UTC中的time_offset转换为UTC

时间:2017-08-01 18:02:37

标签: unix hadoop hive apache-pig hadoop2

我有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

1 个答案:

答案 0 :(得分:1)

您可以使用配置单Date Functions unix_timestampfrom_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_offsetunix_timestamp

unix_timestamp函数导致数字时间戳指定为自纪元以来的秒数。要将其转换回字符串表示形式,我们可以将从unix_timestamp获得的结果传递给from_unixtime,这次指定我们的原始格式说明符。

(请彻底测试以确保结果在您的环境中有意义。时区数学可能会非常棘手。)