1. 程式人生 > 實用技巧 >hive日期函式

hive日期函式

Hive 中,可以用String、Date和Timestamp表示日期時間,String 用 yyyy-MM-dd 的形式表示,Date 用 yyyy-MM-dd 的形式表示,Timestamp 用 yyyy-MM-dd hh:mm:ss 的形式表示。這三種資料型別在使用細節上,有一些需要注意的點:


Join比較
在兩表Join時,會涉及到欄位的比較,此時應注意:

如第一張圖所示,如果時間資訊中不包含時分秒,String 與 Date、Timestamp 表達的時間相同時是可以直接比較的,但是Date和Timestamp之間卻不能直接比較的。
如果想比較這兩種時間型別,需要用cast函式做轉換,如:a_table join b_table on (a_table.timestamp_column = cast(b_table.date_column as timestamp));

如第二張圖所示,如果時間資訊中包含時分秒,此時String 與 Timestamp 表達的時間相同時是可以直接比較的,Date 不能表示帶時分秒的資訊。
Insert value
在insert value時,使用者一般用字串的形式向Hive表中插入value,但是字串的插入的結果與欄位型別相關。如上圖所示,圖中綠線表示插入成功,紅線表示插入失敗得到了不想要的結果。

一、相互轉化

格式化 date_format

select date_format('2019-04-08', 'yyyy') --得到:2019
select date_format('2019-04-08', 'yyyy-MM') --得到:2019-04
select date_format('2019-04-08', 'yy-MM') --得到:19-04
  • 1
  • 2
  • 3

注意:也可以用substr 方式擷取年月或者substr+concat結合

字串轉時間 to_date

語法: to_date(string timestamp)
返回值: string
說明: 返回日期時間欄位中的日期部分。
舉例:

hive> select to_date('2019-02-16 14:02:03') from dual;
OK
2019-02-16
  • 1
  • 2
  • 3

日期轉年函式: year

語法: year(string date)

返回值: int

說明: 返回日期中的年。

舉例:

hive> select year(’2019-12-08 10:03:01′) ;

2011

hive> select year(’2019-12-08′) ;

2012
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

日期轉月函式: month

語法: month (string date)

返回值: int

說明: 返回日期中的月份。

舉例:

hive> select month(’2019-12-08 10:03:01′);

12

hive> select month(’2019-08-08′);

8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

【注意】這裡的轉成月份是0-12的數字顯示不出年份,要輸出‘2019-12’這種格式,需要用到時間戳函式

select from_unixtime( unix_timestamp(欄位名), 'yyyy-MM' ) from 表名
  • 1

例如

-- 獲取當前時間戳(1565858389)
select unix_timestamp()   

--獲取當前日期和時間(2019-11-13 17:18:55)
select from_unixtime(unix_timestamp()) 

--獲取當前日期和時間,精確到毫秒(2019-11-13 17:18:55.720)
select current_timestamp()

-- 獲取當前日期(2019-11-13)
 select from_unixtime(unix_timestamp(),'yyyy-MM-dd')
 
--  獲取當前年份(2019)
 select from_unixtime(unix_timestamp(),'yyyy')
 
 -- 獲取當前月份(0-12的數字)
  select from_unixtime(unix_timestamp(),'MM')


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

日期轉周函式: weekofyear

語法: weekofyear (string date)

返回值: int

說明: 返回日期在當前的週數。

舉例:

hive> select weekofyear(’2019-12-08 10:03:01′);

49
  • 1
  • 2
  • 3

日期轉天函式: day

語法: day (string date)

返回值: int

說明: 返回日期中的天。

舉例:

hive> select day(’2019-12-08 10:03:01′) ;

8

hive> select day(’2019-12-24′);

24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

日期轉小時函式: hour

語法: hour (string date)

返回值: int

說明: 返回日期中的小時。

舉例:

hive> select hour(’2019-12-08 10:03:01′) ;

10
  • 1
  • 2
  • 3

日期轉分鐘函式: minute

語法: minute (string date)

返回值: int

說明: 返回日期中的分鐘。

舉例:

hive> select minute(’2019-12-08 10:03:01′) ;

3
  • 1
  • 2
  • 3

日期轉秒函式: second

語法: second (string date)

返回值: int

說明: 返回日期中的秒。

舉例:

hive> select second(’2019-12-08 10:03:01′);

1
  • 1
  • 2
  • 3

二、時間戳和日期格式互轉

1. 日期>>>>時間戳

(1)unix_timestamp() 獲取當前時間戳

例如:select unix_timestamp()   --1565858389
  • 1
注意事項:

(a)unix_timestamp(string timestamp) 輸入的時間戳格式必須為’yyyy-MM-dd HH:mm:ss’,如不符合則返回null

例如:

select unix_timestamp('2019-08-15 16:40:00')   --1565858400
select unix_timestamp('2019-08-15')  --null
  • 1
  • 2

(b)unix_timestamp(string date,string pattern) 將指定時間字串格式字串轉化成unix時間戳,如不符合則返回null

例如:

select unix_timestamp('2019-08-15','yyyy-MM-dd')   --1565798400

select unix_timestamp('2019-08-15 16:40:00','yyyy-MM-dd HH:mm:ss')   --1565858400

select unix_timestamp('2019-08-15','yyyy-MM-dd HH:mm:ss')   --null
  • 1
  • 2
  • 3
  • 4
  • 5

2.時間戳>>>>日期

(1)普通格式
from_unixtime(bigint unixtime,string format) 將時間戳秒數轉化為UTC時間,並用字串表示,指定輸出的時間格式,其中unixtime 是10位的時間戳值,而13位的所謂毫秒的是不可以的。

例如:

--2019-08-15 16:39:49
select from_unixtime(1565858389,'yyyy-MM-dd HH:mm:ss')  

--2019-08-15
select from_unixtime(1565858389,'yyyy-MM-dd') 
  • 1
  • 2
  • 3
  • 4
  • 5

(2)特殊格式:先轉10位

如果unixtime為13位的,需要先轉成10位

--2019-03-22 00:00:00
select from_unixtime(cast(1553184000488/1000 as int),'yyyy-MM-dd HH:mm:ss')

--2019-03-22 00:00:00 
select from_unixtime(cast(substr(1553184000488,1,10) as int),'yyyy-MM-dd HH:mm:ss')  
  • 1
  • 2
  • 3
  • 4
  • 5

三、獲取當前日期和時間

語法:from_unixtime( unixtime,“string format”)後面的年月日格式可以根據需要自己設定
或者用current_timestamp()current_date()

舉例:

--1. 獲取當前日期和時間(年月日時分秒)
--寫法一:
select from_unixtime(unix_timestamp(),"yyyy-MM-dd HH:mm:ss")
2020-04-21 11:02:55
--寫法二:
select substr(current_timestamp(),1,19)
2020-04-21 11:02:55


-- 2.獲取當前日期
--寫法一:
select from_unixtime(unix_timestamp(),"yyyy-MM-dd")
2020-04-21
--寫法二:(推薦)
select current_date()或者select current_date
2020-04-21
-- 寫法三:
select substr(current_timestamp(),1,10)
2020-04-21

--3.  獲取當前年份(2020)
 select from_unixtime(unix_timestamp(),'yyyy')
 2020
 -- 獲取當前月份(0-12的數字)
 select from_unixtime(unix_timestamp(),'MM')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

四、時間拼接和更改格式

方法1: from_unixtime+ unix_timestamp
--20171205轉成2017-12-05 
select from_unixtime(unix_timestamp('20171205','yyyymmdd'),'yyyy-mm-dd') from dual;

--2017-12-05轉成20171205
select from_unixtime(unix_timestamp('2017-12-05','yyyy-mm-dd'),'yyyymmdd') from dual;

方法2: substr + concat
--20171205轉成2017-12-05 
select concat(substr('20171205',1,4),'-',substr('20171205',5,2),'-',
substr('20171205',7,2)) from dual;

--2017-12-05轉成20171205
select concat(substr('2017-12-05',1,4),substr('2017-12-05',6,2),
substr('2017-12-05',9,2)) from dual;