一、關系函數
1.等值比較:=
語法:A=B
操作類型:全部基本類型
2.不等值比較:<>
語法:A<>B
操作類型:全部基本類型
3.小於比較:<
語法:A<B
操作類型:全部基本類型
4.空值推斷:IS NULL
語法:a is null
操作類型:全部類型
5.非空推斷:IS NOT NULL
語法:a is not null
操作類型:全部基本類型
6.LIKE比較:LIKE
語法:A LIKE B
操作類型:strings
7.JAVA的LIKE操作:RLIKE
語法:A RLIKE B
操作類型:strings
描寫敘述:假設字符串A或者字符串B為NULL。則返回NULL;假設字符串符合
JAVA正則表達式B的正則語法,則為TRUE,否則為FALSE。
8.REGEXP操作:REGEXP
語法:A REGEXP B
操作類型:strings
描寫敘述:功能與RLIKE同樣
二、數學運算
1.加法操作:+
語法:A+B
操作類型:全部數值類型
2.減法操作:-
語法:A-B
操作類型:全部數值類型
3.取余操作:%
語法:A%B
操作類型:全部數值類型
4.位與操作:&
語法:A&B
操作類型:全部數值類型
5.位或操作:|
語法:A|B
操作類型:全部數值類型
6.位異或操作:^
語法:A^B
操作類型:全部數值類型
說明:返回A和B按位進行異或操作的結果。結果的數值類型等於A的類型
和B的類型的最小父類型。
舉例:
hive>select 4^8 from sun_test;
12
hive>select 6^4 from sun_test;
2
7.位取反操作:~
語法:~A
操作類型:全部數值類型
三、邏輯運算
1.邏輯與操作:AND
語法:A AND B
操作類型:boolean
2.邏輯或操作:OR
語法:A OR B
操作類型:boolean
3.邏輯非操作:NOT
語法:NOT A
操作類型:boolean
四、數值計算
1.取整函數:round
語法:round(double a)
返回值:BIGINT
說明:返回double類型的整數值部分(遵循四舍五入)
舉例:
hive>select round(3.14) from sun_test;
3
hive>select round(3.23456,3) from sun_test;
3.234
2.向下取整函數:floor
說明:返回等於或者小於該double變量的最大的整數
hive>select floor(3.56) from sun_test;
3
3.向上取整函數:cel=il/ceiling
說明:返回等於或者大於該double變量的最小的整數
hive>select ceil(3.123) from sun_test;
4
4.取隨機數函數:rand
說明:返回一個0到1范圍內的隨機數。
hive>select rand() from sun_test;
0.43253251532532
5.自然指數函數exp
說明:返回自然對數e的a次方
hive>select exp(2) from sun_test;
7.38905609893065
6.以10為底對數函數:log10
語法:log10(100)=2.0
7.冪運算函數:pow/power
語法:pow(2,3)=8
8.開平方函數:sqrt
語法:sqrt(16)=4
9.十六進制函數:hex
語法:hex(BIGINT a)
說明:假設變量時int類型。那么返回a的16進制表示;假設變量時string類型,
則返回該字符串的10進制表示
hive>select hex(17) from sun_test;
11
hive>select hex('abc') from sun_test;
616263
10.進制轉換函數:conv
conv(17,10,16)=11
conv(17,10,2)=10001
11.絕對值函數:abs
conv(-3.3)=3.3
五、字符串函數
1.字符串長度函數:length
length('acbfa')=5
2.字符串反轉函數:reverse
reverse('abcde')=edcba
3.字符串鏈接函數:concat
concat('aa','bb','cc')=aabbcc
4.帶分隔符字符串連接函數:concat_ws
concat_ws(',','aaa','bbb','ccc')=aaa,bbb,ccc
5.字符串截取函數:substr,substring
substr('abcde',3)=cde
substr('abcde',-1)=e
substr('abcde',-2,2)=de
substr('abcde',3,2)=cd
6.字符串轉大寫函數:upper,ucase
7.字符串轉小寫函數:lower,lcase
8.去空格函數:trim()、ltrim()、rtrim()
9.正則表達式替換函數:regexp_replace
regexp_replace('foobar','oo|ar','')=fb
10.正則表達式解析函數:regexp_extract
語法: regexp_extract(string subject, string pattern, int index)
返回值: string
說明:將字符串subject依照pattern正則表達式的規則拆分,返回index指定的字符。
舉例:
hive> select regexp_extract('foothebar', 'foo(.*?
)(bar)', 1) fromlxw_dual;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) fromlxw_dual;
bar
hive> select regexp_extract('foothebar', 'foo(.*?
)(bar)', 0) fromlxw_dual;
foothebar
注意。在有些情況下要使用轉義字符,以下的等號要用雙豎線轉義,這是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;
14. URL解析函數:parse_url
語法: parse_url(string urlString, string partToExtract [, stringkeyToExtract])
返回值: string
說明:返回URL中指定的部分。
partToExtract的有效值為:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO.
舉例:
hive> selectparse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') fromlxw_dual;
facebook.com
hive> selectparse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY','k1') from lxw_dual;
v1
15. 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 lxw_dual;
amy
六、其他
1.切割字符串函數:split
2.個數統計函數:count
3.總和統計函數:sum
4.平均值統計函數:avg
5.最小值統計函數:min
6.最大值統計函數:max
7.類型轉換函數:cast
版權聲明:本文博主原創文章,博客,未經同意不得轉載。