天天看點

細緻剖析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