美文网首页
HIVE函数练习

HIVE函数练习

作者: AceCream佳 | 来源:发表于2019-10-30 11:42 被阅读0次

单纯的练习,是从尚硅谷大数据课程之Hive(2019新版)学的,反正我记录的都是我手敲过的。。。

1.空字段赋值

函数说明:

NVL:给值为null的数据赋值。格式是NVL(string1 , replace with)。如果string1为null,则NVL函数返回replace_with的值,否则返回string1的值。如果两个参数都为null,则返回null

例如:

> select nvl(comm,0) from emp;

2.时间类

1.date_format 格式化时间

> select date_format('1985-5-23','yyyy-MM-dd');

2.date_add 时间跟天数相加

> select date_add('1985-5-23',6);

可以填负值

3.date_sub 时间跟天数相减

> select date_sub('1985-5-23',6);

4.datediff 两天相减

> select datediff('1985-05-23','1985-05-26');

3.CASE WHEN

1.数据准备

name dept_id sex

悟空 A 男

大海 A 男

松松 B 男

凤姐 A 女

婷姐 B 女

婷婷 B 女

2.需求

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

A 2 1

B 1 2

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


> create table emp_sex( name string, dept_id string, sex string) row format delimited fields terminated by '\t';

> load data local inpath '/opt/soft/files/emp_sex.txt' into table emp_sex;

4.按需求查数据

> select dept_id, sum (case sex when '男' then 1 else 0 end) male_count, sum (case sex when '女' then 1 else 0 end) female_count from emp_sex group by dept_id;

select dept_id, sum (sum(sex='男',1,0)) male_count, sum (sum(sex='女',1,0) female_count from emp_sex group by dept_id;

4.行转列

1.函数说明:

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

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

concat_set(col):函数只接受基本数据类型,主要功能是将某字段的值进行去重,汇总,产生arry类型字段。

数据准备:

name constellation blood_type

孙悟空 白羊座 A

大海 射手座 A

松松 白羊座 B

猪八戒 白羊座 A

凤姐 射手座 A

2.需求

星座血型一样的人归类到一起。结果要求:

射手座,A 大海|凤姐

白羊座,A 孙悟空|猪八戒

白羊座,B 松松

3.创建本地数据constellation.txt

create table person_info( name string, constellation string, blood_type string) row format delimited fields terminated by '\t';

load data local inpath "/opt/soft/files/constellation.txt" into table person_info;

4.查

select constellation_blood_type,concat_ws('|',collect_set(name)) from (select concat(constellation,',',blood_type) constellation_blood_type, name from person_info) t1 group by constellation_blood_type;

5.列转行

1.函数说明

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

LATERAL VIEW

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

解释:用户和split,explode等UDTF一起使用,能将一列数据拆成多行数据,在此基础上可以做数据的聚合。

2.需求

电影如下:

movie category

《疑犯追踪》 悬疑,动作,科幻,剧情

《Lie to me》 悬疑,警匪,动作,心理,剧情

《战狼》 战争,动作,灾难

我们要把电影分类的数组数据展开,结果如下:

《疑犯追踪》 悬疑

《疑犯追踪》 动作

《疑犯追踪》 科幻

《疑犯追踪》 剧情

《Lie to me》 悬疑

《Lie to me》 警匪

《Lie to me》 动作

《Lie to me》 心理

《Lie to me》 剧情

《战狼》 战争

《战狼》 动作

《战狼》 灾难

3.创建本地

create table movie_info( > movie string, > category array<string>) > row format delimited fields terminated by '\t' > collection items terminated by ',';

load data local inpath "/opt/soft/files/movie.txt" into table movie_info;

4.查

select movie,category_name from movie_info lateral view explode(category) table_tmp as category_name;

6.窗口函数

1.相关函数

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

CURRENT ROW:当前行

n PRECEDING:往前n行数据

n FOLLOWING:往后n行数据

UNBOUNDED:起点,UNBOUNDED PRECEDING 表示从前面的起点,UNBOUNDED FOLLOWING表示到后面的终点。

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

LEAD(col,n):往后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

jack,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月份购买过的顾客及总人数

select name,count(*) over() from business where substring(orderdate,1,7)='2017-04' group by name;

over()开窗函数,跟在聚合函数后面,等于开一部分数据集,专门给聚合函数使用

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

select * ,sum(cost) over() from business;

3.上述场景,要将cost按照日期进行累加

select name,orderdate,sum(cost) over(distribute by name sort by orderdate) from business;

4.查询顾客上次的购买时间

select name,orderdate,cost,lag(orderdate,1,'1970-01-01') over(distribute by name sort by orderdate) from business;

7.RANK

1.函数说明

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

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

ROW_NUMBER() 会根据顺序计算

数据:

孙悟空 语文 87

孙悟空 数学 95

孙悟空 英语 68

大海 语文 94

大海 数学 56

大海 英语 84

松松 语文 64

松松 数学 86

松松 英语 84

婷婷 语文 65

婷婷 数学 85

婷婷 英语 78

看效果:

select name,subject,score, rank() over(partition by subject order by score desc) rank1, 
  dense_rank() over(partition by subject order by score desc) rank2, 
    row_number() over(partition by subject order by score desc) rank3 
from score;

结果:


rank效果

其他的结果就不展示了,因为我虚拟机搭的大数据集群是在是太慢了。。。

create语句和load也不详细写了

练习题

1.我们有如下用户访问数据

userId visitDate visitCount

u01 2019/1/21 5

u02 2019/1/23 6

u03 2019/1/22 8

u04 2019/1/20 3

u01 2019/1/23 6

u01 2019/2/21 8

u02 2019/1/23 6

u01 2019/2/22 4

要求使用SQL统计出每个用户的累计访问次数,如下表:

用户 月份 小计 累计

u01 2017-01 11 11

u01 2017-02 12 23

u02 2017-01 12 12

u03 2017-01 8 8

u04 2017-01 3 3

创建表:

create table action(userId string,visitDate string,visitCount int) row format delimited fields terminated by "\t";
load data local inpath '/opt/soft/files/visit.txt' into table action;

解:

select userId, mn, sum_visitCount, sum(sum_visitCount) over(partition by userId order by mn) from (select userId, mn, sum(visitCount) sum_visitCount from (select userId, date_format(regexp_replace(visitDate,'/','-'),"yyyy-MM") mn, visitCount from action)t1 group by userId,mn) t2;

效果:

hive习题1结果.png

这个题有点难搞嗷!!

2.京东

有50w个京东店铺,每个顾客访问任何一个店铺的任何一个商品时,都会产生一条访问日志,访问日志存储的表名为visit,访客用户id为user_id,被访问的店铺名称为shop。请统计:

数据:

u1 a

u2 b

u1 b

u1 a

u3 c

u4 b

u1 a

u2 c

u5 b

u4 b

u6 c

u2 c

u1 a

u2 a

u2 a

u3 a

u5 a

u5 a

u5 a

需求:

1.每个店铺UV(访客数)

2.每个店铺访问次数top3的访客信息。输出店铺名、访客id、访问次数。

建表:

create table visit(user_id string,shop string) row format delimited fields terminated by '\t';
load data local inpath '/opt/soft/files/jd.txt' into table visit;

解题:

Q1:

方法1:

select shop,count(distinct user_id) UV from visit group by shop;

方法2:(优)

select shop,count(*) UV from (select shop,user_id from visit group by shop,user_id)t1 group by shop;

结果:

hive习题2-1.png

Q2:

select shop, user_id, ct from (select shop, user_id, ct, row_number() over(partition by shop order by ct desc) rk from (select shop, user_id, count(*) ct from visit group by shop,user_id)t1) t2 where rk<=3

结果:


hive习题2-2.png

相关文章

  • HIVE函数练习

    单纯的练习,是从尚硅谷大数据课程之Hive(2019新版)学的,反正我记录的都是我手敲过的。。。 1.空字段赋值 ...

  • 大数据开发之Hive篇6-Hive函数详解

    备注:Hive 版本 2.1.1 一.Hive函数概述 1.1 Hive函数分类 函数/UDF输入一行记录,输出一...

  • Hive 自定义函数

    系统内置函数 查看系统内置函数hive> show functions;显示内置函数用法hive> desc fu...

  • Hive函数

    hive中的函数从大体上可以分为两类:hive内置函数和用户自定义函数 hive内置函数又可以根据输入输出细分为以...

  • Hive自定义UDF UDAF UDTF

    hive允许用户使用自定义函数解决hive 自带函数无法处理的逻辑。hive自定义函数只在当前线程内临时有效,可以...

  • HIVE UDTF 自定义函数

    HIVE UDTF 自定义函数 关键词:HIVE UDTF 开发 实例Hive运行用户自定义函数对数据信息处理,...

  • Hive sql常见操作

    基本sql操作 hive表操作 分区操作 Hive内置函数 (1)数学函数 常用的数学函数都有:round、flo...

  • Hive SQL 窗口函数

    本文首发:大数据每日哔哔-Hive SQL 窗口函数 Hive 的窗口函数 在 SQL 中有一类函数叫做聚合函数,...

  • flink modules详解之使用hive函数

    modules概念 通过hive module使用hive函数内置函数自定义函数 sql 客户端的使用 原理分析和...

  • 51cto赵强HADOOP学习(十七)hive的函数

    hive的内置函数

网友评论

      本文标题:HIVE函数练习

      本文链接:https://www.haomeiwen.com/subject/qjrovctx.html