Hive基础知识(十八):Hive 函数的使用

发布于:2024-06-28 ⋅ 阅读:(18) ⋅ 点赞:(0)

1. 系统内置函数

1)查看系统自带的函数

hive (hive3)> show functions;
Time taken: 0.085 seconds, Fetched: 289 row(s)

2)显示自带的函数的用法

hive (hive3)>  desc function upper;
OK
tab_name
upper(str)- Returns str with all characters changed to uppercase
Time taken: 0.107 seconds, Fetched: 1 row(s)

3)详细显示自带的函数的用法

hive (hive3)>  desc function upper;
OK
tab_name
upper(str)- Returns str with all characters changed to uppercase
Time taken: 0.107 seconds, Fetched: 1 row(s)
hive (hive3)> desc function extended upper;
OK
tab_name
upper(str)- Returns str with all characters changed to uppercase
Synonyms: ucase
Example:
> SELECT upper('Facebook') FROM src LIMIT 1;
'FACEBOOK'
Function class:org.apache.hadoop.hive.ql.udf.generic.GenericUDFUpper
Function type:BUILTIN
Time taken: 0.049 seconds, Fetched: 7 row(s)

2. 常用内置函数

2.1 空字段赋值

1)函数说明

NVL:给值为 NULL 的数据赋值,它的格式是 NVL( value,default_value)。它的功能是如果 value 为 NULL,则 NVL 函数返回 default_value 的值,否则返回 value 的值,如果两个参数都为 NULL ,则返回 NULL。

2)查询:如果员工的 comm 为 NULL,则用-1 代替

hive (hive3)> select ename,comm,nvl(comm,-1) from emp;
OK
ename  comm  _c2
SMITH  NULL -1.0
ALLEN 300.0 300.0
WARD 500.0 500.0
JONES  NULL -1.0
MARTIN 1400.0 1400.0
BLAKE  NULL -1.0
CLARK  NULL -1.0
SCOTT  NULL -1.0
KING 10.0 10.0
TURNER 0.0 0.0
ADAMS  NULL -1.0
JAMES  NULL -1.0
FORD  NULL -1.0
MILLER  NULL -1.0
Time taken: 0.184 seconds, Fetched: 14 row(s)

3)查询:如果员工的 comm 为 NULL,则用领导 id 代替

hive (hive3)> select comm, nvl(comm,mgr) from emp;
OK
comm  _c1
NULL 7902.0
300.0 300.0
500.0 500.0
NULL 7839.0
1400.0 1400.0
NULL 7839.0
NULL 7839.0
NULL 7566.0
10.0 10.0
0.0 0.0
NULL 7788.0
NULL 7698.0
NULL 7566.0
NULL 7782.0
Time taken: 0.242 seconds, Fetched: 14 row(s)

2.2 CASE WHEN THEN ELSE END

UDF:一进一出,UDAF:多进多出,UDTF:一进多出

1)数据准备

悟空 A 男
大海 A 男
宋宋 B 男
凤姐 A 女
婷姐 B 女
婷婷 B 女

2)需求

求出不同部门男女各多少人。结果如下:

dept_Id 男女
A 2 1
B 1 2

3)创建本地 emp_sex.txt,导入数据

[root@hadoop102 datas]$ vi emp_sex.txt
悟空 A 男
大海 A 男
宋宋 B 男
凤姐 A 女
婷姐 B 女
婷婷 B 女
4)创建 hive 表并导入数据
hive (hive3)> create table emp_sex(name string,dept_id string,sex string) row format delimited fields terminated by '';
OK
Time taken: 0.105 seconds
hive (hive3)> load data local inpath '/home/root/hive/emp_sex.txt' into table emp_sex;
Loading data to table hive3.emp_sex
OK
Time taken: 0.431 seconds

5)按需求查询数据

select dept_id,sum(case sex when '男' then 1 else 0 end) maleCount,sum(case sex when '女' then 1 else 0 end) femaleCount from emp_sex group by dept_id;
dept_id malecount  femalecount
A 2 1
B 1 2
Time taken: 39.575 seconds, Fetched: 2 row(s)

更换一种查询方式

hive (hive3)> select dept_id,sum(if(sex='男',1,0)) maleCount,sum(if(sex='女',1,0)) femaleCount from emp_sex group by dept_id;
dept_id malecount  femalecount
A 2 1
B 1 2
Time taken: 36.895 seconds, Fetched: 2 row(s)

2.3 行转列

1)相关函数说明

CONCAT(string A/col, string B/col…):返回输入字符串连接后的结果,支持任意个输入字符串;

hive (hive3)> select concat(ename,sal) from emp;
OK
_c0
SMITH800.0
ALLEN1600.0
WARD1250.0
JONES2975.0
MARTIN1250.0
BLAKE2850.0
CLARK2450.0
SCOTT3000.0
NULL
TURNER1500.0
ADAMS1100.0
JAMES950.0
FORD3000.0
MILLER1300.0
Time taken: 1.07 seconds, Fetched: 14 row(s)

CONCAT_WS(separator, str1, str2,...):它是一个特殊形式的 CONCAT()。第一个参数剩余参数间的分隔符。分隔符可以是与剩余参数一样的字符串。如果分隔符是 NULL,返回值也将为 NULL。这个函数会跳过分隔符参数后的任何 NULL 和空字符串。分隔符将被加到被连接的字符串之间;

注意: CONCAT_WS must be "string or array

hive (hive3)> select concat_ws('-',ename,job,hiredate) from emp;
OK
_c0
SMITH-CLERK-1980-12-17
ALLEN-SALESMAN-1981-2-20
WARD-SALESMAN-1981-2-22
JONES-MANAGER-1981-4-2
MARTIN-SALESMAN-1981-9-28
BLAKE-MANAGER-1981-5-1
CLARK-MANAGER-1981-6-9
SCOTT-ANALYST-1987-4-19
KING-PRESIDENT-5000.00
TURNER-SALESMAN-1981-9-8
ADAMS-CLERK-1987-5-23
JAMES-CLERK-1981-12-3
FORD-ANALYST-1981-12-3
MILLER-CLERK-1982-1-23
Time taken: 0.249 seconds, Fetched: 14 row(s)

COLLECT_SET(col):函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生 Array 类型字段。

hive (hive3)> select collect_set(id) from emp;
Total MapReduce CPU Time Spent: 3 seconds 620 msec
OK
_c0
[1001,1002,1111]
Time taken: 37.866 seconds, Fetched: 1 row(s)
hive (hive3)> select collect_list(id) from student;

2)数据准备

[root@hadoop100 hive]$ vim person_info.txt
孙悟空白羊座 A
大海射手座 A
宋宋白羊座 B
猪八戒白羊座 A
凤姐射手座 A
苍老师白羊座 B


孙悟空白羊座 A
大海射手座 A
宋宋白羊座 B
猪八戒白羊座 A
凤姐射手座 A
苍老师白羊座 B

3)需求

把星座和血型一样的人归类到一起。结果如下:

射手座,A 大海|凤姐
白羊座,A 孙悟空|猪八戒
白羊座,B 宋宋|苍老师

4)创建 hive 表并导入数据

hive (hive3)> create table person_info(name string,constellation string,blood_type string) row format delimited fields terminated by '';
OK
Time taken: 0.293 seconds
hive (hive3)> load data local inpath '/home/root/hive/person_info.txt' into table person_info;
Loading data to table hive3.person_info
OK
Time taken: 0.362 seconds

6)按需求查询数据

select
 concat(constellation,',',blood_type) cb,
 name
 from person_info;
#-----------进化------------
 select t1.cb,collect_set(t1.name) from 
(select concat(constellation,',',blood_type) cb,name from person_info) t1 
 group by t1.cb;
​
 OK
t1.cb  _c1
射手座,A ["大海","凤姐"]
白羊座,A ["孙悟空","猪八戒"]
白羊座,B ["宋宋","苍老师"]
Time taken: 38.329 seconds, Fetched: 3 row(s)
#-----------再次进化------------
 select t1.cb,concat_ws('|',collect_set(t1.name)) from 
(select concat(constellation,',',blood_type) cb,name from person_info) t1 
 group by t1.cb;
​
 OK
t1.cb  _c1
射手座,A 大海|凤姐
白羊座,A 孙悟空|猪八戒
白羊座,B 宋宋|苍老师
Time taken: 29.741 seconds, Fetched: 3 row(s)

2.4 列转行

1)函数说明

EXPLODE(col):将 hive 一列中复杂的 Array 或者 Map 结构拆分成多行。

LATERAL VIEW:侧写

用法:LATERAL VIEW udtf(expression) tableAlias AS columnAlias

解释:用于和 split, explode 等 UDTF 一起使用,它能够将一列数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。

2)数据准备

《疑犯追踪》-悬疑,动作,科幻,剧情
《Lie to me》-悬疑,警匪,动作,心理,剧情
《战狼2》-战争,动作,灾难

3)需求

将电影分类中的数组数据展开。结果如下:

4)创建本地 movie.txt,导入数据

[root@hadoop100 bin]$ vi movie.txt
《疑犯追踪》-悬疑,动作,科幻,剧情
《Lie to me》-悬疑,警匪,动作,心理,剧情
《战狼2》-战争,动作,灾难

5)创建 hive 表并导入数据

hive (hive3)> create table movie_info(movie string,category string)row format delimited fields terminated by '-';
OK
Time taken: 1.671 seconds
hive (hive3)> load data local inpath '/home/root/hive/movie.txt' into table movie_info;
Loading data to table hive3.movie_info
OK
Time taken: 1.103 seconds

6)按需求查询数据

#先切开城数组
hive (hive3)> select split(category,",") from movie_info;
OK
_c0
["悬疑","动作","科幻","剧情"]
["悬疑","警匪","动作","心理","剧情"]
["战争","动作","灾难"]
Time taken: 0.828 seconds, Fetched: 3 row(s)
#切成数组之后再展开
hive (hive3)> select explode(split(category,",")) from movie_info;
OK
col
悬疑
动作
科幻
剧情
悬疑
警匪
动作
心理
剧情
战争
动作
灾难
Time taken: 0.223 seconds, Fetched: 12 row(s)
#需要使用侧写关联电影名
hive (hive3)> select movie,category_name
> from movie_info
> lateral VIEW explode(split(category,",")) movie_info_tmp as category_name;
OK
movie  category_name
《疑犯追踪》悬疑
《疑犯追踪》动作
《疑犯追踪》科幻
《疑犯追踪》剧情
《Lie to me》悬疑
《Lie to me》警匪
《Lie to me》动作
《Lie to me》心理
《Lie to me》剧情
《战狼2》战争
《战狼2》动作
《战狼2》灾难
Time taken: 0.15 seconds, Fetched: 12 row(s)

2.4 窗口函数(开窗函数)

1)相关函数说明

OVER():指定分析函数工作的数据窗口大小,这个数据窗口大小可能会随着行的变而变化。

CURRENT ROW:当前行

n PRECEDING:往前 n 行数据

n FOLLOWING:往后 n 行数据

UNBOUNDED:起点,

UNBOUNDED PRECEDING 表示从前面的起点,

UNBOUNDED FOLLOWING 表示到后面的终点

LAG(col,n,default_val):往前第 n 行数据

LEAD(col,n, default_val):往后第 n 行数据

NTILE(n):把有序窗口的行分发到指定数据的组中,各个组有编号,编号从1 开始,对于每一行,NTILE 返回此行所属的组的编号。注意:n 必须为 int 类型

2)数据准备:name,orderdate,cost

jack,2017-01-01,10
tony,2017-01-02,15
jack,2017-02-03,23
tony,2017-01-04,29
jack,2017-01-05,46
jack,2017-04-06,42
tony,2017-01-07,50
jack,2017-01-08,55
mart,2017-04-08,62
mart,2017-04-09,68
neil,2017-05-10,12
mart,2017-04-11,75
neil,2017-06-12,80
mart,2017-04-13,94

3)需求

(1)查询在2017 年4 月份购买过的顾客及总人数

(2)查询顾客的购买明细及月购买总额

(3)上述的场景,将每个顾客的 cost 按照日期进行累加

(4)查询每个顾客上次的购买时间

(5)查询前20%时间的订单信息

4)创建本地 business.txt,导入数据

jack,2017-01-01,10
tony,2017-01-02,15
jack,2017-02-03,23
tony,2017-01-04,29
jack,2017-01-05,46
jack,2017-04-06,42
tony,2017-01-07,50
jack,2017-01-08,55
mart,2017-04-08,62
mart,2017-04-09,68
neil,2017-05-10,12
mart,2017-04-11,75
neil,2017-06-12,80
mart,2017-04-13,94

5)创建 hive 表并导入数据

hive (hive3)> create table business(name string,orderdate string,cose int) row format delimited fields terminated by ',';
OK
Time taken: 0.127 seconds
hive (hive3)> load data local inpath '/home/root/hive/business.txt' into table business;
Loading data to table hive3.business
OK
Time taken: 0.31 seconds

6)按需求查询数据

(1)查询在2017 年4 月份购买过的顾客及总人数

#先找出2017-04购物的人员信息
hive (hive3)> select * from business where substring(orderdate,0,7)= '2017-04';
business.name  business.orderdate  business.cose
jack 2017-04-06 42
mart 2017-04-08 62
mart 2017-04-09 68
mart 2017-04-11 75
mart 2017-04-13 94
Time taken: 0.298 seconds, Fetched: 5 row(s)
#直接使用over
select name,count(*) over ()
from business
where substring(orderdate,1,7)= '2017-04'
group by name;

这里的over()可以理解为进行赋值,如果()里面没有值,那么就按照前面字段的值对每一行进行赋值。

(2)将每个顾客的 cost 按照日期进行累加

方法一:再over里面group by

hive (hive3)> select name,orderdate,cose,sum(cose) over(partition by name order by orderdate) from business;
Query ID = root_20211220101500_4d0b26bf-504a-4fac-941d-aef9c90861ae
name  orderdate  cose  sum_window_0
jack 2017-01-01 10 10
jack 2017-01-05 46 56
jack 2017-01-08 55 111
jack 2017-02-03 23 134
jack 2017-04-06 42 176
mart 2017-04-08 62 62
mart 2017-04-09 68 130
mart 2017-04-11 75 205
mart 2017-04-13 94 299
neil 2017-05-10 12 12
neil 2017-06-12 80 92
tony 2017-01-02 15 15
tony 2017-01-04 29 44
tony 2017-01-07 50 94
Time taken: 30.275 seconds, Fetched: 14 row(s)
select name,orderdate,cost,
sum(cost) over() as sample1,--所有行相加
sum(cost) over(partition by name) as sample2,--按 name 分组,组内数据相加
sum(cost) over(partition by name order by orderdate) as sample3,--按 name
分组,组内数据累加
sum(cost) over(partition by name order by orderdate rows between 
UNBOUNDED PRECEDING and current row ) as sample4 ,--和 sample3 一样,由起点到
当前行的聚合
sum(cost) over(partition by name order by orderdate rows between 1 
PRECEDING and current row) as sample5,--当前行和前面一行做聚合
sum(cost) over(partition by name order by orderdate rows between 1 
PRECEDING AND 1 FOLLOWING ) as sample6,--当前行和前边一行及后面一行
sum(cost) over(partition by name order by orderdate rows between current 
row and UNBOUNDED FOLLOWING ) as sample7 --当前行及后面所有行
from business;

rows 必须跟在 order by 子句之后,对排序的结果进行限制,使用固定的行数来限制分区中的数据行数量。

(3)查看顾客上次的购买时间

hive (hive3)> select name,orderdate,lag(orderdate,1) over(partition by name order by orderdate) from business;
Query ID = root_20211220110225_469d7d71-0dc7-4e34-be38-466b10dda527
OK
name  orderdate  lag_window_0
jack 2017-01-01  NULL
jack 2017-01-05 2017-01-01
jack 2017-01-08 2017-01-05
jack 2017-02-03 2017-01-08
jack 2017-04-06 2017-02-03
mart 2017-04-08  NULL
mart 2017-04-09 2017-04-08
mart 2017-04-11 2017-04-09
mart 2017-04-13 2017-04-11
neil 2017-05-10  NULL
neil 2017-06-12 2017-05-10
tony 2017-01-02  NULL
tony 2017-01-04 2017-01-02
tony 2017-01-07 2017-01-04
Time taken: 28.747 seconds, Fetched: 14 row(s)

(4)查询前20%时间的订单信息

hive (hive3)> select name,orderdate,cose,ntile(5) over(order by orderdate) groupId from business;
name  orderdate  cose  groupid
jack 2017-01-01 10 1
tony 2017-01-02 15 1
tony 2017-01-04 29 1
jack 2017-01-05 46 2
tony 2017-01-07 50 2
jack 2017-01-08 55 2
jack 2017-02-03 23 3
jack 2017-04-06 42 3
mart 2017-04-08 62 3
mart 2017-04-09 68 4
mart 2017-04-11 75 4
mart 2017-04-13 94 4
neil 2017-05-10 12 5
neil 2017-06-12 80 5
Time taken: 27.289 seconds, Fetched: 14 row(s)
select name,orderdate,cose
 from 
(select name,orderdate,cose,ntile(5) over(order by orderdate) groupId from business) t1
 where t1.groupId = 1;
#获取前20%
 hive (hive3)> select name,orderdate,cose
>  from 
> (select name,orderdate,cose,ntile(5) over(order by orderdate) groupId from business) t1
>  where t1.groupId = 1;
Query ID = root_20211220112433_9da80326-137a-47db-9390-03fcbdaa2e23
OK
name  orderdate  cose
jack 2017-01-01 10
tony 2017-01-02 15
tony 2017-01-04 29
Time taken: 28.922 seconds, Fetched: 3 row(s)

2.5 Rank

1)函数说明

RANK()排序相同时会重复,总数不会变

DENSE_RANK()排序相同时会重复,总数会减少

ROW_NUMBER()会根据顺序计算

2)数据准备

孙悟空语文 87
孙悟空数学 95
孙悟空英语 68
大海语文 94
大海数学 56
大海英语 84
宋宋语文 64
宋宋数学 86
宋宋英语 84
婷婷语文 65
婷婷数学 85
婷婷英语 78

3)需求

计算每门学科成绩排名。

4)创建本地 score.txt,导入数据

[root@hadoop100 hive]$ vim score.txt
孙悟空语文 87
孙悟空数学 95
孙悟空英语 68
大海语文 94
大海数学 56
大海英语 84
宋宋语文 64
宋宋数学 86
宋宋英语 84
婷婷语文 65
婷婷数学 85
婷婷英语 78

5)创建 hive 表并导入数据

hive (hive3)> create table score(name string,subject string,score int)row format delimited fields terminated by '';
OK
Time taken: 5.288 seconds
hive (hive3)> load data local inpath '/home/root/hive/score.txt' into table score;
Loading data to table hive3.score
OK
Time taken: 3.227 seconds

6)按需求查询数据

hive (hive3)> select *,rank() over(partition by subject order by score desc) from score;
score.name  score.subject  score.score  rank_window_0
大海数学 56 1
婷婷数学 85 2
宋宋数学 86 3
孙悟空数学 95 4
孙悟空英语 68 1
婷婷英语 78 2
宋宋英语 84 3
大海英语 84 3
宋宋语文 64 1
婷婷语文 65 2
孙悟空语文 87 3
大海语文 94 4
Time taken: 63.935 seconds, Fetched: 12 row(s)
扩展:求出每门学科前三名的学生?

hive (hive3)> select *
> from
> (select *,rank() over(partition by subject order by score) tk from score) t1
> where tk<=3;
Query ID = root_20211220154153_5acd7d2b-d9aa-479c-8bf0-dfedfdc517db
OK
t1.name t1.subject  t1.score  t1.tk
大海数学56 1
婷婷数学85 2
宋宋数学86 3
孙悟空英语68 1
婷婷英语78 2
大海英语84 3
宋宋英语84 3
宋宋语文64 1
婷婷语文65 2
孙悟空语文87 3
Time taken: 68.69 seconds, Fetched: 10 row(s)