1.问题描述
Hive表中存储的Timestamp类型的字段显示日期与Impala中查询出来的日期不一致。
2.问题复现

1.创建一个大略的测试表
create table date_test4(
id INT,
create_date INT,
create_date_str STRING
);
2.向表中插入一条测试数据
insert into date_test4 values(1,'1503751615','2017-08-26 08:46:55');
获取当前系统韶光存入表中:
3.通过Hive查询韶光显示如下
select id,create_date_str,from_unixtime(create_date) from date_test4;
4.通过Impala查询韶光显示如下
select id,create_date_str,cast(create_date as timestamp) from date_test4;
可以看到通过Hive查询看到的韶光与通过Impala查询看到的韶光不一致;
3.问题剖析
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
醉酒鞭名马,少年多浮夸!
岭南浣溪沙,呕吐酒肆下!
石友不肯放,数据玩的花!
温馨提示:要看高清无码套图,请利用手机打开并单击图片放大查看。