Hive函數大全(含例子)之字符串函數(String Functions)

字符串函數 String Functions

ascii(string str)

  • 返回結果: 返回字符串str首字母的十進制ascii碼
  • 返回類型: int
  • select ascii('ABC'); -- 結果爲 65

base64(binary bin)

  • 返回結果: 將二進制轉換爲base64編碼
  • 返回類型: string
  • select base64(encode('Uncle Bean', 'utf8')); -- 結果爲 VW5jbGUgQmVhbg==
  • select base64(encode('Melon-and-fruit-fields', 'utf-8')); -- 結果爲 TWVsb24tYW5kLWZydWl0LWZpZWxkcw====

character_length(string str)

  • character_length 可縮寫爲 char_length
  • 返回結果: 返回str中包含的UTF-8字符數
  • 返回類型: int
  • select character_length('123456'); -- 結果爲 6
  • select char_length('ABCDEFGHIJK'); -- 結果爲 11

chr(bigint|double A)

  • 返回結果: 將數字A轉爲對應的ascii字符, 如果A大於等於256,則結果同chr(A % 256)
  • 返回類型: string
  • select chr(65); -- 結果爲 A
  • select chr(65.6); -- 結果爲 A
  • select chr(321); -- 結果爲 A
  • select chr(321 % 256); -- 結果爲 A

concat(string|binary A, string|binary B...)

  • 返回結果: 拼接字符串,函數接受任意數量的輸入
  • 返回類型: string
  • select concat('A', 'C', 'B'); -- 結果爲 ACB
  • select concat(encode('A', 'utf8'), encode('C', 'utf8'), encode('B', 'utf8')); -- 結果爲 ACB

context_ngrams(array<array>, array, int K, int pf)

  • 返回結果: 使用n-gram模型,通過指定array,提取前K個上下文文本;pf越大,精度越高,同時消耗的內存資源也更大
  • 返回類型: array<struct<string,double>>
  • select context_ngrams(array(array('from','a'),array('from','a'),array('from','b')), array('from', null), 1); -- 結果爲 [{"ngram":["a"],"estfrequency":2.0}]
  • select context_ngrams(array(array('from','a'),array('from','a'),array('from','b')), array('from', null), 2); -- 結果爲 [{"ngram":["a"],"estfrequency":2.0},{"ngram":["b"],"estfrequency":1.0}]

concat_ws(string SEP, string A, string B...)

  • 返回結果: 使用指定分隔符 SEP 拼接字符串,傳入參數爲多個字符串
  • 返回類型: string
  • select concat_ws('-', 'Melon', 'and', 'fruit', 'fields'); -- 結果爲 Melon-and-fruit-fields

concat_ws(string SEP, array)

  • 返回結果: 使用指定分隔符 SEP 拼接字符串,傳入參數爲 array
  • 返回類型: string
  • select concat_ws('-', array('Melon', 'and', 'fruit', 'fields')); -- 結果爲 Melon-and-fruit-fields

decode(binary bin, string charset)

  • 返回結果: 解碼(字符集 charset 包括'US-ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16BE', 'UTF-16LE', 'UTF-16')
  • 返回類型: string
  • select decode(encode('A', 'utf8'), 'UTF-8'); -- 結果爲 A

elt(N int,str1 string,str2 string,str3 string,...)

  • 返回結果: 返回第N個傳入參數,如果N小於1或者大於字符串參數的個數則返回NULL
  • 返回類型: string
  • select elt(2, 'Melon', 'and', 'fruit', 'fields'); -- 結果爲 and
  • select elt(5, 'Melon', 'and', 'fruit', 'fields'); -- 結果爲 NULL

encode(string src, string charset)

  • 返回結果: 編碼
  • 返回類型: binary
  • select encode('A','UTF-8'); -- 結果爲 A
  • select encode('A','US-ASCII'); -- 結果爲 A

field(val T,val1 T,val2 T,val3 T,...)

  • 返回結果: 返回val在後續參數中出現的位置,如果val爲NULL或者未找到則返回0
  • 返回類型: int
  • select field(11, 13, 12, 11); -- 結果爲 3
  • select field('a', 'c', 'b', 'a'); -- 結果爲 3
  • select field('d', 'c', 'b', 'a'); -- 結果爲 0

find_in_set(string str, string strList)

  • 返回結果: 返回str在strList中出現的位置,未找到或者str中包含逗號則返回0(strList是一個用逗號隔開的字符串)
  • 返回類型: int
  • select find_in_set('and', 'Melon,and,fruit,fields'); -- 結果爲 2
  • select find_in_set('And', 'Melon,and,fruit,fields'); -- 結果爲 0
  • select find_in_set('and,', 'Melon,and,fruit,fields'); -- 結果爲 0
  • select find_in_set(NULL, 'Melon,and,fruit,fields'); -- 結果爲 NULL

format_number(number x, int d)

  • 返回結果: 格式化數字x爲包含d個小數位的數字,並用千分位表示
  • 返回類型: string
  • select format_number(1234.16, 1); -- 結果爲 1,234.2
  • select format_number(1234.14, 1); -- 結果爲 1,234.1

get_json_object(string json_string, string path)

  • 返回結果: 提取json對象值
  • 返回類型: string
  • select get_json_object('{"key":"value"}', '$.key'); -- 結果爲 value
  • select get_json_object('[{"key":"value"}, {"key":"value2"}]', '$[1].key'); -- 結果爲 value2
  • select get_json_object('["value3"]', '$[0]'); -- 結果爲 value3

in_file(string str, string filename)

  • 返回結果: 如果str是文件filename裏面的一行則返回true,否則返回false
  • 返回類型: boolean
  • select in_file('123', 'hdfs:/tmp/test/test.csv'); -- 結果爲 true
  • select in_file('12', 'hdfs:/tmp/test/test.csv'); -- 結果爲 false
  • /tmp/test/test.csv爲hdfs上的文件,有兩行數據,第一行爲123,第二行爲456

instr(string str, string substr)

  • 返回結果: 返回substr在str中第一次出現的位置,未出現則返回0(如果參數爲NULL則返回NULL;位置從1開始)
  • 返回類型: int
  • select instr('1234', '23'); -- 結果爲 2
  • select instr('1234', '2345'); -- 結果爲 0
  • select instr('1234', NULL); -- 結果爲 NULL
  • select instr(NULL, '23'); -- 結果爲 NULL

length(string A)

  • 返回結果: 返回字符串A的長度
  • 返回類型: int
  • select length('123'); -- 結果爲 3
  • select length(''); -- 結果爲 0
  • select length(NULL); -- 結果爲 NULL

locate(string substr, string str[, int pos])

  • 返回結果: 返回substr在str的pos-1位後第一次出現的位置
  • 返回類型: int
  • select locate('12', '1212'); -- 結果爲 1
  • select locate('12', '1212', 1); -- 結果爲 1
  • select locate('12', '1212', 2); -- 結果爲 3

lower(string A) lcase(string A)

  • 返回結果: 返回小寫的字符串A
  • 返回類型: string
  • select lower('Aa'); -- 結果爲 aa
  • select lcase('Aa'); -- 結果爲 aa

lpad(string str, int len, string pad)

  • 返回結果: 使用pad填充字符串str的左邊,使其長度變爲len;如果字符串str的長度大於len,則str將被截斷;如果pad爲空字符或者NULL,則返回NULL
  • 返回類型: string
  • select lpad('123', 5, '0'); -- 結果爲 00123
  • select lpad('123', 2, '0'); -- 結果爲 12
  • select lpad('123', 5, ''); -- 結果爲 NULL

ltrim(string A)

  • 返回結果: 去掉字符串A左邊的空格
  • 返回類型: string
  • select ltrim(' 123 3 '); -- 結果爲 '123 3 '

ngrams(array<array>, int N, int K, int pf)

  • 返回結果: 提取前K個N-gram(N-gram就是一個長度爲N的詞語組成的序列)
  • 返回類型: array<struct<string,double>>
  • select ngrams(array(array('from','a'),array('from','a'),array('from','b')), 2, 1); -- 結果爲 [{"ngram":["from","a"],"estfrequency":2.0}]
  • select ngrams(array(array('from','a'),array('from','a'),array('from','b')), 1, 1); -- 結果爲 [{"ngram":["from"],"estfrequency":3.0}]

octet_length(string str)

  • 返回結果: 返回以UTF-8編碼保存字符串str所需的八位字節數
  • 返回類型: int
  • select octet_length('AAA1'); -- 結果爲 15

parse_url(string urlString, string partToExtract [, string keyToExtract])

  • 返回結果: 解析url(partToExtract包括HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, 以及USERINFO)
  • 返回類型: string
  • select parse_url('https://github.com/TheUncleWhoGrowsBeans/Melon-and-fruit-fields', 'HOST'); -- 結果爲 github.com
  • select parse_url('https://github.com/TheUncleWhoGrowsBeans/Melon-and-fruit-fields', 'PATH'); -- 結果爲 /TheUncleWhoGrowsBeans/Melon-and-fruit-fields
  • select parse_url('http://hostname.com/path?k1=v1&k2=v2#ref1', 'REF'); -- 結果爲 ref1
  • select parse_url('http://hostname.com/path?k1=v1&k2=v2#ref1', 'QUERY', 'k2'); -- 結果爲 v2

printf(String format, Obj... args)

  • 返回結果: 返回printf-style的字符串
  • 返回類型: string
  • select printf('%s%d歲了', 'Uncle Bean', 1); -- 結果爲 Uncle Bean1歲了

quote(String text)

  • 返回結果: 返回帶引號的字符串
  • 返回類型: string
  • select quote('Uncle Bean'); -- 結果爲 'Uncle Bean'

regexp_extract(string subject, string pattern, int index)

  • 返回結果: 正則提取
  • 返回類型: string
  • select regexp_extract('http://hostname.com/path?k1=v1&k2=v2#ref1', 'http://([0-9a-zA-z]+.[0-9a-zA-z]+)/', 1); -- 結果爲 hostname.com

regexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT)

  • 返回結果: 正則替換
  • 返回類型: string
  • select regexp_replace('我1不23要456數7890字', '[0-9]+', ''); -- 結果爲 我不要數字

repeat(string str, int n)

  • 返回結果: 返回重複n次str後的字符串
  • 返回類型: string
  • select repeat('a', 3); -- 結果爲 aaa

replace(string A, string OLD, string NEW)

  • 返回結果: 替換
  • 返回類型: string
  • select replace('123123', '123', 'haha'); -- 結果爲 hahahaha

reverse(string A)

  • 返回結果: 反轉字符串A
  • 返回類型: string
  • select reverse('1234'); -- 結果爲 4321

rpad(string str, int len, string pad)

  • 返回結果: 使用pad填充字符串str的右邊,使其長度變爲len;如果字符串str的長度大於len,則str將被截斷;如果pad爲空字符或者NULL,則返回NULL
  • 返回類型: string
  • select rpad('123', 5, '0'); -- 結果爲 12300
  • select rpad('123', 2, '0'); -- 結果爲 12
  • select rpad('123', 5, ''); -- 結果爲 NULL

rtrim(string A)

  • 返回結果: 去掉字符串A右邊的空格
  • 返回類型: string
  • select rtrim('1 123 '); -- 結果爲 1 123

sentences(string str, string lang, string locale)

  • 返回結果: 將自然語言文本串標記爲單詞和句子(分詞)
  • 返回類型: array<array>
  • select sentences('Hello Bean! How are you?'); -- 結果爲 [["Hello","Bean"],["How","are","you"]]

space(int n)

  • 返回結果: 返回n個空格的字符串
  • 返回類型: string
  • select concat('->', space(3), '<-'); -- 結果爲 -> <-

split(string str, string pat)

  • 返回結果: 使用指定分隔符pat拆分字符串str
  • 返回類型: array
  • select split('123123', '2'); -- 結果爲 ["1","31","3"]
  • select split('123123', '12'); -- 結果爲 ["","3","3"]

str_to_map(text[, delimiter1, delimiter2])

  • 返回結果: 將字符串轉換爲map
  • 返回類型: map<string,string>
  • select str_to_map('k1:v1,k2:v2'); -- 結果爲 {"k1":"v1","k2":"v2"}

substr(string|binary A, int start) substring(string|binary A, int start)

  • 返回結果: 截取字符串
  • 返回類型: substr
  • select substr('12345', 2); -- 結果爲 2345
  • select substring('12345', 2); -- 結果爲 2345

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

  • 返回結果: 截取字符串
  • 返回類型: string
  • select substr('12345', 2, 3); -- 結果爲 234
  • select substring('12345', 2, 3); -- 結果爲 234

substring_index(string A, string delim, int count)

  • 返回結果: 根據delim將字符串A分爲多個部分,然後根據count返回部分字符串
  • 返回類型: string
  • select substring_index('1.2.3', '.', 2); -- 結果爲 1.2
  • select substring_index('1.2.3', '.', -2); -- 結果爲 2.3

translate(string|char|varchar input, string|char|varchar from, string|char|varchar to)

  • 返回結果: 將出現在from中的每個字符替換爲to中的相應字符;若from比to長,那麼在from中比to中多出的字符將會被刪除
  • 返回類型: string
  • select translate('abc不是ab', 'abc', 'd'); -- 結果爲 d不是d
  • select translate('abc不是a,也不是b,也不是c', 'abc', 'd'); -- 結果爲 d不是d,也不是,也不是

trim(string A)

  • 返回結果: 去掉字符串A左右兩邊的空格
  • 返回類型: string
  • select concat('->', trim(' 1 1 '), '<-'); -- 結果爲 ->1 1<-

unbase64(string str)

  • 返回結果: base64解碼
  • 返回類型: binary
  • select unbase64('VW5jbGUgQmVhbg=='); -- 結果爲 Uncle Bean

upper(string A) ucase(string A)

  • 返回結果: 返回大寫的字符串A
  • 返回類型: string
  • select upper('aa'); -- 結果爲 AA
  • select ucase('aa'); -- 結果爲 AA

initcap(string A)

  • 返回結果: 轉換爲首字母大寫的字符串A
  • 返回類型: string
  • select initcap('the uncle who grows beans'); -- 結果爲 The Uncle Who Grows Beans

levenshtein(string A, string B)

  • 返回結果: 返回兩個字符串之間的Levenshtein距離(也稱編輯距離,指的是兩個字符串之間,由一個轉換成另一個所需的最少編輯操作次數)
  • 返回類型: int
  • select levenshtein('hehe', 'haha'); -- 結果爲 2

soundex(string A)

  • 返回結果: 返回字符串的soundex代碼
  • 返回類型: string
  • select soundex('Uncle Bean'); -- 結果爲 U524

相關文章

Hive函數大全(含例子)之集合函數、日期函數、條件函數

Hive函數大全(含例子)之數學函數(Mathematical Functions)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章