1. 程式人生 > >Hive常用字串函式

Hive常用字串函式

Hive內部提供了很多操作字串的相關函式,本文將對其中部分常用的函式進行介紹。

下表為Hive內建的字串函式,具體的用法可以參見本文的下半部分。

返回型別 函式名 描述
int ascii(string str) 返回str第一個字串的數值
string base64(binary bin) 將二進位制引數轉換為base64字串
string concat(string|binary A, string|binary B...) 返回將A和B按順序連線在一起的字串,如:concat('foo', 'bar') 返回'foobar'
array<struct<string,double>> context_ngrams(array<array<string>>, array<string>, int K, int pf) 從一組標記化的句子中返回前k個文字
string concat_ws(string SEP, string A, string B...) 類似concat() ,但使用自定義的分隔符SEP
string concat_ws(string SEP, array<string>) 類似concat_ws() ,但引數為字串陣列
string decode(binary bin, string charset) 使用指定的字符集將第一個引數解碼為字串,如果任何一個引數為null,返回null。可選字符集為: 'US_ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16BE', 'UTF-16LE', 'UTF-16'
binary encode(string src, string charset) 使用指定的字符集將第一個引數編碼為binary ,如果任一引數為null,返回null
int find_in_set(string str, string strList) 返回str在strList中第一次出現的位置,strList為用逗號分隔的字串,如果str包含逗號則返回0,若任何引數為null,返回null。如: find_in_set('ab', 'abc,b,ab,c,def') 返回3
string format_number(number x, int d) 將數字x格式化為'#,###,###.##',四捨五入為d位小數位,將結果做為字串返回。如果d=0,結果不包含小數點或小數部分
string get_json_object(string json_string, string path) 從基於json path的json字串中提取json物件,返回json物件的json字串,如果輸入的json字串無效返回null。Json 路徑只能有數字、字母和下劃線,不允許大寫和其它特殊字元
boolean in_file(string str, string filename) 如果str在filename中以正行的方式出現,返回true
int instr(string str, string substr) 返回substr在str中第一次出現的位置。若任何引數為null返回null,若substr不在str中返回0。Str中第一個字元的位置為1
int length(string A) 返回A的長度
int locate(string substr, string str[, int pos]) 返回substr在str的位置pos後第一次出現的位置
string lower(string A) lcase(string A) 返回字串的小寫形式
string lpad(string str, int len, string pad) 將str左側用字串pad填充,長度為len
string ltrim(string A) 去掉字串A左側的空格,如:ltrim(' foobar ')的結果為'foobar '
array<struct<string,double>> ngrams(array<array<string>>, int N, int K, int pf) 從一組標記化的Returns the top-k 句子中返回前K個N-grams
string parse_url(string urlString, string partToExtract [, string keyToExtract]) 返回給定URL的指定部分,partToExtract的有效值包括HOST,PATH, QUERY, REF, PROTOCOL, AUTHORITY,FILE和USERINFO。例如:  parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') 返回 'facebook.com'.。當第二個引數為QUERY時,可以使用第三個引數提取特定引數的值,例如: parse_url('http://facebook.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1') 返回'v1'
string printf(String format, Obj... args) 將輸入引數進行格式化輸出
string regexp_extract(string subject, string pattern, int index) 使用pattern從給定字串中提取字串。如: regexp_extract('foothebar', 'foo(.*?)(bar)', 2) 返回'bar' 有時需要使用預定義的字元類:使用'\s' 做為第二個引數將匹配s,'s'匹配空格等。引數index是Java正則匹配器方法group()方法中的索引
string regexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT) 使用REPLACEMENT替換字串INITIAL_STRING中匹配PATTERN的子串,例如: regexp_replace("foobar", "oo|ar", "") 返回'fb'
string repeat(string str, int n) 將str重複n次
string reverse(string A) 將字串A翻轉
string rpad(string str, int len, string pad) 在str的右側使用pad填充至長度len
string rtrim(string A) 去掉字串A右側的空格,如: rtrim(' foobar ') 返回 ' foobar'
array<array<string>> sentences(string str, string lang, string locale) 將自然語言文字處理為單詞和句子,每個句子在適當的邊界分割,返回單詞的陣列。引數lang和local為可選引數,例如: sentences('Hello there! How are you?') 返回( ("Hello", "there"), ("How", "are", "you") )
string space(int n) 返回n個空格的字串
array split(string str, string pat) 用pat分割字串str,pat為正則表示式
map<string,string> str_to_map(text[, delimiter1, delimiter2]) 使用兩個分隔符將文字分割為鍵值對。第一個分隔符將文字分割為K-V 對,第二個分隔符分隔每個K-V 對。預設第一個分隔符為““,第二個分隔符為=
string substr(string|binary A, int start) substring(string|binary A, int start) 返回A從位置start直到結尾的子串
string substr(string|binary A, int start, int len) substring(string|binary A, int start, int len) 返回A中從位置start開始,長度為len的子串,如: substr('foobar', 4, 1) 返回 'b'
string translate(string input, string from, string to) 將input中出現在from中的字元替換為to中的字串,如果任何引數為null,結果為null
string trim(string A) 去掉字串A兩端的空格
binary unbase64(string str) 將base64字串轉換為二進位制
string upper(string A) ucase(string A) 返回字串A的大寫形式

1.、字串長度計算函式:length

語法: length(string A), 返回值: int 說明:返回字串A的長度 例子:

hive> select length('iteblog') from iteblog;

7

2.、字串反轉函式:reverse

語法: reverse(string A) 返回值: string 說明:返回字串A的反轉結果 例子:

hive> select reverse(iteblog') from iteblog;

golbeti

3.、字串連線函式:concat

語法: concat(string A, string B…) 返回值: string 說明:返回輸入字串連線後的結果,支援任意個輸入字串 例子:

hive> select concat('www','.iteblog','.com') from iteblog;

www.iteblog.com

4、帶分隔符字串連線函式:concat_ws

語法: concat_ws(string SEP, string A, string B…) 返回值: string 說明:返回輸入字串連線後的結果,SEP表示各個字串間的分隔符 例子:

hive> select concat_ws('.','www','iteblog','com') from iteblog;

www.iteblog.com

5、字串擷取函式:substr,substring

語法: substr(string A, int start),substring(string A, int start) 返回值: string 說明:返回字串A從start位置到結尾的字串 例子:

hive> select substr('iteblog',3) from iteblog;

eblog

hive>  selectsubstr('iteblog',-1) from iteblog;

g

6、 字串擷取函式:substr,substring

語法: substr(string A, int start, int len),substring(string A, intstart, int len) 返回值: string 說明:返回字串A從start位置開始,長度為len的字串 例子:

hive> select substr('abcde',3,2) from iteblog;

cd

hive> select substring('abcde',3,2) from iteblog;

cd

hive>select substring('abcde',-2,2) from iteblog;

de

7、字串轉大寫函式:upper,ucase

語法: upper(string A) ucase(string A) 返回值: string 說明:返回字串A的大寫格式 例子:

hive> select upper('abSEd') from iteblog;

ABSED

hive> select ucase('abSEd') from iteblog;

ABSED

8、字串轉小寫函式:lower,lcase

語法: lower(string A) lcase(string A) 返回值: string 說明:返回字串A的小寫格式 例子:

hive> select lower('abSEd') from iteblog;

absed

hive> select lcase('abSEd') from iteblog;

absed

9、去空格函式:trim

語法: trim(string A) 返回值: string 說明:去除字串兩邊的空格 例子:

hive> select trim(' abc ') from iteblog;

abc

10、左邊去空格函式:ltrim

語法: ltrim(string A) 返回值: string 說明:去除字串左邊的空格 例子:

hive> select ltrim(' abc ') from iteblog;

abc

11、右邊去空格函式:rtrim

語法: rtrim(string A) 返回值: string 說明:去除字串右邊的空格 例子:

hive> select rtrim(' abc ') from iteblog;

abc

12、正則表示式替換函式:regexp_replace

語法: regexp_replace(string A, string B, string C) 返回值: string 說明:將字串A中的符合java正則表示式B的部分替換為C。注意,在有些情況下要使用轉義字元,類似oracle中的regexp_replace函式。 例子:

hive> select regexp_replace('foobar', 'oo|ar', '') from iteblog;

fb

13、正則表示式解析函式:regexp_extract

語法: regexp_extract(string subject, string pattern, int index) 返回值: string 說明:將字串subject按照pattern正則表示式的規則拆分,返回index指定的字元。 例子:

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) fromiteblog;

the

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) fromiteblog;

bar

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) fromiteblog;

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. 例子:

iteblog.com

> 'QUERY','weixin') from iteblog;

iteblog_hadoop

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":"[email protected]_for_json_udf_test.net",

>  "owner":"amy"

> }

> ','$.owner') from iteblog;

amy

16、空格字串函式:space

語法: space(int n) 返回值: string 說明:返回長度為n的字串 例子:

hive> select space(10) from iteblog;

hive> select length(space(10)) from iteblog;

10

17、重複字串函式:repeat

語法: repeat(string str, int n) 返回值: string 說明:返回重複n次後的str字串 例子:

hive> select repeat('abc',5) from iteblog;

abcabcabcabcabc

18、首字元ascii函式:ascii

語法: ascii(string str) 返回值: int 說明:返回字串str第一個字元的ascii碼 例子:

hive> select ascii('abcde') from iteblog;

97

19、左補足函式:lpad

語法: lpad(string str, int len, string pad) 返回值: string 說明:將str進行用pad進行左補足到len位 例子:

hive> select lpad('abc',10,'td') from iteblog;

tdtdtdtabc

注意:與GP,ORACLE不同,pad 不能預設

20、右補足函式:rpad

語法: rpad(string str, int len, string pad) 返回值: string 說明:將str進行用pad進行右補足到len位 例子:

hive> select rpad('abc',10,'td') from iteblog;

abctdtdtdt

21、分割字串函式: split

語法: split(string str, stringpat) 返回值: array 說明: 按照pat字串分割str,會返回分割後的字串陣列 例子:

hive> select split('abtcdtef','t') from iteblog;

["ab","cd","ef"]

22、集合查詢函式:find_in_set

語法: find_in_set(string str, string strList) 返回值: int 說明: 返回str在strlist第一次出現的位置,strlist是用逗號分割的字串。如果沒有找該str字元,則返回0 例子:

hive> select find_in_set('ab','ef,ab,de') from iteblog;

2

hive> select find_in_set('at','ef,ab,de') from iteblog;

0