Hive日期格式转换详解(包含13位时间戳转毫秒时间)


        在生产实践中,例如:需要对页面点击行为日志进行ETL清洗,这时会发现,同一秒中会有多个请求日志,需要区分这些请求日志的先后顺序,必须采用毫秒统计。这就意味着将13位时间戳除以1000转换成10位的方法已经不复存在了。

        虽然hive中没有提供13位时间戳转换内置函数,但是我们可以使用以下方法解决:

--时间戳转换成固定日期
select
    concat(t,".",substring(1629637370845,11,13))
from(
    select from_unixtime(
        cast(substring(1629637370845,0,10) as bigint),
        'yyyy-MM-dd HH:mm:ss') as t
)tmp;
 
输出结果:
2021-08-22 13:02:50.845
 
 
-- 固定日期转换成时间戳
select concat(t,substring('2021-08-22 13:02:50.845',21,23))
from (
    select unix_timestamp(
        concat(substring('2021-08-22 13:02:50.845',1,19),' +0800')
        ,'yyyy-MM-dd HH:mm:ss Z') as t
)tmp;
输出结果:
1629608570845
备注:

        1、读者也可以使用自定义函数完成,实现过程也很简单。

        2、from_unixtime存在一些小坑,后文将会具体介绍。

关于UTC时间
        世界的每个地区都有自己的本地时间,整个地球分为二十四时区,每个时区都对应着自己的本地时间。在国际无线电通信中,为了统一时间,而普遍使用一个标准时间,称为通用协调时(UTC, Universal Time Coordinated)。UTC与格林尼治平均时(GMT, Greenwich Mean Time)一样,都与英国伦敦的本地时相同。UTC与GMT含义完全相同。

        以北京时间东八区(UTC+8)为例,需要将UTC时间转换成北京时间,可以使用以下两个函数:

       1、 to_utc_timestamp(timestamp, timezone)        --将timestamp转换成UTC时间

       2、 from_utc_timestamp(timestamp, timezone)    --UTC时间转换成timezone时间

--UTC时间转换成北京时间
select from_utc_timestamp(1629637370845,'PRC');
select from_utc_timestamp(1629637370845,'Asia/Shanghai');
输出结果:
2021-08-22 21:02:50.845
2021-08-22 21:02:50.845
 
--北京时间转换成UTC/GMT时间
select to_utc_timestamp(1629637370845,'UTC');
select to_utc_timestamp(1629637370845,'GMT');
输出结果:
2021-08-22 13:02:50.845
2021-08-22 13:02:50.845
 备注:北京时间可以使用PRC(the People's Republic of China,中华人民共和国)简写,也可以使用Asia/Shanghai(亚洲/上海)。

获取当前时间
UNIX_TIMESTAMP()         -- 得到时间戳

--返回当天三种方式
SELECT CURRENT_DATE();
SELECT CURRENT_TIMESTAMP();
select unix_timestamp();
输出结果:
2021-08-22
2021-08-22 14:48:18.455
1629614898
 
 
--返回当月的第一天
select trunc('2021-08-22','MM');
--返回当年的第一天
select trunc('2021-08-22','YEAR');
输出结果:
2021-08-01
2021-01-01
from_unixtime()填坑
1、from_unixtime(unix_time, format)         --返回指定时间内的unix_时间

2、date_format(date/timestamp/string, fmt)

                                --将日期/时间戳/字符串转换为日期格式fmt指定格式的字符串值。

select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss');
select date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss');
输出结果:
2021-08-22 09:34:11
2021-08-22 17:34:11

        当前日期:2021-08-22 17:34:11

        读者可能会发现两者时间相差8个小时,翻找官方文档查看unix_timestamp获取时间属性:

Converts time string in format yyyy-MM-dd HH:mm:ss to Unix timestamp (in seconds), using the default timezone and the default locale, return 0 if fail;
翻译:
将时间字符串转换为Unix时间戳(以秒为单位),使用默认时区和默认语言环境,如果失败,返回0。

        所以想要获取正确的时间,可以修改虚拟机的默认时区和默认语言环境,或者简单的使用select date_format(current_timestamp,'yyyy-MM-dd HH:mm:ss')代替。实际上,当在使用unix_timestamp()时,官方已经提示unix_timestamp(void)被弃用,如图所示:

 小提示:我们还可以使用 select from_utc_timestamp(unix_timestamp() * 1000,'PRC');来获取正确的时间。

固定日期转换成时间戳
1、to_unix_timestamp(date[, pattern])        --返回UNIX时间戳

2、unix_timestamp(date[, pattern])            --将时间转换为数字

select to_unix_timestamp('2021-08-22 14:51:30 +0800','yyyy-MM-dd HH:mm:ss Z');
 
select unix_timestamp('2021-08-22 14:51:30 +0800','yyyy-MM-dd HH:mm:ss Z');
 
返回结果:
1629615090
1629615090
 
--注意:如果不给定时区,会比正确结果领先8小时。
select unix_timestamp('2021-08-22 14:51:30 +0800','yyyy-MM-dd HH:mm:ss');
返回结果:
1629643890
特殊的,将22/Aug/2021:15:05:01 +0800转换成时间戳: 
select unix_timestamp('22/Aug/2021:15:05:01 +0800','dd/MMM/yyy:HH:mm:ss Z');
输出结果:
1629615901
        2.特殊的,将22/Aug/2021:15:05:01 +0800转换成正常格式(yyyy-MM-dd HH:mm:ss):

select from_unixtime(
        to_unix_timestamp('22/Aug/2021:15:05:01 +0800', 'dd/MMM/yyy:HH:mm:ss')
);
输出结果:
2021-08-22 15:05:01
备注:如果将写成

select from_unixtime(
        to_unix_timestamp('22/Aug/2021:15:05:01 +0800', 'dd/MMM/yyy:HH:mm:ss Z')
);

则会晚8小时,输出结果为:

2021-08-22 07:05:01

返回日期时间字段中的日期部分
返回日期:2021-08-22
        select to_date('2021-08-22 14:48:18');

返回日期中的年:2021
        select year('2021-08-22 14:48:18');

返回日期中的月:8
        select month('2021-08-22 14:48:18');

返回日期中的日:22
        select day('2021-08-22 14:48:18') ;

返回日期中的时:14
        select hour('2021-08-22 14:48:18') ;

返回日期中的分:48
        select minute('2021-08-22 14:48:18') ;

返回日期中的秒:18
        select second('2021-08-22 14:48:18') ;

返回日期在当前的周数:33
        select weekofyear('2021-08-22 14:48:18') ;

返回日期中date是星期几:1(周日--1、周一--2、周二--3.......)

        select dayofweek('2021-08-22 14:48:18');

返回日期的增减操作
返回结束日期减去开始日期的天数:10
        select datediff('2021-08-22','2021-08-12')

返回开始日期增加days天后的日期:2021-09-01
        select date_add('2021-08-22',10)

返回开始日期减少days天后的日期:2021-08-12
        select date_sub('2021-08-22',10)
 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值