如何在Hive中将UTC的时间转为我们常用的时间呢?简单往下看!
先来看看官方怎么说
Return Type |
Name(Signature) |
Description |
bigint |
unix_timestamp(string date, string pattern) |
Convert time string with given pattern (see [http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html]) to Unix time stamp (in seconds), return 0 if fail: unix_timestamp('2009-03-20', 'yyyy-MM-dd') = 1237532400. |
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". |
看第二行,使用from_unixtime将UTC转为string时间,这里虽然这样说了,但是在处理的时候还是有要注意的地方的(注意下面的/1000)
SELECT from_unixtime(cast(1426041039030/1000 as bigint));
返回值为
2015-03-11 10:30:39
为了验证我的转换是否正确,我再使用postgresql进行一下转换
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1426041039030 * INTERVAL '1 MILLISECONDS'
结果为
2015-03-11 10:30:39.03+08
看到了是正确的,那个时间是带时区的,hive自动按服务器所在时区进行转换了。
扩展一下
如果要将正常的时间转为UTC呢?看下面
select unix_timestamp('2015-03-11 10:30:39');
select unix_timestamp('2015-03-11 10:30:39.03');
返回值为
1426041039
是不是发现和原来的
1426041039030
不一样,对少了030,这个是微妙数,这个我怎么也弄不出来,但是少了030再反转到正常时间后,就会出现问题了,这个问题等我解决了会再修改这篇博客。