Hiv时间戳、时间格式转换
发布时间
阅读量:
阅读量
该文本详细讨论了在Hive数据库中使用unixtimestamp和fromunixtime函数进行时间戳与日期时间格式转换的操作。展示了如何将不同格式的时间字符串(如yyyy-MM-dd HH:mm:ss和yyyyMMdd HH:mm:ss)转换为10位的时间戳,并从时间戳重新获取正确的时间字符串。特别强调了以下几点:
from_unixtime函数的入参不能是字符串类型;
时间戳需要使用bigint类型;
当源值为string类型时需先将其转为bigint类型;
避免将13位整数直接作为入参;
提供了错误示例以说明不正确操作可能导致的报错原因及解决方法。
select unix_timestamp(右边格式的日期时间,'yyyy-MM-dd HH:mm:ss'); -- 返回10位时间戳
select unix_timestamp(右边格式的日期时间,'yyyyMMdd HH:mm:ss');-- 返回10位时间戳
select from_unixtime(时间戳,'yyyyMMdd HH:mm:ss'); -- 返回右边格式的日期时间,时间戳需要是bigint类型!!!
select from_unixtime(时间戳,'yyyy-MM-dd HH:mm:ss') -- 返回右边格式的日期时间,时间戳需要是bigint类型!!!
select from_unixtime(unix_timestamp(右边格式的日期时间,'yyyyMMdd HH:mm:ss'),'yyyy-MM-dd HH:mm:ss') ;
select from_unixtime(unix_timestamp(右边格式的日期时间,'yyyy-MM-dd HH:mm:ss'),'yyyyMMdd HH:mm:ss') ;
1 yyyyMMdd HH:mm:ss >> yyyy-MM-dd HH:mm:ss
hive> select from_unixtime(unix_timestamp('20190430 23:59:02','yyyyMMdd HH:mm:ss'),'yyyy-MM-dd HH:mm:ss') as created_time;
OK
2019-04-30 23:59:02
Time taken: 0.056 seconds, Fetched: 1 row(s)
hive>
2 yyyy-MM-dd HH:mm:ss >> yyyyMMdd HH:mm:ss
hive> select from_unixtime(unix_timestamp('2019-04-30 23:59:02','yyyy-MM-dd HH:mm:ss'),'yyyyMMdd HH:mm:ss') as created_time;
OK
20190430 23:59:02
Time taken: 0.06 seconds, Fetched: 1 row(s)
hive>
3 yyyy-MM-dd HH:mm:ss >> 10位时间戳
hive> select unix_timestamp('2019-04-30 23:59:02','yyyy-MM-dd HH:mm:ss');
OK
1556639942
Time taken: 0.053 seconds, Fetched: 1 row(s)
hive>
4 yyyyMMdd HH:mm:ss >> 10位时间戳
hive> select from_unixtime(1556639942,'yyyyMMdd HH:mm:ss') as created_time;
OK
20190430 23:59:02
Time taken: 0.059 seconds, Fetched: 1 row(s)
-- 注意:from_unixtime的时间戳入参不能是string类型!!!
hive> select from_unixtime('1556639942','yyyyMMdd HH:mm:ss') as created_time;
FAILED: SemanticException [Error 10014]: Line 1:7 Wrong arguments ''yyyyMMdd HH:mm:ss'':
No matching method for class......
hive>
5 时间戳转换为yyyyMMdd HH:mm:ss 或 yyyy-MM-dd HH:mm:ss
-- 需要使用cast(xxx as bigint)
hive> select from_unixtime(cast(1556639942000/1000 as bigint),'yyyyMMdd HH:mm:ss') as created_time;
OK
20190430 23:59:02
Time taken: 0.064 seconds, Fetched: 1 row(s)
-- 如果源值是string类型,还需要先使用cast(xxxx as bigint),然后再除以1000!!!!
hive> select from_unixtime(cast(cast('1556639942000' as bigint)/1000 as bigint),'yyyyMMdd HH:mm:ss') as created_time;
OK
20190430 23:59:02
Time taken: 0.086 seconds, Fetched: 1 row(s)
hive>
-- 注意:from_unixtime的时间戳不能是13位的整数!!!
hive> select from_unixtime(1556639942123,'yyyyMMdd HH:mm:ss') as created_time;
OK
512971215 07:55:23 -- 结果错误
Time taken: 0.101 seconds, Fetched: 1 row(s)
hive>
6 错误示例:
hive> select from_unixtime(1556639942123/1000,'yyyyMMdd HH:mm:ss') as created_time;
FAILED: SemanticException [Error 10014]
hive> select from_unixtime(1556639942000/1000,'yyyyMMdd HH:mm:ss') as created_time;
FAILED: SemanticException [Error 10014]
报错如下,可知:输入的入参是double类型的,但是第一个参数需要是int或bigint类型的:
No matching method for class org.apache.hadoop.hive.ql.udf.UDFFromUnixTime with (double, string).
Possible choices: _FUNC_(bigint) _FUNC_(bigint, string) _FUNC_(int) _FUNC_(int, string)
7 为何6报错??为何cast(xxx as bigint)正确?
hive> select 1556639942000/1000 ;
OK
1.556639942E9 -- 是double类型的,所以6报错
Time taken: 0.04 seconds, Fetched: 1 row(s)
hive> select 1556639942123/1000 ;
OK
1.556639942123E9 -- 是double类型的,所以6报错
Time taken: 0.05 seconds, Fetched: 1 row(s)
hive> select cast(1556639942123/1000 as bigint);
OK
1556639942 -- 是bigint类型的 ,所以5正确!!
Time taken: 0.05 seconds, Fetched: 1 row(s)
hive>
全部评论 (0)
还没有任何评论哟~
