Hive的sql语句练习 如果有时间看

(九)Hive的5个面试题

目录

正文

回到顶部

一、求单月访问次数和总访问次数

1、数据说明

数据字段说明

用户名,月份,访问次数

数据格式

  1. A,2015-01,5
  2. A,2015-01,15
  3. B,2015-01,5
  4. A,2015-01,8
  5. B,2015-01,25
  6. A,2015-01,5
  7. A,2015-02,4
  8. A,2015-02,6
  9. B,2015-02,10
  10. B,2015-02,5
  11. A,2015-03,16
  12. A,2015-03,22
  13. B,2015-03,23
  14. B,2015-03,10
  15. B,2015-03,1

2、数据准备

(1)创建表

  1. use myhive;
  2. create external table if not exists t_access(
  3. uname string comment '用户名',
  4. umonth string comment '月份',
  5. ucount int comment '访问次数'
  6. ) comment '用户访问表'
  7. row format delimited fields terminated by ","
  8. location "/hive/t_access";

(2)导入数据

load data local inpath "/home/hadoop/access.txt" into table t_access;

(3)验证数据

select * from t_access;

3、结果需求

现要求出:每个用户截止到每月为止的最大单月访问次数和累计到该月的总访问次数,结果数据格式如下

4、需求分析

此结果需要根据用户+月份进行分组

(1)先求出当月访问次数

  1. --求当月访问次数
  2. create table tmp_access(
  3. name string,
  4. mon string,
  5. num int
  6. );
  7. insert into table tmp_access
  8. select uname,umonth,sum(ucount)
  9. from t_access t group by t.uname,t.umonth;
  10. select * from tmp_access;

(2)tmp_access进行自连接视图

  1. create view tmp_view as
  2. select a.name anme,a.mon amon,a.num anum,b.name bname,b.mon bmon,b.num bnum from tmp_access a join tmp_access b
  3. on a.name=b.name;
select * from tmp_view;

(3)进行比较统计

  1. select anme,amon,anum,max(bnum) as max_access,sum(bnum) as sum_access
  2. from tmp_view
  3. where amon>=bmon
  4. group by anme,amon,anum;

回到顶部

二、学生课程成绩

 1、说明

  1. use myhive;
  2. CREATE TABLE `course` (
  3. `id` int,
  4. `sid` int ,
  5. `course` string,
  6. `score` int
  7. ) ;
  1. // 插入数据
  2. // 字段解释:id, 学号, 课程, 成绩
  3. INSERT INTO `course` VALUES (1, 1, 'yuwen', 43);
  4. INSERT INTO `course` VALUES (2, 1, 'shuxue', 55);
  5. INSERT INTO `course` VALUES (3, 2, 'yuwen', 77);
  6. INSERT INTO `course` VALUES (4, 2, 'shuxue', 88);
  7. INSERT INTO `course` VALUES (5, 3, 'yuwen', 98);
  8. INSERT INTO `course` VALUES (6, 3, 'shuxue', 65);

复制代码

2、需求

求:所有数学课程成绩 大于 语文课程成绩的学生的学号

1、使用case...when...将不同的课程名称转换成不同的列

  1. create view tmp_course_view as
  2. select sid, case course when "shuxue" then score else 0 end as shuxue,
  3. case course when "yuwen" then score else 0 end as yuwen from course;
select * from tmp_course_view;

 

2、以sid分组合并取各成绩最大值

  1. create view tmp_course_view1 as
  2. select aa.sid, max(aa.shuxue) as shuxue, max(aa.yuwen) as yuwen from tmp_course_view aa group by sid;
select * from tmp_course_view1;

3、比较结果

select * from tmp_course_view1 where shuxue > yuwen;

回到顶部

三、求每一年最大气温的那一天  + 温度

 1、说明

数据格式

2010012325

具体数据

 View Code

数据解释

2010012325表示在2010年01月23日的气温为25度

2、 需求

比如:2010012325表示在2010年01月23日的气温为25度。现在要求使用hive,计算每一年出现过的最大气温的日期+温度。要计算出每一年的最大气温。我用select substr(data,1,4),max(substr(data,9,2)) from table2 group by substr(data,1,4);出来的是 年份 + 温度 这两列数据例如 2015 99

但是如果我是想select 的是:具体每一年最大气温的那一天 + 温度 。例如 20150109 99请问该怎么执行hive语句。。group by 只需要substr(data,1,4),但是select substr(data,1,8),又不在group by 的范围内。 是我陷入了思维死角。一直想不出所以然。。求大神指点一下。在select 如果所需要的。不在group by的条件里。这种情况如何去分析?

3、解析

(1)创建一个临时表tmp_weather,将数据切分

  1. create table tmp_weather as
  2. select substr(data,1,4) years,substr(data,5,2) months,substr(data,7,2) days,substr(data,9,2) temp from weather;
select * from tmp_weather;

(2)创建一个临时表tmp_year_weather

  1. create table tmp_year_weather as
  2. select substr(data,1,4) years,max(substr(data,9,2)) max_temp from weather group by substr(data,1,4);
select * from tmp_year_weather;

(3)将2个临时表进行连接查询

select * from tmp_year_weather a join tmp_weather b on a.years=b.years and a.max_temp=b.temp;

回到顶部

四、求学生选课情况

回到顶部

1、数据说明

(1)数据格式

  1. id course
  2. 1,a
  3. 1,b
  4. 1,c
  5. 1,e
  6. 2,a
  7. 2,c
  8. 2,d
  9. 2,f
  10. 3,a
  11. 3,b
  12. 3,c
  13. 3,e

(2)字段含义

表示有id为1,2,3的学生选修了课程a,b,c,d,e,f中其中几门。

回到顶部

2、数据准备

(1)建表t_course

create table t_course(id int,course string)
row format delimited fields terminated by ",";

(2)导入数据

load data local inpath "/home/hadoop/course/course.txt" into table t_course;

回到顶部

3、需求

编写Hive的HQL语句来实现以下结果:表中的1表示选修,表中的0表示未选修

  1. id a b c d e f
  2. 1 1 1 1 0 1 0
  3. 2 1 0 1 1 0 1
  4. 3 1 1 1 0 1 0

回到顶部

4、解析

第一步:

select collect_set(course) as courses from id_course;

第二步:

  1. set hive.strict.checks.cartesian.product=false;
  2. create table id_courses as select t1.id as id,t1.course as id_courses,t2.course courses
  3. from
  4. ( select id as id,collect_set(course) as course from id_course group by id ) t1
  5. join
  6. (select collect_set(course) as course from id_course) t2;

启用严格模式:hive.mapred.mode = strict // Deprecatedhive.strict.checks.large.query = true该设置会禁用:1. 不指定分页的orderby         2. 对分区表不指定分区进行查询          3. 和数据量无关,只是一个查询模式

hive.strict.checks.type.safety = true严格类型安全,该属性不允许以下操作:1. bigint和string之间的比较                  2. bigint和double之间的比较

hive.strict.checks.cartesian.product = true该属性不允许笛卡尔积操作

第三步:得出最终结果:思路:拿出course字段中的每一个元素在id_courses中进行判断,看是否存在。

  1. select id,
  2. case when array_contains(id_courses, courses[0]) then 1 else 0 end as a,
  3. case when array_contains(id_courses, courses[1]) then 1 else 0 end as b,
  4. case when array_contains(id_courses, courses[2]) then 1 else 0 end as c,
  5. case when array_contains(id_courses, courses[3]) then 1 else 0 end as d,
  6. case when array_contains(id_courses, courses[4]) then 1 else 0 end as e,
  7. case when array_contains(id_courses, courses[5]) then 1 else 0 end as f
  8. from id_courses;

回到顶部

五、求月销售额和总销售额

1、数据说明

(1)数据格式

  1. a,01,150
  2. a,01,200
  3. b,01,1000
  4. b,01,800
  5. c,01,250
  6. c,01,220
  7. b,01,6000
  8. a,02,2000
  9. a,02,3000
  10. b,02,1000
  11. b,02,1500
  12. c,02,350
  13. c,02,280
  14. a,03,350
  15. a,03,250

(2)字段含义

店铺,月份,金额

2、数据准备

(1)创建数据库表t_store

  1. use class;
  2. create table t_store(
  3. name string,
  4. months int,
  5. money int
  6. )
  7. row format delimited fields terminated by ",";

(2)导入数据

load data local inpath "/home/hadoop/store.txt" into table t_store;

3、需求

编写Hive的HQL语句求出每个店铺的当月销售额和累计到当月的总销售额

4、解析

(1)按照商店名称和月份进行分组统计

create table tmp_store1 as 
select name,months,sum(money) as money from t_store group by name,months;

select * from tmp_store1;

(2)对tmp_store1 表里面的数据进行自连接

  1. create table tmp_store2 as
  2. select a.name aname,a.months amonths,a.money amoney,b.name bname,b.months bmonths,b.money bmoney from tmp_store1 a
  3. join tmp_store1 b on a.name=b.name order by aname,amonths;
select * from tmp_store2;

(3)比较统计

select aname,amonths,amoney,sum(bmoney) as total from tmp_store2 where amonths >= bmonths group by aname,amonths,amoney;

第一题 需求 我们有如下的用户访问数据 userId visitDate visitCount u01 2017/1/21 5 u02 2017/1/23 6 u03 2017/1/22 8 u04 2017/1/20 3 u01 2017/1/23 6 u01 2017/2/21 8 U02 2017/1/23 6 U01 2017/2/22 ...
有以下一份数据: A,2015-01,5 A,2015-01,15 B,2015-01,5 A,2015-01,8 B,2015-01,25 A,2015-01,5 A,2015-02,4 A,2015-02,6 B,2015-02,10 B,2015-02,5 A,2015-03,16 A,2015-03,22 B,2015-03,23 B,2015-03,10 B,2015-03,11 ...

  • wangping623
    我能想到的:这些题目非常有意思,就是第1,2,3,5题的解法实在太不合理了,本来使用窗口函数很容易搞定的。第4题学生选课情况的解法刚好,这个问题的使用场景非常普遍,像视频热度统计、商品订单统计都有类似的场景。1年前回复
  • lvpassing
    Lv_小诚:## 第一题:可以用hive窗口函数
    1. -- 建表语句
    2. create external table if not exists access(
    3. name string comment '用户名',
    4. month string comment '月份',
    5. count int comment '访问次数'
    6. ) comment '用户访问表'
    7. row format delimited
    8. fields terminated by ","
    9. location "/user/test/access";
    10. -- 一条sql搞定
    11. select
    12. t.name,
    13. t.month,
    14. t.total,
    15. max(t.total) over(partition by t.name order by t.month) max_mth,
    16. sum(t.total) over(partition by t.name order by t.month) total_mth
    17. from (
    18. select
    19. name,
    20. month,
    21. sum(count) total
    22. from access
    23. group by name, month
    24. order by name, month
    25. ) t
    26. ;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值