Hive QL内置字符串处理函数

Hive提供有许多内置函数来帮助我们处理和查询数据,包括:

  • 字符串操作函数
  • 数据操作函数
  • 类型转换函数
  • 条件运算符函数
  • 数学函数

官方文档参考:查看

查看Hive所支持的函数列表:

show functions;    

如果要查看每个函数的帮助和用法,使用下面这样的方法:

-- 查看函数upper的帮助说明
desc function upper;
desc function extended upper;

-- 查看函数month的帮助说明
desc function month;
desc function extended month;

-- 查看函数parse_url的帮助说明
desc function parse_url;
desc function extended parse_url;    

请参考 Hive内置函数_字符串函数(视频教程)

请参考 Hive内置函数_字符串正则函数(视频教程)

一、Hive字符串函数

字符串函数主要用于一些常用的字符串处理。

1、字符串长度计算函数

-- 语法: length(string A),
-- 返回值: int
-- 说明:返回字符串A的长度
select length('cda_hive') str_length;

2、字符串反转函数

-- 语法: reverse(string A)
-- 返回值: string
-- 说明:返回字符串A的反转结果
select reverse('cda_hive') str_reverser;

3、字符串连接函数

-- 语法: concat(string A, string B, …)
-- 返回值: string
-- 说明:返回输入字符串连接后的结果,支持任意个输入字符串
select concat('we-','love-','hive') str_concat;

4、带分隔符字符串连接函数。

返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符。

-- 语法: concat_ws(string SEP, string A, string B…)
-- 返回值: string
-- 说明:返回输入字符串连接后的结果,SEP表示各个字符串间的分隔符
select concat_ws('-','we','love','hive') str_concat_ws;  

5、字符串截取函数: substr, substring

-- 语法: substr(string A, int start),substring(string A, int start)
--       substr(string A, int start, int len),substring(string A, intstart, int len)
-- 返回值: string
-- 说明:返回字符串A从start位置到结尾的字符串
-- 返回字符串从start位置到结尾的字符串
select substr('abcde',3);  
select substring('abcde',3);  
select substr('abcde',-1);
-- 返回字符串从start位置开始,长度为len的字符串  
select substr('abcde',3,2);  
select substring('abcde',3,2);  
select substring('abcde',-2,2); 

6、字符串转大写函数:upper,ucase

-- 语法: upper(string A) ucase(string A)
-- 返回值: string
-- 说明:返回字符串A的大写格式
select upper('abSEd');  
select ucase('abSEd'); 
select upper("hello world");

7、字符串转小写函数:lower,lcase

-- 语法: lower(string A) lcase(string A)
-- 返回值: string
-- 说明:返回字符串A的小写格式
select lower('abSEd');  
select lcase('abSEd');

8、去空格函数:trim,去除字符串两边的空格

-- 语法: trim(string A)
-- 返回值: string
-- 说明:去除字符串两边的空格
select trim(' abc ');  

9、左边去空格函数:ltrim

-- 语法: ltrim(string A)
-- 返回值: string
-- 说明:去除字符串左边的空格
select ltrim(' abc '); 

10、右边去空格函数:rtrim

-- 语法: rtrim(string A)
-- 返回值: string
-- 说明:去除字符串右边的空格
select rtrim(' abc ');  

11、空格字符串函数:space

-- 语法: space(int n)
-- 返回值: string  
-- 说明:返回长度为n的字符串  
select space(10);  
select length(space(10));

12、重复字符串函数:repeat

-- 语法: repeat(string str, int n)
-- 返回值: string  
-- 说明:返回重复n次后的str字符串  
select repeat('abc',5); 

13、首字符ascii函数:ascii

-- 语法: ascii(string str)
-- 返回值: int  
-- 说明:返回字符串str第一个字符的ascii码  
select ascii('abcde'); 

14、左补足函数:lpad

-- 语法: lpad(string str, int len, string pad)
-- 返回值: string  
-- 说明:将str进行用pad进行左补足到len位  
select lpad('abc',10,'td');  

15、右补足函数:rpad

-- 语法: rpad(string str, int len, string pad)
-- 返回值: string  
-- 说明:将str进行用pad进行右补足到len位  
select rpad('abc',10,'td');  

16、分割字符串函数: split

-- 语法:  split(string str, string pat)
-- 返回值:  array  
-- 说明: 按照pat字符串分割str,会返回分割后的字符串数组  
select split('abtcdtef','t');  

17、集合查找函数: find_in_set

-- 语法: find_in_set(string str, string strList)
-- 返回值: int  
-- 说明: 返回str在strlist第一次出现的位置,strlist是用逗号分割的字符串。如果没有找该str字符,则返回0  
select find_in_set('ab','ef,ab,de');  
select find_in_set('at','ef,ab,de');  

18、正则表达式替换函数:regexp_replace

-- 语法: regexp_replace(string A, string B, string C)
-- 返回值: string
-- 说明:将字符串A中的符合java正则表达式B的部分替换为C。注意,在有些情况下要使用转义字符
select regexp_replace('我喜欢和平,反对暴力和枪支', '暴力|枪支', '**');

19、正则表达式解析函数:regexp_extract

-- 语法: regexp_extract(string subject, string pattern, int index)  
-- 返回值: string  
-- 说明:将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符
select regexp_extract('foothebar', 'foo(.*?)(bar)', 1);  
select regexp_extract('foothebar', 'foo(.*?)(bar)', 2);  
select regexp_extract('foothebar', 'foo(.*?)(bar)', 0);  

20、URL解析函数:parse_url

-- 语法: parse_url(string urlString, string partToExtract [, stringkeyToExtract])
-- 返回值: string
-- 说明:解析URL字符串并返回URL中指定的部分。
--       partToExtract的选项包含[HOST,PATH,QUERY,REF,PROTOCOL,FILE,AUTHORITY,USERINFO]。  
select parse_url('http://www.cda.com/path/p1.php?query=1', 'HOST');
select parse_url('http://www.cda.com/path/p1.php?query=1', 'PATH');
select parse_url('http://www.cda.com/path/p1.php?query=1', 'QUERY'); 
select parse_url('http://www.cda.com/path/p1.php?query=1', 'FILE'); 
select parse_url('http://www.cda.com/path/p1.php?query=1#Ref', 'PROTOCOL');  
select parse_url('http://www.cda.com/path/p1.php?query=1#Ref', 'REF');	
--      可以指定key来返回特定参数,例如  
select parse_url('http://www.cda.com/path/p1.php?query=1&kd=phone', 'QUERY','kd');  
-- 获得与每本图书相关联的小图像的FILE部分:
select parse_url('http://images.amazon.com/images/P/0671870432.01.LZZZZZZZ.jpg','FILE');

21、json解析函数:get_json_object

-- 语法: get_json_object(string json_string, string path)
-- 返回值: string
-- 说明:解析json的字符串json_string,返回path指定的内容。如果输入的json字符串无效,那么返回NULL。
/*
{
   "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"
}
*/
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');
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"}','$.email');
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"}','$.store.bicycle.price');
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"}','$.store.fruit[0].weight');

二、【示例】天气数据分析

下面这个示例中,我们将通过编写Hive QL查询语句,使用Hive字符串函数来处理天气数据,主要目的是熟悉Hive字符串函数的使用。

请参考Hive用例_分析气温监测数据(视频教程)

气象传感器在全球各个地方每小时收集一次数据,并聚集为大量的日志数据,每个气象站每年一个数据压缩包。这非常适合使用MapReduce进行分析(半结构化数据,面向记录的) 本案例中使用NCDC(美国国家气象资料中心)的数据,这些数据使用一行一行的ASCII格式存储,每行一条记录。我们重点关注其中的温度值。

气象数据样本sample.txt:如下:

0067011990999991950051507004+68750+023550FM-12+038299999V0203301N00671220001CN9999999N9+00001+99999999999
0043011990999991950051512004+68750+023550FM-12+038299999V0203201N00671220001CN9999999N9+00221+99999999999
0043011990999991950051518004+68750+023550FM-12+038299999V0203201N00261220001CN9999999N9-00111+99999999999
0043012650999991949032412004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+01111+99999999999
0043012650999991949032418004+62300+010750FM-12+048599999V0202701N00461220001CN0500001N9+00781+99999999999

年份和温度值如下度所示:


请按以下步骤执行:

1)创建存放天气日志数据的hive表

create table weather(line string);

2)加载数据文件到表中

load data inpath '/wd/wd-input/sample.txt' overwrite into table weather;

3)查询"年份"、"温度"值

select substr(line,16,4) as year,cast(substr(line,88,5) as int) as temp from weather;

4)查询每年的最高温度

select year,max(temp)
from(
  select substr(line,16,4) as year,
         cast(substr(line,88,5) as int) as temp 
  from weather
) as w
group by year;

《Flink原理深入与编程实战》