【Hive】字符串函数

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  |
+-------------+--+
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive中,有许多字符串函数可用于处理和操作字符串。其中一些常用的字符串函数包括: 1. 字符串长度函数:length(str) 这个函数返回字符串str的长度。 2. 字符串反转函数:reverse(str) 这个函数返回将字符串str反转后的结果。 3. 字符串连接函数:concat(str1, str2, ...) 这个函数将多个字符串连接在一起。 4. 带分隔符字符串连接函数:concat_ws(sep, str1, str2, ...) 这个函数将多个字符串连接在一起,并使用指定的分隔符sep进行分隔。 5. 字符串截取函数:substr(str, start, len) 或 substring(str, start, len) 这个函数返回从字符串str中从start位置开始,长度为len的子字符串。 6. 字符串转大写函数:upper(str) 或 ucase(str) 这个函数字符串str转换为大写格式。 7. 字符串转小写函数:lower(str) 或 lcase(str) 这个函数字符串str转换为小写格式。 8. 去空格函数:trim(str) 这个函数去除字符串str两端的空格。 9. 左边去空格函数:ltrim(str) 这个函数去除字符串str左边的空格。 10. 右边去空格函数:rtrim(str) 这个函数去除字符串str右边的空格。 这些是Hive中一些常用的字符串函数,你可以根据需要选择使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Hive--函数--常用内置函数--字符串函数](https://blog.csdn.net/qq_46893497/article/details/110948038)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Hive常用字符串函数](https://blog.csdn.net/weixin_43823423/article/details/86663824)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值