0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。

1.问题描述

Hive表中存储的Timestamp类型的字段显示日期与Impala中查询出来的日期不一致。

2.问题复现

1.创建一个简单的测试表

0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

2.向表中插入一条测试数据

insert into date_test4 values(1,'1503751615','2017-08-26 08:46:55');

获取当前系统时间存入表中:

0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

3.通过Hive查询时间显示如下

select id,create_date_str,from_unixtime(create_date) from date_test4;

0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

4.通过Impala查询时间显示如下

select id,create_date_str,cast(create_date as timestamp) from date_test4;

0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

可以看到通过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;

0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

指定时区后时间与原始Hive中显示时间一致,时区查看参考如下地址:

http://zh.thetimenow.com/time-zones-abbreviations.php

醉酒鞭名马,少年多浮夸! 岭南浣溪沙,呕吐酒肆下!挚友不肯放,数据玩的花!
温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。
0014-Hive中的Timestamp类型日期与Impala中显示不一致分析

猜你喜欢

转载自blog.51cto.com/14049791/2318338
今日推荐