1.问题描述php
Hive表中存储的Timestamp类型的字段显示日期与Impala中查询出来的日期不一致。html
2.问题复现apache
1.建立一个简单的测试表函数
2.向表中插入一条测试数据测试
insert into date_test4 values(1,'1503751615','2017-08-26 08:46:55');
获取当前系统时间存入表中:ui
3.经过Hive查询时间显示以下3d
select id,create_date_str,from_unixtime(create_date) from date_test4;
4.经过Impala查询时间显示以下unix
select id,create_date_str,cast(create_date as timestamp) from date_test4;
能够看到经过Hive查询看到的时间与经过Impala查询看到的时间不一致;code
3.问题分析orm
3.1Hive的from_unixtime
Hive官网from_unixtime函数说明:
Return Type | Name(Signature) | Description |
---|---|---|
string | from_unixtime(bigint unixtime, string format) | Converts the number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a string representing the timestamp of that moment in the current system time zone in the format of "1970-01-01 00:00:00". |
在Hive中经过from_unixtime函数将TIMESTAMP时间戳转换成当前时区的日期格式的字符串,默认格式为“yyyy-MM-dd HH:mm:ss”,因此Hive在查询的时候能正确的将存入的时间戳转成当前时区的时间;
3.2Impala的TIMESTAMP
默认状况下,Impala不会使用本地时区存储时间戳,以免意外的时区问题形成没必要要的问题,时间戳均是使用UTC进行存储和解释。具体说明请参考官方文档:
http://impala.apache.org/docs/build/html/topics/impala_timestamp.html#timestamp
4.解决方法
使用Impala的from_utc_timestamp函数指定时区进行时间转换,事例以下:
select id,create_date_str, cast(create_date as timestamp),from_utc_timestamp(cast(create_date as timestamp), 'EDT') from date_test4;
指定时区后时间与原始Hive中显示时间一致,时区查看参考以下地址:
http://zh.thetimenow.com/time-zones-abbreviations.php
醉酒鞭名马,少年多浮夸! 岭南浣溪沙,呕吐酒肆下!挚友不愿放,数据玩的花!