hive内置函数大全

it2025-07-12  2

====================================== 一、关系函数 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

版权声明:本文博主原创文章,博客,未经同意不得转载。

转载于:https://www.cnblogs.com/bhlsheji/p/4914962.html

相关资源:hive函数参考手册,包含常用的hive内置函数.doc
最新回复(0)