细致剖析Hive的时间函数

1、时间戳与任何格式的日期互转
from_unixtime(时间戳,时间格式) 
    将时间戳转换为致指定的日期格式
    hive> select from_unixtime(1234567890,'yyyy-MM-dd');
    2009-02-14
    hive> select from_unixtime(1234567890,'yyyy-MM');
    2009-02
    hive> select from_unixtime(unix_timestamp());
    2017-01-07 11:36:55

unix_timestamp() 
    获取当前时间对应的时间戳
    hive> select unix_timestamp();
    1578208499

unix_timestamp(时间字符串)
    将yyyy-MM-dd HH:mm:ss格式的时间字符串转换为时间戳
    hive> select unix_timestamp('2019-11-11 10:10:10');
    1573438210
    
unix_timestamp(时间字符串,时间格式)
    将时间格式的时间字符串转换成时间戳
    hive> select unix_timestamp('2016-07-16 14:02:03','yyyy-MM-dd HH:mm:ss');
    1468648923


注意:
    使用以上两个函数,其实就完全可以将时任何格式的时间字符串转换成时间戳,或者将时间戳转换成任何格式的时间字符串。
    


2、获取年月日时分秒周星期
获取年份
    hive> select year('2019-12-31 10:31:20');
    2019
获取月份
    hive> select month('2019-12-31 10:31:20');
    12
获取日
    hive> select day('2019-12-30 10:31:20');
    30
获取小时数
    hive> select hour('2019-12-31 10:31:20');
    10
获取分钟数
    hive> select minute('2019-12-31 10:31:20');
    31
获取秒数
    hive> select second('2019-12-31 10:31:20');
    20
获取周数
    hive> select weekofyear('2019-01-23');
    4
获取星期
    hive> select dayofweek('2019-11-01');
    6

注意:
    以上获取年月日时分秒周的函数,如果遇到2019-01-02这样情况时,获取的月份是1而不是01,天是2而不是02。

4、日期运算
datediff(结束日期,开始日期)
    查询两个日期之间使劲按之差
    hive> select datediff('2019-12-31','2018-12-31');
    365

date_add(基准日期,天数)
    获取在基准日期上往后推移指定天数的日期
    hive> select date_add('2019-12-31',4);
    2020-01-04
    hive> select date_add('2019-12-31',-4);
    2019-12-27

date_sub(基准日期,天数)
    获取在基准日期上往前推移指定天数的日期
    hive> select date_sub('2019-12-31',4);
    2019-12-27
    hive> select date_sub('2019-12-31',-4);
    2020-01-04

add_months('时间字符串',[-]n)
    往前或往后推移n个月
    hive> select add_months('2019-11-11',-1);
    2019-10-11

思考题:
    如果获取两个时间(yyyy-MM-DD HH:mm:ss)之间的小时数之差?
    答:
       两个时间用hour()函数获取纸面小时数之差n,用datediff()获取天数之差m,m*24+n及小时数之差。




5、获取当前时间
current_timestamp()
    获取当前的时间,精确到毫秒 
    hive> select current_timestamp();
    2020-01-05 15:16:39.623

current_date()
    获取当前时间,精确到天
    hive> select current_date();
    2020-01-07

last_day(时间字符串)
    获取传入时间的最后一天
    hive> select last_day('2019-01-32');
    2019-02-28
    hive> select last_day('2019-01-00');  --这个很有意思哈哈
    2018-12-31   

to_date(时间字符串)
    从时间字符串中获取日期
    hive> select to_date('2019-12-31');
    2019-12-31
    hive> select to_date('2019-12-31 10:00:00');
    2019-12-31



6、trunc
获取当年当月第一天:
    hive> select trunc('2019-11-21','YYYY');
    2019-01-01
    hive> select trunc('2019-11-21','MM');
    2019-11-01
获取上年上月最后一天
    hive> select date_sub(trunc('2019-11-21','YYYY'),1);
    2018-12-31
    hive> select date_sub(trunc('2019-11-21','MM'),1);
    2019-10-31
获取本年本月的最后一天
    hive> select add_months(date_sub(trunc('2019-11-21','YYYY'),1),12);
    2019-12-31
    hive> select add_months(date_sub(trunc('2019-11-21','MM'),1),1);
    2019-11-30
    hive> select last_day('2019-11-21');
    2019-11-30
    

7、获取下个星期n的日期
    周一:MO;周二:TU;周三:WE ;周四:TH ;周五:FR ;周六:SA;周日SU
    hive> select next_day('2020-01-12','MO');
    2020-01-13
    hive> select next_day('2020-01-12','TU');
    2020-01-14
    hive> select next_day('2020-01-12','WE');
    2020-01-15
    hive> select next_day('2020-01-12','TH');
    2020-01-16
    hive> select next_day('2020-01-12','FR');
    2020-01-17
    hive> select next_day('2020-01-12','SA');
    2020-01-18
    hive> select next_day('2020-01-12','SU');
    2020-01-19


    
    









https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

你可能感兴趣的:(数据仓库Hive学习总结)