hive時間日期函數及典型場景應用


1、hive取得當前日期時間:

1.1) 取得當前日期:
select current_date();

1.2) 取得當前日期時間:
select current_timestamp();

1.3) hive取得當前時間戳:
select unix_timestamp();

1.4) 時間戳轉日期:
select from_unixtime(1517725479,'yyyy-MM-dd HH:dd:ss');

1.5) 日期轉unix時間戳:
select to_nuix_timestamp('2017-01-01 12:12:12','yyyy-MM-dd HH:dd:ss');

1.6) hive取得當前時間:
select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:dd:ss');

2、hive自動計算其他日期(昨天,今天):

hive中日期加減函數:date_add(start_date,num_days)

2.1) 取得昨天日期:

select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);

2.2) 取得明天日期:

select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);

3、hive取得兩個日期之間差值(差值為天數):

datediff(date1,date2):date1大於date2,返回值為正,否則,返回值為負。

select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-10));
select datediff(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),10));

4、字符串轉時間(字符串必須為:yyyy-MM-dd格式)

select to_date('2017-01-01 12:12:12');

5、日期、時間戳、字符串類型格式化輸出標准時間格式:

select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');

select date_format(current_date(),'yyyyMMdd');

select date_format('2017-01-01','yyyy-MM-dd HH:mm:ss');       --字符串必須滿足yyyy-MM-dd格式

6、utc時間轉換:

select from_utc_timestamp(current_timestamp(),8);

select to_utc_timestamp(current_timestamp(),8);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM