Hive常⽤函数⼤全-字符串函数
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
tabletotal函数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('www.iteblog/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') from table--www.iteblog
select parse_url('www.iteblog/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=>新款棉⿇专物,secondcategorynam select string_to_map(str ,",","=>")["aid"]as aid from table--aid
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论