Hive练习-影评案例

通过Hive进行影评数据的实战练习,包括与电影表的链接查询、评分统计、用户性别与评分结合分析、年龄分段统计等多维度问题解答,涉及多步骤的SQL操作。
摘要由CSDN通过智能技术生成

题目:

现有如此三份数据:
1、users.dat    数据格式为:  2::M::56::16::70072
对应字段为:UserID BigInt, Gender String, Age Int, Occupation String, Zipcode String
对应字段中文解释:用户id,性别,年龄,职业,邮政编码

2、movies.dat		数据格式为: 2::Jumanji (1995)::Adventure|Children's|Fantasy
对应字段为:MovieID BigInt, Title String, Genres String
对应字段中文解释:电影ID,电影名字,电影类型

3、ratings.dat		数据格式为:  1::1193::5::978300760
对应字段为:UserID BigInt, MovieID BigInt, Rating Double, Timestamped String
对应字段中文解释:用户ID,电影ID,评分,评分时间戳

题目要求:

数据要求:
(1)写shell脚本清洗数据。(hive不支持解析多字节的分隔符,也就是说hive只能解析':', 不支持解析'::',所以用普通方式建表来使用是行不通的,要求对数据做一次简单清洗)
(2)使用Hive能解析的方式进行

Hive要求:
(1)正确建表,导入数据(三张表,三份数据),并验证是否正确
(2)求被评分次数最多的10部电影,并给出评分次数(电影名,评分次数)
(3)分别求男性,女性当中评分最高的10部电影(性别,电影名,影评分)
(4)求movieid = 2116这部电影各年龄段(因为年龄就只有7个,就按这个7个分就好了)的平均影评(年龄段,影评分)
(5)求最喜欢看电影(影评次数最多)的那位女性评最高分的10部电影的平均影评分(观影者,电影名,影评分)
(6)求好片(评分>=4.0)最多的那个年份的最好看的10部电影
(7)求1997年上映的电影中,评分最高的10部Comedy类电影
(8)该影评库中各种类型电影中评价最高的5部电影(类型,电影名,平均影评分)
(9)各年评分最高的电影类型(年份,类型,影评分)
(10)每个地区最高评分的电影名,把结果存入HDFS(地区,电影名,影评分)
第一题:
(1)正确建表,导入数据(三张表,三份数据),并验证是否正确
建立users表
create table users(UserID BigInt,Gender String, Age Int,Occupation String,
Zipcode String) 
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' 
with serdeproperties('input.regex'='(.*)::(.*)::(.*)::(.*)::(.*)','
output.format.string'='%1$s %2$s %3$s %4$s %5$s') 
stored as textfile;


导入数据
load data local inpath '/home/potter/users.dat' into table users;

结果:
hive> select * from users limit 5;
OK
1       F       1       10      48067
2       M       56      16      70072
3       M       25      15      55117
4       M       45      7       02460
5       M       25      20      55455
建立movies表
create table movies(MovieID BigInt,Title String,Genres String) 
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' 
with serdeproperties('input.regex'='(.*)::(.*)::(.*)','
output.format.string'='%1$s %2$s %3$s')
stored as textfile;

导入数据
load data local inpath '/home/potter/movies.dat' into table movies;

结果:
hive> select * from movies limit 5;
OK
1       Toy Story (1995)        Animation|Children's|Comedy
2       Jumanji (1995)  Adventure|Children's|Fantasy
3       Grumpier Old Men (1995) Comedy|Romance
4       Waiting to Exhale (1995)        Comedy|Drama
5       Father of the Bride Part II (1995)      Comedy
建立ratings表
create table ratings(UserID BigInt,MovieID BigInt,Rating Double,Timestamped String)
row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe' 
with serdeproperties('input.regex'='(.*)::(.*)::(.*)::(.*)','
output.format.string'='%1$s %2$s %3$s %4$s') 
stored as textfile;

导入数据
load data local inpath '/home/potter/ratings.dat' into table ratings;

结果:
hive> select * from ratings limit 5;
OK
1       1193    5.0     978300760
1       661     3.0     978302109
1       914     3.0     978301968
1       3408    4.0     978300275
1       2355    5.0     978824291
第二题:

思路:评分表ratings和电影表做链接查询,然后按照电影分组,计评分次数,按次数降序排,取前10条数据

(2)求被评分次数最多的10部电影,并给出评分次数(电影名,评分次数)
select count(r.userid) c ,m.title from movies m join ratings r on m.movieid=r.movieid 
group by m.title order by c desc limit 10;

结果:
3428    American Beauty (1999)
2991    Star Wars: Episode IV - A New Hope (1977)
2990    Star Wars: Episode V - The Empire Strikes Back (1980)
2883    Star Wars: Episode VI - Return of the Jedi (1983)
2672    Jurassic Park (1993)
2653    Saving Private Ryan (1998)
2649    Terminator 2: Judgment Day (1991)
2590    Matrix, The (1999)
2583    Back to the Future (1985)
2578    Silence of the Lambs, The (1991)
Time taken: 324.57 seconds, Fetched: 10 row(s)
第三题:

思路:评价最高,就是评分最高,然后再分男女,以评分表为主表,链接电影表movies查询电影名,链接用户名,确定用户性别

(3)分别求男性,女性当中评分最高的10部电影(性别,电影名,影评分)
select avg(r.rating) rr, u.gender ,m.tit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值