获取系统当前时间
unix_timestamp()
在hive
中获取系统当前时间可以使用unix_timestamp()
函数,不过这个函数获取的是bigint
值如下
从上可以看到获取到了当前系统时间,数值类型是0: jdbc:hive2://lx01:10000> select unix_timestamp(); +-------------+ | _c0 | +-------------+ | 160886930 | +-------------+
bigint
的,但是我们通常是想获取标准时间,这个时候可以和from_unixtime()
函数配合使用,得到标准时间。from_unixtime(bigint n,string str)
from_unixtime()
函数和unix_timestamp()
函数的组合使用方式如下from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
,通过这种方式就可以得到标准的系统当前时间,如下所示
可以看到获取到了我们需要的标准系统当前时间。0: jdbc:hive2://lx01:10000> select from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss'); +----------------------+ | _c0 | +----------------------+ | 2020-12-24 21:42:27 | +----------------------+
unix_timestamp(string str)
上面介绍了如何获取标准的系统时间,当unix_timestamp()
函数的括号中不填任何参数时获取的是当前系统时间的时间戳,如果在括号括号中填入标准的'yyyy-MM-dd HH:mm:ss'
的字符串就可以得到任意指定的标准时间的时间戳,如下所示
如果字符串只有年月日,如0: jdbc:hive2://lx01:10000> select unix_timestamp('2010-12-12 12:22:31'); +-------------+ | _c0 | +-------------+ | 1292127751 | +-------------+
'2020-01-15'
,可以通过如下方式获取时间戳
unix_timestamp('2020-01-15','yyyy-MM-dd')
,结果如下所示
这样就可以拿到我们想要的时间戳。0: jdbc:hive2://lx01:10000> select unix_timestamp('2020-01-15', 'yyyy-MM-dd'); +-------------+ | _c0 | +-------------+ | 1579017600 | +-------------+