暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

数仓面试之Hive SQL案例

大数据研习社 2021-05-26
486

长按二维码关注

大数据领域必关注的公众号

By大数据研习社

概要:Hive SQL是从事大数据分析同学的基本功,也是数仓建设的重要工具。无论是秋招、春招或者是实习,Hive SQL都是面试官考察的重点。

关键词:数仓、Hive、面试、开窗函数


1 需求分析

统计b站视频观看数topn

统计b站视频分类热度topn

统计b站每个类别视频观看数topn(开窗函数)

统计b站视频不同评分等级的视频数(列转行)

统计上传b站视频最多的用户Top10,以及这些用户上传的视频观看次数在前10的视频


2 数据结构

2.1用户表(user)



2.2视频表(video)



3 准备工作


3.1 创建数据库

create database  myvideo;


3.2 创建原始数据表

创建用户原始表user_orignal

create table  if not exists  user_orignal

(uid int,

name string,

regtime string,

visitnum int,

lastvisit string,

gender int,

birthday string,

country string,

province string,

city string,

uploadvideos int)

row format delimited fields terminated by ","

stored as textfile;


创建原始视频表video_orignal

create table  if not exists  video_orignal

(vid string,

uid int,

vday int,

vtype string,

vlength int,

visit int,

score int,

comments int,

collection int,

fabulous int,

forward int)

row format delimited fields terminated by ","

stored as textfile;


3.3 创建orc+Snappy数据表

创建用户表user_orc

create table  if not exists  user_orc

(uid int,

name string,

regtime string,

visitnum int,

lastvisit string,

gender int,

birthday string,

country string,

province string,

city string,

uploadvideos int)

row format delimited fields terminated by ","

stored as orc

tblproperties("orc.compress"="SNAPPY");


创建视频表video_orc

create table  if not exists  video_orc

(vid string,

uid int,

vday int,

vtype string,

vlength int,

visit int,

score int,

comments int,

collection int,

fabulous int,

forward int)

row format delimited fields terminated by ","

stored as orc

tblproperties("orc.compress"="SNAPPY");


3.4 数据加载到原始表

load data local inpath "/home/hadoop

/shell/data/user.txt" into table user_orignal;

load data local inpath "/home/hadoop/

shell/data/video.txt" into table video_orignal;


3.5 数据加载到orc+Snappy

insert into table user_orc select * from user_orignal;

insert into table video_orc select * from video_orignal;


4 业务分析

4.1 统计b站视频观看数topn

select vid,visit from video_orc order by visit desc limit 10;


4.2 统计b站视频分类热度topn

select vtype,count(vid) hot from video_

orc group by vtype

order by hot desc limit 10;


4.3 统计每个类别视频观看数topn

select v.vtype,v.vid,v.visit from  

(select vtype,vid,visit,rank() over(part-

ition by vtype order by visit desc) rk from video_orc) v where rk<=3;


分析函数:用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返回多行,而聚合函数对于每个组只返回一行。


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


备注:

排序函数rank():在每个分组类进行排名。

开窗函数over(partition by vtype order by visit):按照vtype分区,在一个分区内按照visit排序。


4.4 统计b站视频不同评分等级(列转行)的视频数

select 

max(case v.score when 1 then v.num else 0 end) 1star,

max(case v.score when 2 then v.num else 0 end) 2star,

max(case v.score when 3 then v.num else 0 end) 3star,

max(case v.score when 4 then v.num else 0 end) 4star,

max(case v.score when 5 then v.num else 0 end) 5star

from 

(select score,count(*) as num from video_orc group by score) v;


4.5 统计上传b站视频最多的用户Top10,以及这些用户上传的视频观看次数在前10的视频

select v.vid,v.visit,v.uid from

(select uid,uploadvideos from user_orc order by uploadvideos desc limit 10) u join video_orc v on u.uid=v.uid order by v.visit desc limit 10;


欢迎点赞 + 收藏 + 在看  素质三连 


往期精彩回顾
程序员,如何避免内卷
【全网首发】Hadoop 3.0分布式集群安装
【2020最新整理】大数据面试130题
某集团大数据平台整体架构及实施方案完整目录
大数据平台基础架构指南
大数据凉凉了?Apache将一众大数据开源项目束之高阁!
实战企业数据湖,抢先数仓新玩法
Superset制作智慧数据大屏,看它就够了

Apache Flink 在快手的过去、现在和未来

大数据基础运维:HDFS参数调优

大数据无处不在,向左还是向右

【HBase调优】Hbase万亿级存储性能优化总结
【Python精华】100个Python练手小程序
【HBase企业应用开发】工作中自己总结的Hbase笔记,非常全面!
【剑指Offer】近50个常见算法面试题的Java实现代码

     长按识别左侧二维码

         关注领福利    

      领10本经典大数据书

文章转载自大数据研习社,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论