在Hive3.x的版本中执行如下函数:

select unix_timestamp('2022-07-26 09:22:39');

会比实际的值差8小时,查看系统时区,以及JVM的时区配置都没有问题,翻看代码可以看到在HiveServer2中to_unix_timestamp将时区强制设置成了UTC:

image

to_unix_timestamp函数在hive 2.7版本中的12192 freatue把unit time stamp统一设置成了utc,https://www.mail-archive.com/issues@hive.apache.org/msg124569.html

unix_timestamp 背后调用的是java.sql.Timestamp,之前是使用lcoal时区,当你的时区是America/Los_Angeles 的时候,会有一个bug会返回错误的值。

为了解决这个问题hive统一把unix_timestamp的时区设置成了utc,上层各自根据自己的业务处理,因此如果需要使用该函数,需要自行传入参数。

unix_timestamp函数默认也为utc:

image
image

通过代码可以看到,如果arguments大于0,会调用super.initializeInput(arguments);

image

如果小于0,直接输出的为前面的UTC时区。

因此执行这样的sql:

select unix_timestamp('2022-07-26 09:22:39');

建议的使用方式为:

SELECT unix_timestamp(to_utc_timestamp ('2022-07-26 09:22:39','GMT-8'));

通过自行设置时区。


扫码手机观看或分享: