【Hive】一些函數

一、substr函數(截取字符串)

語法:substr(string str,int start,int end)

說明:str指定字符串(字段),start指定截取開始的位置,end指定截取幾個字符

二、split函數(分割字符串)

語法: split(string str, string pat)
返回值: array
說明: 按照pat字符串分割str,會返回分割後的字符串數組
舉例:
1.基本用法

hive> select split('abcdef', 'c') from test;
["ab", "def"]

2.截取字符串中的某個值

//獲取年月日
hive (default)> select split('2020-04-27 20:35:38',' ')[0];
OK
_c0
2020-04-27
Time taken: 0.827 seconds, Fetched: 1 row(s)

連續切分

//獲取月份
hive (default)> select split(split('2020-04-27 20:35:38',' ')[0],'-')[1];
OK
_c0
04
Time taken: 0.069 seconds, Fetched: 1 row(s)

3.特殊字符
如正則表達式中的特殊符號作爲分隔符時,需做轉義 (前綴加上\)

hive> select split('ab_cd_ef', '\_')[0] from test;
ab
hive> select split('ab?cd_ef', '\\?')[0] from test;
ab
1
2
3
4

如果是在shell中運行,則(前綴加上\\)

hive -e "select split('ab?cd_ef', '\\\\?')[0] from test" 
1

注:有些特殊字符轉義只需\,而有些需\\,eg.?。可能在語句翻譯過程中經歷經歷幾次轉義。

 

三、hive根據日期獲取星期幾的方法

方式一:

dayofweek(#date#)

 

方式二:

hive原生未提供獲取一個日期是星期幾的方法,所以只有我們自己編寫udf函數提供,udf就不說了,在這裏給出了一個使用hive原生函數獲取星期幾的技巧。

pmod(datediff(#date#, '1920-01-01') - 3, 7) 

#date#表示給的日期。

輸出的結果爲0-6的數,分別表示 日,一,二 ... 六。

hive (itcast)> select pmod(datediff('2020-04-27 21:08:12', '1920-01-01') - 3, 7);
OK
_c0
1
Time taken: 0.052 seconds, Fetched: 1 row(s)
hive (itcast)> 

如果想讓週一到週六對應數字1-7只需要將查詢出來的數據進行判斷就行了,如下:

IF(pmod(datediff(#date#, '1920-01-01') - 3, 7)='0', 7,pmod(datediff(#date#, '1920-01-01') - 3, 7))

hive (itcast)> select IF(pmod(datediff('2020-04-26 21:08:12', '1920-01-01') - 3, 7)='0', 7,pmod(datediff('2020-04-27 21:08:12', '1920-01-01') - 3, 7));
OK
_c0
7
Time taken: 0.05 seconds, Fetched: 1 row(s)
hive (itcast)> 

 

四、hive獲取年月日時分秒周、季度

1、hive獲取年月日時分秒

hive (itcast)> select year('2020-04-26 21:08:12');
OK
_c0
2020
Time taken: 0.064 seconds, Fetched: 1 row(s)
hive (itcast)> select month('2020-04-26 21:08:12');
OK
_c0
4
Time taken: 0.051 seconds, Fetched: 1 row(s)
hive (itcast)> select day('2020-04-26 21:08:12');
OK
_c0
26
Time taken: 0.047 seconds, Fetched: 1 row(s)
hive (itcast)> select hour('2020-04-26 21:08:12');
OK
_c0
21
Time taken: 0.051 seconds, Fetched: 1 row(s)
hive (itcast)> select minute('2020-04-26 21:08:12');
OK
_c0
8
Time taken: 0.054 seconds, Fetched: 1 row(s)
hive (itcast)> select second('2020-04-26 21:08:12');
OK
_c0
12
Time taken: 0.049 seconds, Fetched: 1 row(s)
hive (itcast)> 

2、hive獲取周(年中第幾周)

> select weekofyear('2020-01-01 21:08:12');
OK
_c0
1
Time taken: 0.046 seconds, Fetched: 1 row(s)
hive (itcast)> select weekofyear('2020-01-02 21:08:12');
OK
_c0
1
Time taken: 0.039 seconds, Fetched: 1 row(s)
hive (itcast)> select weekofyear('2020-01-03 21:08:12');
OK
_c0
1
Time taken: 0.03 seconds, Fetched: 1 row(s)
hive (itcast)> select weekofyear('2020-01-04 21:08:12');
OK
_c0
1
Time taken: 0.043 seconds, Fetched: 1 row(s)
hive (itcast)> select weekofyear('2020-01-05 21:08:12');
OK
_c0
1
Time taken: 0.034 seconds, Fetched: 1 row(s)
hive (itcast)> select weekofyear('2020-01-06 21:08:12');
OK
_c0
2
Time taken: 0.044 seconds, Fetched: 1 row(s)
hive (itcast)> select weekofyear('2020-01-07 21:08:12');
OK
_c0
2
Time taken: 0.036 seconds, Fetched: 1 row(s)
hive (itcast)> 

年中第幾周:weekofyear

月中第幾天:dayofmonth

週中第幾天dayofweek

3、hive獲取季度

方式一:

語法:

select case 
when month(#data#)=1 then 'quarter1'
when month(#data#)=2 then 'quarter1'
when month(#data#)=3 then 'quarter1'
when month(#data#)=4 then 'quarter2'
when month(#data#)=5 then 'quarter2'
when month(#data#)=6 then 'quarter2'
when month(#data#)=7 then 'quarter3'
when month(#data#)=8 then 'quarter3'
when month(#data#)=9 then 'quarter3'
else 'quarter4' end;

例如:2020-04-28是哪個季度

select case 
when month('2020-04-28 12:24:50')=1 then 'quarter1'
when month('2020-04-28 12:24:50')=2 then 'quarter1'
when month('2020-04-28 12:24:50')=3 then 'quarter1'
when month('2020-04-28 12:24:50')=4 then 'quarter2'
when month('2020-04-28 12:24:50')=5 then 'quarter2'
when month('2020-04-28 12:24:50')=6 then 'quarter2'
when month('2020-04-28 12:24:50')=7 then 'quarter3'
when month('2020-04-28 12:24:50')=8 then 'quarter3'
when month('2020-04-28 12:24:50')=9 then 'quarter3'
else 'quarter4' end;

方式二:

 語法:

select month(#data#)/3.1+1;

例如:2020-04-28是哪個季度

select month('2020-04-28')/3.1+1;

 

五、Hive 時間戳和日期相互轉換

1、時間戳和日期相互轉換

//時間戳轉成日期
select distinct from_unixtime(1441565203,'yyyy-MM-dd HH:mm:ss');

//日期轉成時間戳
select distinct unix_timestamp('2020-04-28 12:10:10'); // 默認格式爲“yyyy-MM-dd HH:mm:ss“
select distinct unix_timestamp('2020-04-28 12:10:10','yyyy-MM-dd HH:mm:ss');

2、yyyymmdd和yyyy-mm-dd日期之間的切換

方法1: from_unixtime+ unix_timestamp

20200428轉成2020-04-28
select from_unixtime(unix_timestamp('20200428','yyyymmdd'),'yyyy-mm-dd');

2020-04-28轉成20200428
select from_unixtime(unix_timestamp('2020-04-28','yyyy-mm-dd'),'yyyymmdd');

方法2: substr + concat

20200428轉成2020-04-28
select concat(substr('20200428',1,4),'-',substr('20200428',5,2),'-',substr('20200428',7,2));

2020-04-28轉成20200428
select concat(substr('2020-04-28',1,4),substr('2020-04-28',6,2),substr('2020-04-28',9,2));

注:

  1. 值得注意的是,時間戳有可能是毫秒級的,然後這時候直接使用from_unixtime(1441565203,‘yyyy/MM/dd HH:mm:ss’)的話就會得到很奇怪的日期了,這時候要這樣from_unixtime(cast(151331629920/1000 as int)),同樣的,時間轉成毫秒級的時間戳也要乘以1000,如:unix_timestamp(‘2018-12-18 00:38:50’)*1000
  2. 如何區分時間戳是秒級還是毫秒級呢?一般來說,常見的時間戳是10位數的,13位數的時間戳就是毫秒級的

更多的函數請看這裏:hive函數大全

 

 

謝謝你長得這麼好看還給我點贊

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章