结论:使用 select date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss') 代替
select from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss') 。
总结:以后还是尽量不用官方弃用的函数。
今天在迁移项目时,发现HIVE中unix_timestamp得到的时间在测试环境和线上环境不一致,相差8小时。
使用 select from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss') 进行测试:
测试环境:
线上环境:
发现确实相差8小时,这是为什么呢?
猜测可能是环境版本不一样导致,遂立即查询两环境hive版本:
测试环境:2.1.1
线上环境:3.1.1
事实上,使用 select from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss') 时就会提示
unix_timestamp(void) is deprecated. Use current_timestamp instead.(unix_timestamp已过时,应使用current_timestamp代替)
> select from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss') ;
unix_timestamp(void) is deprecated. Use current_timestamp instead.
替换current_timestamp后,再次查询
select date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss') ;
结果俩环境时间显示都正常:
测试环境:
线上环境: