【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 |
+-------------+--+
【Hive】字符串函数相关推荐
- Hive常用运算(关系运算)、逻辑运算与数学运算、数值运算、日期函数、条件函数、字符串函数
hive 常用运算 第一部分:关系运算 Hive支持的关系运算符 •常见的关系运算符 •等值比较: = •不等值比较: <> •小于比较: < •小于等于比较: <= •大于比 ...
- hive 如何将数组转成字符串_Hive函数大全(含例子)之字符串函数(String Functions)...
字符串函数 String Functions ascii(string str) 返回结果: 返回字符串str首字母的十进制ascii码返回类型: intselect ascii('ABC'); -- ...
- hive常用函数之七字符串函数
七.字符串函数 1. 字符串长度函数:length 语法: length(string A) 返回值: int 说明:返回字符串A的长度 举例: hive> select length('abc ...
- Hive函数(内置函数(字符串函数,数学函数,日期函数,集合函数,条件函数,聚合函数,表生成函数)和自定义函数(自定义函数创建流程,临时函数,永久函数)))(四)
Hive函数(内置函数和自定义函数) 一.内置函数 1.字符串函数 (1)ascii (2)base64 (3)concat (4)concat_ws (5)format_number (6)subs ...
- hive函数大全(数学函数,集合函数,类型转换,日期函数,条件函数,字符串函数,及侧视图)
在hive中,可以使用以下命令查看某个函数的用法: desc function extended 函数名字; 比如查函数date_add函数的用法: desc function extended da ...
- hive:函数:字符串函数
Hive内部提供了很多操作字符串的相关函数,本文将对其中部分常用的函数进行介绍. 下表为Hive内置的字符串函数,具体的用法可以参见本文的下半部分. 返回类型 函数名 描述 int ascii(str ...
- 【Hive】内置函数—字符串函数
目录 一.测试表结构 二.字符串函数使用介绍 1.字符串长度函数:length 2.字符串反转函数:reverse 3.字符串连接函数:concat 4.带分隔符字符串连接函数:concat_ws 5 ...
- 2021年大数据Hive(八):Hive自定义函数
全网最详细的Hive文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 系列历史文章 前言 Hive自定义函数 一.概述 1.UDF(User-Define ...
- Hive常用函数大全一览
Hive常用函数大全一览 1 关系运算 1.1 1.等值比较: = 1.2 2.不等值比较: 1.3 3.小于比较: < 1.4 4.小于等于比较: <= 1.5 5.大于比较: > ...
- hive中如何把13位转化为时间_重要知识点收藏 | Hive常用函数大全
关系运算 1.等值比较: = 语法:A=B 操作类型:所有基本类型 描述: 如果表达式A与表达式B相等,则为TRUE:否则为FALSE 举例: hive>select 1 from lxw_du ...
最新文章
- CSS浮动元素特点有什么
- 卸载和安装LINUX上的JDK
- CFileFind 遍历目录与子目录
- 【线上分享】如何使用开源SFU构建RTC云服务
- 利用矩阵的n次方求图的连通性
- vue2.0的Element UI的表格table列时间戳格式化
- 想换行做 5G 的开发者到底该咋办?
- 【PL/SQL】九九乘法口诀表
- 函数和存储过程的区别
- 【SW】利用3D打印机打印 PCB 钢网的方法
- spring诸如方式_一个简单的步进器,具有诸如下一步,返回和结束之类的简单动作,可以执行简单的表格
- python报错:index 1 is out of bounds for axis 0 with size 1
- 字节跳动确认将自研芯片;美国国土安全部被曝大量购买和使用手机定位数据;谷歌被俄罗斯罚款3.6亿美元 | 每日大事件...
- 5种常见的重复形式,平面设计手法进阶学习
- C++switch语句详解
- 【07】QQ群管理公告小结:
- C++中的exit()函数
- 总线概述及常见总线(转)
- 大不了重头再来 之 Archlinux GNOME3
- 小红书如何引流到淘宝店铺,提升产品销量?