Hive常用函数大全-字符串函数
阅读原文时间:2023年07月09日阅读:1

1、字符串长度函数:length(X)(返回字符串X的长度)
select length('qwerty') from table --6

2、字符串反转函数:reverse(X)(返回字符串X反转的结果)
select reverse('qwerty') from table --ytrewq

3、字符串连接函数:concat(X,Y,…)(返回输入字符串连接后的结果,支持任意个输入字符串连接)
select concat('abc','qwe','rty') from table --abcqwerty

4、带分隔符字符串连接函数:concat_ws(X,y,Z)(返回输入字符串连接后的结果,X表示各个字符串间的分隔符)
select concat_ws('/','abc','qwe','rty') from table --abc/qwe/rty

5、字符串截取函数:substr(X,Y,Z)/substring(X,Y,Z)(返回字符串X从Y位置开始,截取长度为Z的字符串)
select substr('qwerty',1,3) from table --qwe

6、字符串转大写函数:upper(X)/ucase(X)(返回字符串X的大写格式)
select upper('qwERt') from table --QWERT
select ucase('qwERt') from table --QWERT

7、字符串转小写函数:lower(X)/lcase(X)(返回字符串X的小写格式)
select lower('qwERt') from table --qwert
select lcase('qwERt') from table --qwert

8、去空格函数:trim(X)(去除X字符串两边的空格)
select trim(' qwe rty uiop ') from table --'qwe rty uiop'
左边去空格函数:ltrim(X)(去除X字符串左边的空格)
select ltrim(' qwe rty uiop ') from table --'qwe rty uiop '
右边去空格函数:rtrim(X)(去除X字符串右边的空格)
select rtrim(' qwe rty uiop ') from table --' qwe rty uiop'

9、正则表达式替换函数:regexp_replace(X,Y,Z)(将字符串X中的符合java正则表达式Y的部分替换为Z:将X中与Y相同的字符串用Z替换)
select regexp_replace('foobar', 'o|ar', '234') from table --f234234b234

10、正则表达式解析函数:regexp_extract(X,Y,Z)(将字符串X按照Y正则表达式的规则拆分,返回Z指定的字符)
select regexp_extract('foothebar', 'foo(.*?)bar', 0) from table --foothebar
select regexp_extract('foothebar', 'foo(.*?)bar', 1) from table --the
select regexp_extract('foothebar', 'foo(.*?)bar', 2) from table --bar

11、URL解析函数:parse_url(X,Y,Z)(返回URL中指定的部分。X的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, and USERINFO)
select parse_url('https://www.iteblog.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') from table --www.iteblog.com
select parse_url('https://www.iteblog.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY','k1') from table --v1

12、json解析函数:get_json_object(X,Y)(解析json的字符串X,返回Y指定的内容)
例:X='[{"height":540,"size":79466,"width":960,"card_info":{"title":"超火热门小说","mp_tag_type":0,"head_desc_type":2,"pure_color_rgb":"#643C15","font_color":"#FFFFFF"}}]'
select get_json_object(substr(X,2,length(crt_info)-2), '$.card_info.pure_color_rgb') as color_rgb from table --#643C15
select get_json_object(substr(X,2,length(crt_info)-2), '$.size') as size_ from table --79466

13、空格字符串函数:space([int] X)(返回长度为X的字符串)
select space(10) from table --空
select length(space(10)) from table --10

14、重复字符串函数:repeat(X,Y)(返回重复Y次后的X字符串)
select repeat('abc',3) from table --abcabcabc

15、首字符ascii函数:ascii(X)(返回字符串X首字符的ascii码)
select ascii('abcde') from table --97

16、左补足函数:lpad(X,Y,Z)(将字符串X左补足到Y位:将字符串Z填充在字符串X的左边,直至字符串长度为Y)
备注:左补足时,补足的字符串Z要倒序补足
select lpad('abc',6,'wq') from table --wqwabc
右补足函数:rpad(X,Y,Z)(将字符串X右补足到Y位:将字符串Z填充在字符串X的右边,直至字符串长度为Y)
select rpad('abc',6,'wq') from table --abcwqw

17、分割字符串函数: split(X,Y)(将字符串X按照Y字符串进行分割,返回分割后的字符串数组)
select split('abcabcabc','b') from table --["a","ca","ca","c"]

18、集合查找函数: find_in_set(X,Y)(返回X在Y第一次出现的位置,Y是用逗号分割的字符串。如果没有找该X字符,则返回0)
select find_in_set('as','ad,af,as,ag') from table --3
select find_in_set('ak','ad,af,as,ag') from table --0

19、str_to_map()
例:str='aid=>31201343148,amsfirstcategoryid=>214748336535,amsfirstcategoryname=>服饰鞋帽箱包,city_level=>1,corporation=>杭州女王轻奢网络科技有限公司,crt_size=>666,end_time=>20210316,name=>新款棉麻专物,secondcategoryname=>服饰鞋包综合,snsid=>135498092586548652538,source=>1,spid=>spid37a67b6f563,spname=>天津太古时代网络技术有限公司'
select string_to_map(str ,",","=>")["aid"] as aid from table --aid