hive中获取日期与时间戳和当前时间戳
hive中的函数,日期转时间戳代码,
select unix_timestamp('2020-01-16 17:02:00');
+-------------+
| _c0 |
+-------------+
| 1579212120 |
+-------------+
hive中的时间转时间戳函数(不加引号)第二个参数为默认;
select from_unixtime(1579212120,'yyyy-mm-dd hh:mm:ss') ;
+----------------------+
| _c0 |
+----------------------+
| 2020-01-16 17:02:00 |
+----------------------+
hive中获取设备中的当前时间的时间戳信息
select unix_timestamp() ;
+-------------+
| _c0 |
+-------------+
| 1579196036 |
+-------------+
知识拓展:
hive中的时间戳精确到秒级10位,而scala和java中的时间戳是精确到毫秒级的13位所以我们获取scala中的时间戳要除以1000才能被hive识别
当我们在scala中求出时间往hive中添加的时候
select from_unixtime(1575430219000/1000) ;//会报错在hive中这样除会得到一个double类型的值,所以我们要用cast强转数据类型
我们添加一个cast强转
select from_unixtime(cast(1575430219000/1000 as bigint)) ;
+----------------------+
| _c0 |
+----------------------+
| 2019-12-03 22:30:19 |
+----------------------+
获取时间戳的年份
select year(from_unixtime(cast(1575430219000/1000 as bigint))) ;
+-------+
| _c0 |
+-------+
| 2019 |
+-------+
获取时间戳的月份
select month(from_unixtime(cast(1575430219000/1000 as bigint))) ;
+------+
| _c0 |
+------+
| 12 |
+------+
获取时间戳的天
select day(from_unixtime(cast(1575430219000/1000 as bigint))) ;
+------+
| _c0 |
+------+
| 3 |
+------+