首頁 > 易卦

hive字串處理方法

作者:由 mitsuhide1992 發表于 易卦日期:2022-08-19

如何獲取函式下標

字串分割

substring_index

substring_index(str, “-”, 1)

同mysql用法

split

字串分割函式,和python差不多,但是陣列下標不支援負數。

例子:

split(‘a,b,c,d’,‘,’)得到的結果:[“a”,“b”,“c”,“d”]split(‘a,b,c,d’,‘,’)[0]得到的結果:a

split特殊字元

特殊分割符號

regex 為字串匹配的引數,所以遇到特殊字元的時候需要做特殊的處理

例3: “。” 點

split(‘192。168。0。1’,‘。’)

得到的結果:

[]

正確的寫法:

split(‘192。168。0。1’,‘\\。’)

得到的結果:

[“192”,“168”,“0”,“1”]

需要注意的是:

當然當split包含在 “” 之中時 需要加4個\

hive -e “。。。。 split(‘192。168。0。1’,‘\\\\。’) 。。。 ”

不然得到的值是null

字串擷取

substr

substr(string A, int start):擷取到結尾。

substr(string A, int start, int len):擷取到指定長度。

hive> select substr(‘abcde’,3) from dual; cde hive> select substr(‘abcde’,-1) from dual; e hive> select substr(‘abcde’,3,2) from dual; cd

substring

substring(string A, int start)

substr(string A, int start, int len),substring(string A, int start, int len)

hive> select substring(‘abcde’,3) from dual; cde hive> select substring(‘abcde’,3,2) from dual; cd hive> select substring(‘abcde’,-2,2) from dual; de

字串長度

length

length(string A) hive> select length(‘abcedfg’) from dual; 7

字串反轉

reverse

reverse(string A) hive> select reverse(‘abcedfg’) from dual; gfdecba

字串連線

concat

concat(string A, string B…) hive> select concat(‘abc’,‘def’,’gh’) from dual; abcdefgh

concat_ws

帶分隔符字串連線:

說明:返回輸入字串連線後的結果,SEP表示各個字串間的分隔符。

concat_ws(string SEP, string A, string B…) hive> select concat_ws(‘,’,‘abc’,’def’,‘gh’) from dual; abc,def,gh

字串轉大小寫

upper

hive> select upper(‘abSEd’) from dual; ABSED

ucase

hive> select ucase(‘abSEd’) from dual; ABSED

lower

hive> select lower(‘abSEd’) from dual; absed

lcase

hive> select lcase(‘abSEd’) from dual; absed

去空格

trim

去除字串兩邊的空格

hive> select trim(‘ abc ‘) from dual; abc

ltrim

左邊去空格函式

hive> select ltrim(‘ abc ‘) from dual; abc

rtrim

右邊去空格函式

hive> select rtrim(‘ abc ‘) from dual; abc

重複字串

repeat

返回重複n次後的str字串

repeat(string str, int n)hive> select repeat(‘abc’,5) from dual; abcabcabcabcabc

補足

lpad

左補足,將str進行用pad進行左補足到len位

lpad(string str, int len, string pad)hive> select lpad(‘abc’,10,’td’) from dual; tdtdtdtabc

rpad

右補足:將str進行用pad進行右補足到len位

rpad(string str, int len, string pad)hive> select rpad(‘abc’,10,’td’) from dual; abctdtdtdt

集合查詢

find_in_set

返回str在strlist第一次出現的位置,strlist是用逗號分割的字串。如果沒有找該str字元,則返回0

find_in_set(string str, string strList)hive> select find_in_set(‘ab’,’ef,ab,de’) from dual; 2 hive> select find_in_set(‘at’,‘ef,ab,de’) from dual; 0