Hive中常见的时间处理

发布于:2024-12-06 ⋅ 阅读:(104) ⋅ 点赞:(0)

参考:
hive中日期格式转换
在用命令行插入值的时候,需要进行类型转换,参考:
Hive 时间数据类型

常用函数

unix_timestamp函数, 日期转时间戳

返回值:bigint,10位
说明:自1970年1月1日午夜(UTC)以来的秒数;获得当前时区的UNIX时间戳(UTC为世界统一时间);北京时间将比UTC时间快8个小时。

  • 当函数参数为空时,返回当前时间戳。
select unix_timestamp()  ---1571282494
  • 不输入时间格式,默认’yyyy-MM-dd HH:mm:ss’格式
select unix_timestamp(string date)
select unix_timestamp('2019-09-11 23:23:12') --1568215392
select unix_timestamp('2019-09-11')      ---返回null
  • 正常情况下使用自定义格式,当输入类型和格式匹配返回正确值,否则返回null。
select unix_timestamp(string date, string pattern)
select unix_timestamp('2019-09-11','yyyy-mm-dd') --1547136540

from_unixtime函数 时间戳转日期

返回值:string
说明:转化UNIX时间戳(自1970年1月1日午夜(UTC)以来的秒数)到当前时区的时间格式

select from_unixtime(bigint unixtime[, string format])

format:yyyy-MM-dd HH:dd:ss.sss(毫秒)、yyyy-MM-dd HH:dd:ss(秒)、yyyy-MM-dd(日期)等

  • 默认格式 ‘yyyy-MM-dd HH:mm:ss’,不能为空值,可以指定特定格式
select from_unixtime(1547136540) --2019-01-11 00:09:00
select from_unixtime(1547136540,'HH:mm:ss') --00:09:00

其他

一、…1、…2、…
以下参考:hive中日期格式转换

3、经常使用的用法:日期格式之间的互换
把20190911转化2019/09/11

select from_unixtime(unix_timestamp('20190911','yyyyMMdd'),'yyyy/MM/dd')

   
   
   

4、日期格式yyyy-MM-dd格式转化成其他格式
date_format主要针对yyyy-MM-dd的时间格式转化成其他格式的日期字符串。

select date_format('2019-09-11','yyyy/MM/dd')

   
   
   
二、其他时间函数,针对’yyyy-MM-dd HH:mm:ss’

1、to_date函数,返回日期时间中的日期部分
通常只能应用于’yyyy-MM-dd HH:mm:ss’格式的日期时间截取

select to_date('2019-09-11 12:10:21') --2019-09-11

   
   
   

2、year, month, day, hour, minute,second函数

select second('2019-09-11 12:10:21') ---21

   
   
   

3、日期转周函数weekofyear

select weekofyear('2011-12-08 10:03:01') from dual;--49

   
   
   
三、时间函数运算

1、datediff 日期比较函数,默认格式’yyyy-MM-dd HH:mm:ss’

select datediff('2019-01-11','2019-08-11')   --输出-212

   
   
   

2、时间增加函数 date_add

select date_add('2012-12-08',10)

   
   
   

3、时间减少函数 date_sub

select date_sub('2019-09-11',10)

   
   
   

4、获取两个时间的相差月份数

select floor(months_between('2018-07-01','2018-02-04')) from default.dual

   
   
   
四、获取当前时间
select current_date()   --2019-10-17 获取日期格式
select unix_timestamp(). --1571285121  获取时间戳

   
   
   

hive中如何将13位bigint类型的时间戳的转化为毫秒标准时间格式

这一部分是因为做核桃编程的笔试题,遇到了unix_timestamp这个格式的bigint类型的时间,且单位精确到ms(13位),而hive的unix_timestamp是秒级别的(10位),不是毫秒级别,暂不清楚该时间的产生原因,可能的原因为:

Java的date默认精度是毫秒,由date转换成的时间戳是13位的,而c,php生成的时间默认就是10位的,精度是秒。

下面进行转换:
参考: hive中如何将13位bigint类型的时间戳的转化为毫秒标准时间格式
首先想到:

SELECT FROM_UNIXTIME(1571709884123/1000,'yyyy-MM-dd HH:dd:ss.sss');
--2019-10-22 10:22:44.044

1571709884123/1000是为了转换成hive的10位时间戳,但是毫秒部分存在精度问题。
解决精度问题:

select
    concat(t,".",substring(1571709884123,11,13))
from(
select from_unixtime(cast(substring(1571709884123,0,10) as bigint),'yyyy-MM-dd HH:dd:ss') as t
)tmp;

3、经常使用的用法:日期格式之间的互换
把20190911转化2019/09/11

select from_unixtime(unix_timestamp('20190911','yyyyMMdd'),'yyyy/MM/dd')

   
   
 

4、日期格式yyyy-MM-dd格式转化成其他格式
date_format主要针对yyyy-MM-dd的时间格式转化成其他格式的日期字符串。

select date_format('2019-09-11','yyyy/MM/dd')

   
   
 
二、其他时间函数,针对’yyyy-MM-dd HH:mm:ss’

1、to_date函数,返回日期时间中的日期部分
通常只能应用于’yyyy-MM-dd HH:mm:ss’格式的日期时间截取

select to_date('2019-09-11 12:10:21') --2019-09-11

   
   
 

2、year, month, day, hour, minute,second函数

select second('2019-09-11 12:10:21') ---21

   
   
 

3、日期转周函数weekofyear

select weekofyear('2011-12-08 10:03:01') from dual;--49

   
   
 
三、时间函数运算

1、datediff 日期比较函数,默认格式’yyyy-MM-dd HH:mm:ss’

select datediff('2019-01-11','2019-08-11')   --输出-212

   
   
 

2、时间增加函数 date_add

select date_add('2012-12-08',10)

   
   
 

3、时间减少函数 date_sub

select date_sub('2019-09-11',10)

   
   
 

4、获取两个时间的相差月份数

select floor(months_between('2018-07-01','2018-02-04')) from default.dual

   
   
 
四、获取当前时间
select current_date()   --2019-10-17 获取日期格式
select unix_timestamp(). --1571285121  获取时间戳

   
   
 

网站公告

今日签到

点亮在社区的每一天
去签到