最近新搭的平台用到了hive 3.1的版本,發現和1.2的版本用法上存在差異
hive 3.1源碼
其中涉及到時間轉化的兩個常用函數 from_unixtime 和 unix_timestamp 兩者的TimeZone不在是從hive配置或者系統配置中獲取,而是直接寫死的UTC
hive 1.2 最后獲取依賴 rt.jar包
這樣就導致了帶參的from_unixtime 和 unix_timestamp 函數 結果就是以UTC來算的,而不是我們北京時間PRC
為了兼容原來的功能,我們需做代碼層上的修改
如:
from_unixtime: 1.2版本: select from_unixtime(1596988800,'yyyy-MM-dd HH:mm:ss'); 3.1版本: from_utc_timestamp(1596988800*1000L,'PRC'); //默認是yyyy-MM-dd HH:mm:ss 轉化為其他格式: date_format(from_utc_timestamp(c_timestamp*1000L,'PRC'),'yyyy-MM-dd HH') unix_timestamp: 1.2版本: select unix_timestamp('2020-08-10 23:42:11','yyyy-MM-dd HH:mm:ss'); 3.1版本: unix_timestamp(to_utc_timestamp('2020-08-10 23:42:11','PRC'),'yyyy-MM-dd HH:mm:ss')