题目:
现有如此三份数据:
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