Hive版本: hive-1.1.0-cdh5.14.2
1. 首字符转ascii码函数:ascii
语法:ascii(string str)
返回值:int
描述:返回字符串str首字符的ascii编码
0: jdbc:hive2://node03:10000> select ascii('hello') as col1, ascii('hehe') as col2, ascii('Hi') as col3;
+-------+-------+-------+--+
| col1 | col2 | col3 |
+-------+-------+-------+--+
| 104 | 104 | 72 |
+-------+-------+-------+--+
2. 字符串连接函数: concat
语法: concat(string|binary A, string|binary B…)
返回值: string
描述: 连接A,B…,可以是任意多个
0: jdbc:hive2://node03:10000> select concat('hello',' ','world');
+--------------+--+
| _c0 |
+--------------+--+
| hello world |
+--------------+--+
3. 自定义分隔符的字符串连接函数: concat_ws
语法: concat_ws(string SEP, string A, string B…)
返回值: string
描述: 以SEP为分隔符,连接A,B…
0: jdbc:hive2://node03:10000> select concat_ws('#','welcome','to','beijing');
+---------------------+--+
| _c0 |
+---------------------+--+
| welcome#to#beijing |
+---------------------+--+
4. 自定义分隔符的字符串数组连接函数:concat_ws
语法:concat_ws(string SEP, array)
返回值: string
描述:以SEP为分隔符,连接array中的字符串
0: jdbc:hive2://node03:10000> select concat_ws('#',array('welcome','to','beijing'));
+---------------------+--+
| _c0 |
+---------------------+--+
| welcome#to#beijing |
+---------------------+--+
5. 字符串查找函数: field
语法:field(val T,val1 T,val2 T,val3 T,…)
返回值:int
描述: 返回val在 val1,val2,val3,…出现的位置,查找不到返回0
0: jdbc:hive2://node03:10000> select field('world','say','hello','world');
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
6. 集合查找函数: find_in_set
语法:find_in_set(string str, string strList)
返回值: int
描述: 返回str在字符串列表strList中第一次出现的位置,查找不到返回0
0: jdbc:hive2://node03:10000> select find_in_set('ab', 'abc,b,ab,c,def');
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
7. 数字格式化函数: format_number
语法: format_number(number x, int d)
返回值: string
描述: 把数字x格式化为逗号分隔的千分数数字(’#,###,###.##’),并保留d个小数位
0: jdbc:hive2://node03:10000> select format_number(123456789.000, 2) as col1,
. . . . . . . . . . . . . . > format_number(123456789.000, 0) as col2,
. . . . . . . . . . . . . . > format_number(123456789.000, 5) as col3;
+-----------------+--------------+--------------------+--+
| col1 | col2 | col3 |
+-----------------+--------------+--------------------+--+
| 123,456,789.00 | 123,456,789 | 123,456,789.00000 |
+-----------------+--------------+--------------------+--+
8. json解析函数:get_json_object
语法: get_json_object(string json_string, string path)
返回值: string
描述: 解析json字符串json_string,返回path指定的内容。如果json_string无效,返回null
0: jdbc:hive2://node03:10000> select get_json_object('{"store":{"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}], "bicycle":{"price":19.95,"color":"red"} },"email":"amy@only_for_json_udf_test.net","owner":"amy"}','$.owner');
+------+--+
| _c0 |
+------+--+
| amy |
+------+--+
9. 字符查找函数:instr
语法: instr(string str, string substr)
返回值: int
描述:返回substr在str中第一次出现的位置,查找不到返回0
0: jdbc:hive2://node03:10000> select instr('abcdef','c');
+------+--+
| _c0 |
+------+--+
| 3 |
+------+--+
10. 字符串长度函数: length
语法:length(string A)
返回值: int
描述: 返回字符串A的长度
0: jdbc:hive2://node03:10000> select length('hello');
+------+--+
| _c0 |
+------+--+
| 5 |
+------+--+
11. 字符串查找函数:locate
语法: locate(string substr, string str[, int pos])
返回值: int
描述:从pos位置开始查找,返回substr在str中第一次出现的位置。pos缺省为1
0: jdbc:hive2://node03:10000> select locate('c', 'cabcdef') as col1, locate('c', 'cabcdef', 2) as col2;
+-------+-------+--+
| col1 | col2 |
+-------+-------+--+
| 1 | 4 |
+-------+-------+--+
12. 转换小写函数: lower / lcase
语法:lower(string A) / lcase(string A)
返回值: string
描述: 把字符串A转换为小写
0: jdbc:hive2://node03:10000> select lower('fOoBaR') as lower, lcase('fOoBaR') as lcase;
+---------+---------+--+
| lower | lcase |
+---------+---------+--+
| foobar | foobar |
+---------+---------+--+
13. 转换大写函数:upper / ucase
语法:upper(string A) ucase(string A)
返回值: string
描述:把字符串A转换为大写
0: jdbc:hive2://node03:10000> select upper('fOoBaR') as upper, ucase('fOoBaR') as ucase;
+---------+---------+--+
| upper | ucase |
+---------+---------+--+
| FOOBAR | FOOBAR |
+---------+---------+--+
14. 首字母大写函数: initcap
语法:initcap(string A)
返回值:string
描述: A的首字母转换为大写,其他转换为小写
0: jdbc:hive2://node03:10000> select initcap('fOoBaR');
+---------+--+
| _c0 |
+---------+--+
| Foobar |
+---------+--+
15. 左补齐函数: lpad
语法: lpad(string str, int len, string pad)
返回值: string
描述: 使用pad左补齐str到len位。如果len小于str的长度,则str会被在尾部截断。如果pad为空,则返回值也为空
0: jdbc:hive2://node03:10000> select lpad('abcdef', 8, '#') as col1,
. . . . . . . . . . . . . . > lpad('abcdef', 5, '#') as col2,
. . . . . . . . . . . . . . > lpad('abcdef', 8, '') as col3;
+-----------+--------+-------+--+
| col1 | col2 | col3 |
+-----------+--------+-------+--+
| ##abcdef | abcde | NULL |
+-----------+--------+-------+--+
16. 右补齐函数:rpad
语法:rpad(string str, int len, string pad)
返回值:string
描述: 使用pad右补齐str到len位。如果len小于str的长度,则str会被在尾部截断。如果pad为空,则返回值也为空
0: jdbc:hive2://node03:10000> select rpad('abcdef', 8, '#') as col1,
. . . . . . . . . . . . . . > rpad('abcdef', 5, '#') as col2,
. . . . . . . . . . . . . . > rpad('abcdef', 8, '') as col3;
+-----------+--------+-------+--+
| col1 | col2 | col3 |
+-----------+--------+-------+--+
| abcdef## | abcde | NULL |
+-----------+--------+-------+--+
17. 去空格函数: trim
语法: trim(string A)
返回值: string
描述:去掉字符串A两端的空格
0: jdbc:hive2://node03:10000> select concat('|',' foobar ','|') as notrim, concat('|',trim(' foobar '),'|') as trim;
+-------------+-----------+--+
| notrim | trim |
+-------------+-----------+--+
| | foobar | | |foobar| |
+-------------+-----------+--+
18. 左去空格函数:ltrim
语法:ltrim(string A)
返回值: string
描述:去掉字符串A左边的空格
0: jdbc:hive2://node03:10000> select concat('|',' foobar ','|') as notrim, concat('|',ltrim(' foobar '),'|') as ltrim;
+-------------+------------+--+
| notrim | ltrim |
+-------------+------------+--+
| | foobar | | |foobar | |
+-------------+------------+--+
19. 右去空格函数: rtrim
语法: rtrim(string A)
返回值:string
描述: 去掉字符串A右边的空格
0: jdbc:hive2://node03:10000> select concat('|',' foobar ','|') as notrim, concat('|',rtrim(' foobar '),'|') as rtrim;
+-------------+------------+--+
| notrim | rtrim |
+-------------+------------+--+
| | foobar | | | foobar| |
+-------------+------------+--+
20. URL解析函数: parse_url
语法:parse_url(string urlString, string partToExtract [, string keyToExtract])
返回值: string
描述: 返回URL中指定的部分。partToExtract的有效值为:HOST, PATH, QUERY, REF, PROTOCOL, AUTHORITY, FILE, USERINFO
0: jdbc:hive2://node03:10000> select parse_url('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1','HOST') as host,
. . . . . . . . . . . . . . > parse_url('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1','QUERY','k1') as k1,
. . . . . . . . . . . . . . > parse_url('https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1','PROTOCOL') as protocol;
+--------------------+-----+-----------+--+
| host | k1 | protocol |
+--------------------+-----+-----------+--+
| www.tableName.com | v1 | https |
+--------------------+-----+-----------+--+
21. 格式打印函数:printf
语法:printf(String format, Obj… args)
返回值:string
描述:返回按照printf中格式字符串format格式化后的输入
0: jdbc:hive2://node03:10000> select printf('I am %d %s old', 28, 'years');
+--------------------+--+
| _c0 |
+--------------------+--+
| I am 28 years old |
+--------------------+--+
22. 正则解析函数:regexp_extract
语法: regexp_extract(string subject, string pattern, int index)
返回值: string
描述: 将字符串subject,按照pattern正则表达式拆分,返回index指定的部分
0: jdbc:hive2://node03:10000> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) as pattern1,
. . . . . . . . . . . . . . > regexp_extract('foothebar', 'foo(.*?)(bar)', 2) as pattern2,
. . . . . . . . . . . . . . > regexp_extract('foothebar', 'foo(.*?)(bar)', 0) as pattern0;
+-----------+-----------+------------+--+
| pattern1 | pattern2 | pattern0 |
+-----------+-----------+------------+--+
| the | bar | foothebar |
+-----------+-----------+------------+--+
23. 正则替换函数: regexp_replace
语法: regexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT)
返回值: string
描述: 将原字符串INITIAL_STRING中,符合PATTERN规则的替换为REPLACEMENT
0: jdbc:hive2://node03:10000> select regexp_replace('hello world, hello hive','world|hive','NextAction');
+-------------------------------------+--+
| _c0 |
+-------------------------------------+--+
| hello NextAction, hello NextAction |
+-------------------------------------+--+
24. 重复函数: repeat
语法: repeat(string str, int n)
返回值: string
描述: 重复n次字符串str
0: jdbc:hive2://node03:10000> select repeat('I love China.',3);
+------------------------------------------+--+
| _c0 |
+------------------------------------------+--+
| I love China.I love China.I love China. |
+------------------------------------------+--+
25. 反转函数:reverse
语法: reverse(string A)
返回值: string
描述: 反转字符串A中的字符顺序
0: jdbc:hive2://node03:10000> select reverse('NextAction');
+-------------+--+
| _c0 |
+-------------+--+
| noitcAtxeN |
+-------------+--+
26. 句子转单词函数:sentences
语法:sentences(string str, string lang, string locale)
返回值: array<array>
描述:将句子拆分为包含单词的数组,lang和locale是可选参数
0: jdbc:hive2://node03:10000> select sentences('Hello there! How are you?');
+------------------------------------------+--+
| _c0 |
+------------------------------------------+--+
| [["Hello","there"],["How","are","you"]] |
+------------------------------------------+--+
27. 空格生成函数:space
语法:space(int n)
返回值: string
描述:返回n个空格
0: jdbc:hive2://node03:10000> select concat('hello world', space(5), '!');
+--------------------+--+
| _c0 |
+--------------------+--+
| hello world ! |
+--------------------+--+
28. 字符串分割函数:split
语法: split(string str, string pat)
返回值: array
描述:按照pat分隔字符串str
0: jdbc:hive2://node03:10000> select split('abxefxmn', 'x');
+-------------------+--+
| _c0 |
+-------------------+--+
| ["ab","ef","mn"] |
+-------------------+--+
29. 字符串转键值对函数:str_to_map
语法: str_to_map(text[, delimiter1, delimiter2])
返回值: map<string,string>
描述:将text分割为K-V键值对。delimiter1将text分割为多个K-V对,delimiter2再将K-V对分割。delimiter1缺省为逗号(’,’),delimiter2缺省为冒号(’:’)
0: jdbc:hive2://node03:10000> select str_to_map('name:NextAction,age:28,location:China', ',', ':');
+----------------------------------------------------+--+
| _c0 |
+----------------------------------------------------+--+
| {"location":"China","name":"NextAction","age":"28"} |
+----------------------------------------------------+--+
30. 字符串截取函数:substr / substring
语法: substr(string|binary A, int start, int len) / substring(string|binary A, int start, int len)
返回值: string
描述:将字符串A,从start位置开始,截取len位。如缺省len,默认截取到最后
0: jdbc:hive2://node03:10000> select substr('NextAction', 5) as sub_5,
. . . . . . . . . . . . . . > substr('NextAction', 5,1) as sub_5_1;
+---------+----------+--+
| sub_5 | sub_5_1 |
+---------+----------+--+
| Action | A |
+---------+----------+--+
31. 字符替换函数: translate
语法: translate(string|char|varchar input, string|char|varchar from, string|char|varchar to)
返回值: string
描述:将input中出现在from中的字符,按照to中对应位置的字符替换
0: jdbc:hive2://node03:10000> select translate('N1xtA23ion', '123', 'ect');
+-------------+--+
| _c0 |
+-------------+--+
| NextAction |
+-------------+--+