Hello 大家好,今天為大家分享下Hive的常用函數,不廢話我們直接進入主題;
Hive的常用函數主要包含以下幾大類:
- 關係運算
- 數學運算
- 邏輯運算
- 數值運算
- 日期運算
- 條件函數
- 字符串函數
- 集合統計函數
- 複合類型構建操作
- 複雜類型訪問操作
- 複雜類型長度統計函數
這裡面我們不會全部為大家分享的,主要分享一些經常用的,更詳細的大家可以參考如下鏈接:
#本文來源下鏈接
https://www.iteblog.com/archives/2258.html#1UNIX_from_unixtime
關係運算/數學運算/邏輯運算如下(操作及其簡單):
數值運算如下:
集合統計函數如下:
複合類型如下:
在這裡面我們重點為大家分享一下日期函數,條件函數和字符串函數:
字符串函數
like比較:like
語法: A LIKE B
操作類型: strings
描述: 如果字符串A或者字符串B為NULL,則返回NULL;如果字符串A符合表達式B 的正則語法,則為TRUE;否則為FALSE。B中字符”_”表示任意單個字符,而字符”%”表示任意數量的字符。
hive> select 1 from iteblog where 'football' like 'foot%';
1
hive> select 1 from iteblog where 'football' like 'foot____';
1
注意:否定比較時候用NOT A LIKE B
hive> select 1 from iteblog where NOT 'football' like 'fff%';
1
字符串長度函數:length
語法: length(string A)
返回值: int
說明:返回字符串A的長度
hive> select length('abcedfg') from iteblog;
7
字符串反轉函數:reverse
語法: reverse(string A)
返回值: string
說明:返回字符串A的反轉結果
hive> select reverse(abcedfg’) from iteblog;
gfdecba
字符串連接函數:concat
語法: concat(string A, string B…)
返回值: string
說明:返回輸入字符串連接後的結果,支持任意個輸入字符串
hive> select concat(‘abc’,'def’,'gh’) from iteblog;
abcdefgh
帶分隔符字符串連接函數:concat_ws
語法: concat_ws(string SEP, string A, string B…)
返回值: string
說明:返回輸入字符串連接後的結果,SEP表示各個字符串間的分隔符
hive> select concat_ws(',','abc','def','gh') from iteblog;
abc,def,gh
字符串截取函數:substr,substring
語法: substr(string A, int start),substring(string A, int start)
返回值: string
說明:返回字符串A從start位置到結尾的字符串
hive> select substr('abcde',3) from iteblog;
cde
hive> select substring('abcde',3) from iteblog;
cde
hive> select substr('abcde',-1) from iteblog; (和ORACLE相同)
e
語法: substr(string A, int start, int len),substring(string A, int start, int len)
返回值: string
說明:返回字符串A從start位置開始,長度為len的字符串
hive> select substr('abcde',3,2) from iteblog;
cd
hive> select substring('abcde',3,2) from iteblog;
cd
hive>select substring('abcde',-2,2) from iteblog;
de
字符串轉大寫函數:upper,ucase
語法: upper(string A) ucase(string A)
返回值: string
說明:返回字符串A的大寫格式
hive> select upper('abSEd') from iteblog;
ABSED
hive> select ucase('abSEd') from iteblog;
ABSED
字符串轉小寫函數:lower,lcase
語法: lower(string A) lcase(string A)
返回值: string
說明:返回字符串A的小寫格式
hive> select lower('abSEd') from iteblog;
absed
hive> select lcase('abSEd') from iteblog;
absed
去空格函數:trim
語法: trim(string A)
返回值: string
說明:去除字符串兩邊的空格
hive> select trim(' abc ') from iteblog;
abc
左邊去空格函數:ltrim
語法: ltrim(string A)
返回值: string
說明:去除字符串左邊的空格
hive> select ltrim(' abc ') from iteblog;
abc
右邊去空格函數:rtrim
語法: rtrim(string A)
返回值: string
說明:去除字符串右邊的空格
hive> select rtrim(' abc ') from iteblog;
abc
正則表達式替換函數:regexp_replace
語法: regexp_replace(string A, string B, string C)
返回值: string
說明:將字符串A中的符合java正則表達式B的部分替換為C。注意,在有些情況下要使用轉義字符,類似oracle中的regexp_replace函數。
hive> select regexp_replace('foobar', 'oo|ar', '') from iteblog;
fb
正則表達式解析函數:regexp_extract
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字符串subject按照pattern正則表達式的規則拆分,返回index指定的字符。
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) from iteblog;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) from iteblog;
bar
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) from iteblog;
foothebar
strong>注意,在有些情況下要使用轉義字符,下面的等號要用雙豎線轉義,這是java正則表達式的規則。
select data_field,
regexp_extract(data_field,'.*?bgStart\\=([^&]+)',1) as aaa,
regexp_extract(data_field,'.*?contentLoaded_headStart\\=([^&]+)',1) as bbb,
regexp_extract(data_field,'.*?AppLoad2Req\\=([^&]+)',1) as ccc
from pt_nginx_loginlog_st
where pt = '2012-03-26' limit 2;
URL解析函數:parse_url
語法: parse_url(string urlString, string partToExtract [, string keyToExtract])
返回值: string
說明:返回URL中指定的部分。partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
hive> select parse_url('https://www.iteblog.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') from iteblog;
facebook.com
hive> select parse_url('https://www.iteblog.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1') from iteblog;
v1
json解析函數:get_json_object
語法: get_json_object(string json_string, string path)
返回值: string
說明:解析json的字符串json_string,返回path指定的內容。如果輸入的json字符串無效,那麼返回NULL。
hive> select get_json_object('{"store":
> {"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
> "bicycle":{"price":19.95,"color":"red"}
> },
> "email":"amy@only_for_json_udf_test.net",
> "owner":"amy"
> }
> ','$.owner') from iteblog;
amy
空格字符串函數:space
語法: space(int n)
返回值: string
說明:返回長度為n的字符串
hive> select space(10) from iteblog;
hive> select length(space(10)) from iteblog;
10
重複字符串函數:repeat
語法: repeat(string str, int n)
返回值: string
說明:返回重複n次後的str字符串
hive> select repeat('abc',5) from iteblog;
abcabcabcabcabc
首字符ascii函數:ascii
語法: ascii(string str)
返回值: int
說明:返回字符串str第一個字符的ascii碼
hive> select ascii('abcde') from iteblog;
97
左補足函數:lpad
右補足函數:rpad
語法: lpad(string str, int len, string pad)
語法: rpad(string str, int len, string pad)
返回值: string
說明:將str進行用pad進行左/右補足到len位
hive> select rpad('abc',10,'td') from iteblog;
abctdtdtdt
分割字符串函數: split
語法: split(string str, string pat)
返回值: array
說明: 按照pat字符串分割str,會返回分割後的字符串數組
hive> select split('abtcdtef','t') from iteblog;
["ab","cd","ef"]
集合查找函數: find_in_set
語法: find_in_set(string str, string strList)
返回值: int
說明: 返回str在strlist第一次出現的位置,strlist是用逗號分割的字符串。如果沒有找該str字符,則返回0
hive> select find_in_set('ab','ef,ab,de') from iteblog;
2
hive> select find_in_set('at','ef,ab,de') from iteblog;
0
條件函數
If函數: if
語法: if(boolean testCondition, T valueTrue, T valueFalseOrNull)
返回值: T
說明: 當條件testCondition為TRUE時,返回valueTrue;否則返回valueFalseOrNull
hive> select if(1=2,100,200) from iteblog;
200
hive> select if(1=1,100,200) from iteblog;
100
非空查找函數: COALESCE
語法: COALESCE(T v1, T v2, …)
返回值: T
說明: 返回參數中的第一個非空值;如果所有值都為NULL,那麼返回NULL
hive> select COALESCE(null,'100','50′) from iteblog;
100
條件判斷函數:CASE
語法: CASE a WHEN b THEN c [WHEN d THEN e]* [ELSE f] END
返回值: T
說明:如果a等於b,那麼返回c;如果a等於d,那麼返回e;否則返回f
hive> Select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from iteblog;
mary
hive> Select case 200 when 50 then 'tom' when 100 then 'mary' else 'tim' end from iteblog;
tim
條件判斷函數:CASE
語法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END
返回值: T
說明:如果a為TRUE,則返回b;如果c為TRUE,則返回d;否則返回e
hive> select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end from iteblog;
mary
hive> select case when 1=1 then 'tom' when 2=2 then 'mary' else 'tim' end from iteblog;
tom
日期函數
UNIX時間戳轉日期函數: from_unixtime
語法: from_unixtime(bigint unixtime[, string format])
返回值: string
說明: 轉化UNIX時間戳(從1970-01-01 00:00:00 UTC到指定時間的秒數)到當前時區的時間格式
hive> select from_unixtime(1323308943,'yyyyMMdd') from iteblog;
20111208
獲取當前UNIX時間戳函數: unix_timestamp
語法: unix_timestamp()
返回值: bigint
說明: 獲得當前時區的UNIX時間戳
hive> select unix_timestamp() from iteblog;
1323309615
日期轉UNIX時間戳函數: unix_timestamp
語法: unix_timestamp(string date)
返回值: bigint
說明: 轉換格式為"yyyy-MM-dd HH:mm:ss"的日期到UNIX時間戳。如果轉化失敗,則返回0。
hive> select unix_timestamp('2011-12-07 13:01:03') from iteblog;
1323234063
指定格式日期轉UNIX時間戳函數: unix_timestamp
語法: unix_timestamp(string date, string pattern)
返回值: bigint
說明: 轉換pattern格式的日期到UNIX時間戳。如果轉化失敗,則返回0。
hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from iteblog;
1323234063
日期時間轉日期函數: to_date
語法: to_date(string timestamp)
返回值: string
說明: 返回日期時間字段中的日期部分。
hive> select to_date('2011-12-08 10:03:01') from iteblog;
2011-12-08
日期轉年函數: year
語法: year(string date)
返回值: int
說明: 返回日期中的年。
hive> select year('2011-12-08 10:03:01') from iteblog;
2011
hive> select year('2012-12-08') from iteblog;
2012
ok,此處年月日小時分鐘秒依舊類推即可~~~
寫到這呢基本就複製的插不多啦,為啥複製呢,因為人家比咋們整理的更加齊全,若有感興趣的同道中人推薦參考參考這篇文檔將案例自行推敲幾遍~~~
閱讀更多 北京圖靈學院 的文章