Hive 字符串函数
来源:互联网 发布:测试端口通不通命令 编辑:程序博客网 时间:2024/06/05 16:32
Hive内部提供了很多操作字符串的相关函数,本文将对其中部分常用的函数进行介绍。
下表为Hive内置的字符串函数,具体的用法可以参见本文的下半部分。
'QUERY', 'k1') 返回'v1'stringprintf(String format, Obj... args)将输入参数进行格式化输出stringregexp_extract(string subject, string pattern, int index)使用pattern从给定字符串中提取字符串。如: regexp_extract('foothebar', 'foo(.*?)(bar)', 2) 返回'bar' 有时需要使用预定义的字符类:使用'\s' 做为第二个参数将匹配s,'s'匹配空格等。参数index是Java正则匹配器方法group()方法中的索引stringregexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT)使用REPLACEMENT替换字符串INITIAL_STRING中匹配PATTERN的子串,例如: regexp_replace("foobar", "oo|ar", "") 返回'fb'stringrepeat(string str, int n)将str重复n次stringreverse(string A)将字符串A翻转stringrpad(string str, int len, string pad)在str的右侧使用pad填充至长度lenstringrtrim(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") )stringspace(int n)返回n个空格的字符串arraysplit(string str, string pat)用pat分割字符串str,pat为正则表达式map<string,string>str_to_map(text[, delimiter1, delimiter2])使用两个分隔符将文本分割为键值对。第一个分隔符将文本分割为K-V 对,第二个分隔符分隔每个K-V 对。默认第一个分隔符为“,“,第二个分隔符为=stringsubstr(string|binary A, int start) substring(string|binary A, int start)返回A从位置start直到结尾的子串stringsubstr(string|binary A, int start, int len) substring(string|binary A, int start, int len)返回A中从位置start开始,长度为len的子串,如: substr('foobar', 4, 1) 返回 'b'stringtranslate(string input, string from, string to)将input中出现在from中的字符替换为to中的字符串,如果任何参数为null,结果为nullstringtrim(string A)去掉字符串A两端的空格binaryunbase64(string str)将base64字符串转换为二进制stringupper(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.
例子:
hive>
select
parse_url(
'http://iteblog.com?weixin=iteblog_hadoop'
,
'HOST'
)
from
iteblog;
iteblog.com
hive>
select
parse_url(
'http://iteblog.com?weixin=iteblog_hadoop'
,
>
'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":"amy@only_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
- hive常用字符串函数
- hive字符串函数
- hive字符串函数
- Hive常用字符串函数
- hive字符串函数
- hive字符串函数
- hive字符串函数
- hive常用字符串函数
- hive 字符串拼接函数
- Hive 字符串函数
- Hive UDF整理 字符串函数
- hive字符串基础操作函数
- hive字符串基础解析函数
- hive函数 -- split 字符串分割函数
- hive函数 -- split 字符串分割函数
- hive函数 -- split 字符串分割函数
- hive函数_split 字符串分割函数
- 【Hive自定义UDF函数】 与 【hive字符串函数】
- 数组排序和常用类
- 单片机中映射表在UI设计中的应用
- MySQL 输入密码闪退
- java 异常
- Android睡眠唤醒机制--Kernel态
- Hive 字符串函数
- 史上最简单清晰的红黑树讲解
- java.util.zip.ZipException: invalid LOC header (bad signature)
- pom.xml报错:web.xml is missing and <failOnMissingWebXml> is set to true
- NYOJ 首字母变大写
- 不得不了解的四大作用域:application,session,request,page
- C++写插入排序
- GameEntity(十一)—— Iplayer
- HDU-1798 Tell me the area(数学)