Hive學習之路 (十二)Hive SQL練習之影評案例


案例說明

現有如此三份數據:
1、users.dat 數據格式為: 2::M::56::16::70072,

共有6040條數據
對應字段為:UserID BigInt, Gender String, Age Int, Occupation String, Zipcode String
對應字段中文解釋:用戶id,性別,年齡,職業,郵政編碼

2、movies.dat 數據格式為: 2::Jumanji (1995)::Adventure|Children's|Fantasy,

共有3883條數據
對應字段為:MovieID BigInt, Title String, Genres String
對應字段中文解釋:電影ID,電影名字,電影類型

3、ratings.dat 數據格式為: 1::1193::5::978300760,

共有1000209條數據
對應字段為: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(地區,電影名,影評分)

數據下載

https://files.cnblogs.com/files/qingyunzong/hive%E5%BD%B1%E8%AF%84%E6%A1%88%E4%BE%8B.zip

解析

之前已經使用MapReduce程序將3張表格進行合並,所以只需要將合並之后的表格導入對應的表中進行查詢即可。

1、正確建表,導入數據(三張表,三份數據),並驗證是否正確

(1)分析需求

需要創建一個數據庫movie,在movie數據庫中創建3張表,t_user,t_movie,t_rating

t_user:userid bigint,sex string,age int,occupation string,zipcode string
t_movie:movieid bigint,moviename string,movietype string
t_rating:userid bigint,movieid bigint,rate double,times string

原始數據是以::進行切分的,所以需要使用能解析多字節分隔符的Serde即可

使用RegexSerde

需要兩個參數:
input.regex = "(.*)::(.*)::(.*)"
output.format.string = "%1$s %2$s %3$s"

(2)創建數據庫

drop database if exists movie;
create database if not exists movie;
use movie;

(3)創建t_user表

create table t_user(
userid bigint,
sex 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;

(4)創建t_movie表

use movie;
create table t_movie(
movieid bigint,
moviename string,
movietype 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;

(5)創建t_rating表

use movie;
create table t_rating(
userid bigint,
movieid bigint,
rate double,
times 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;

(6)導入數據

0: jdbc:hive2://hadoop3:10000> load data local inpath "/home/hadoop/movie/users.dat" into table t_user;
No rows affected (0.928 seconds)
0: jdbc:hive2://hadoop3:10000> load data local inpath "/home/hadoop/movie/movies.dat" into table t_movie;
No rows affected (0.538 seconds)
0: jdbc:hive2://hadoop3:10000> load data local inpath "/home/hadoop/movie/ratings.dat" into table t_rating;
No rows affected (0.963 seconds)
0: jdbc:hive2://hadoop3:10000> 

(7)驗證

select t.* from t_user t;

select t.* from t_movie t;

select t.* from t_rating t;

2、求被評分次數最多的10部電影,並給出評分次數(電影名,評分次數)

(1)思路分析:

  1、需求字段:電影名        t_movie.moviename

          評分次數    t_rating.rate          count()

  2、核心SQL:按照電影名進行分組統計,求出每部電影的評分次數並按照評分次數降序排序

(2)完整SQL:

create table answer2 as 
select a.moviename as moviename,count(a.moviename) as total 
from t_movie a join t_rating b on a.movieid=b.movieid 
group by a.moviename 
order by total desc 
limit 10;
select * from answer2;

3、分別求男性,女性當中評分最高的10部電影(性別,電影名,影評分)

 (1)分析思路:

  1、需求字段:性別  t_user.sex

          電影名 t_movie.moviename

          影評分 t_rating.rate

  2、核心SQL:三表聯合查詢,按照性別過濾條件,電影名作為分組條件,影評分作為排序條件進行查詢

(2)完整SQL:

女性當中評分最高的10部電影(性別,電影名,影評分)評論次數大於等於50次

create table answer3_F as 
select "F" as sex, c.moviename as name, avg(a.rate) as avgrate, count(c.moviename) as total  
from t_rating a 
join t_user b on a.userid=b.userid 
join t_movie c on a.movieid=c.movieid 
where b.sex="F" 
group by c.moviename 
having total >= 50
order by avgrate desc 
limit 10;
select * from answer3_F;

男性當中評分最高的10部電影(性別,電影名,影評分)評論次數大於等於50次

create table answer3_M as 
select "M" as sex, c.moviename as name, avg(a.rate) as avgrate, count(c.moviename) as total  
from t_rating a 
join t_user b on a.userid=b.userid 
join t_movie c on a.movieid=c.movieid 
where b.sex="M" 
group by c.moviename 
having total >= 50
order by avgrate desc 
limit 10;
select * from answer3_M;

 

4、求movieid = 2116這部電影各年齡段(因為年齡就只有7個,就按這個7個分就好了)的平均影評(年齡段,影評分)

(1)分析思路:

  1、需求字段:年齡段  t_user.age

          影評分 t_rating.rate

  2、核心SQL:t_user和t_rating表進行聯合查詢,用movieid=2116作為過濾條件,用年齡段作為分組條件

(2)完整SQL:

create table answer4 as 
select a.age as age, avg(b.rate) as avgrate 
from t_user a join t_rating b on a.userid=b.userid 
where b.movieid=2116 
group by a.age;
select * from answer4;

5、求最喜歡看電影(影評次數最多)的那位女性評最高分的10部電影的平均影評分(觀影者,電影名,影評分)

(1)分析思路:

  1、需求字段:觀影者 t_rating.userid

          電影名 t_movie.moviename

          影評分 t_rating.rate

  2、核心SQL:

    A.  需要先求出最喜歡看電影的那位女性

          需要查詢的字段:性別:t_user.sex

                  觀影次數:count(t_rating.userid)

    B.  根據A中求出的女性userid作為where過濾條件,以看過的電影的影評分rate作為排序條件進行排序,求出評分最高的10部電影

          需要查詢的字段:電影的ID:t_rating.movieid

    C.  求出B中10部電影的平均影評分

          需要查詢的字段:電影的ID:answer5_B.movieid

                  影評分:t_rating.rate

(2)完整SQL:

 A.  需要先求出最喜歡看電影的那位女性

select a.userid, count(a.userid) as total 
from t_rating a join t_user b on a.userid = b.userid 
where b.sex="F" 
group by a.userid 
order by total desc 
limit 1;

B.  根據A中求出的女性userid作為where過濾條件,以看過的電影的影評分rate作為排序條件進行排序,求出評分最高的10部電影

create table answer5_B as 
select a.movieid as movieid, a.rate as rate  
from t_rating a 
where a.userid=1150 
order by rate desc 
limit 10;
select * from answer5_B;

C.  求出B中10部電影的平均影評分

create table answer5_C as 
select b.movieid as movieid, c.moviename as moviename, avg(b.rate) as avgrate 
from answer5_B a 
join t_rating b on a.movieid=b.movieid 
join t_movie c on b.movieid=c.movieid 
group by b.movieid,c.moviename;
select * from answer5_C;

6、求好片(評分>=4.0)最多的那個年份的最好看的10部電影

(1)分析思路:

  1、需求字段:電影id t_rating.movieid

          電影名 t_movie.moviename(包含年份)

          影評分 t_rating.rate

          上映年份 xxx.years

  2、核心SQL:

    A.  需要將t_rating和t_movie表進行聯合查詢,將電影名當中的上映年份截取出來,保存到臨時表answer6_A中

          需要查詢的字段:電影id t_rating.movieid

                  電影名 t_movie.moviename(包含年份)

                  影評分 t_rating.rate

    B.  從answer6_A按照年份進行分組條件,按照評分>=4.0作為where過濾條件,按照count(years)作為排序條件進行查詢

          需要查詢的字段:電影的ID:answer6_A.years

    C.  從answer6_A按照years=1998作為where過濾條件,按照評分作為排序條件進行查詢

          需要查詢的字段:電影的ID:answer6_A.moviename

                  影評分:answer6_A.avgrate

(2)完整SQL:

A.  需要將t_rating和t_movie表進行聯合查詢,將電影名當中的上映年份截取出來

create table answer6_A as
select  a.movieid as movieid, a.moviename as moviename, substr(a.moviename,-5,4) as years, avg(b.rate) as avgrate
from t_movie a join t_rating b on a.movieid=b.movieid 
group by a.movieid, a.moviename;
select * from answer6_A;

B.  從answer6_A按照年份進行分組條件,按照評分>=4.0作為where過濾條件,按照count(years)作為排序條件進行查詢

select years, count(years) as total 
from answer6_A a 
where avgrate >= 4.0 
group by years 
order by total desc 
limit 1;

C.  從answer6_A按照years=1998作為where過濾條件,按照評分作為排序條件進行查詢

create table answer6_C as
select a.moviename as name, a.avgrate as rate 
from answer6_A a 
where a.years=1998 
order by rate desc 
limit 10;
select * from answer6_C;

7、求1997年上映的電影中,評分最高的10部Comedy類電影

(1)分析思路:

  1、需求字段:電影id t_rating.movieid

          電影名 t_movie.moviename(包含年份)

          影評分 t_rating.rate

         上映年份 xxx.years(最終查詢結果可不顯示)

         電影類型 xxx.type(最終查詢結果可不顯示)

  2、核心SQL:

    A.  需要電影類型,所有可以將第六步中求出answer6_A表和t_movie表進行聯合查詢

          需要查詢的字段:電影id answer6_A.movieid

                  電影名 answer6_A.moviename

                  影評分 answer6_A.rate

                  電影類型 t_movie.movietype 

                  上映年份 answer6_A.years

    B.  從answer7_A按照電影類型中是否包含Comedy和按上映年份作為where過濾條件,按照評分作為排序條件進行查詢,將結果保存到answer7_B中

          需要查詢的字段:電影的ID:answer7_A.id

                  電影的名稱:answer7_A.name

                  電影的評分:answer7_A.rate

(2)完整SQL:

A.  需要電影類型,所有可以將第六步中求出answer6_A表和t_movie表進行聯合查詢

create table answer7_A as 
select b.movieid as id, b.moviename as name, b.years as years, b.avgrate as rate, a.movietype as type 
from t_movie a join answer6_A b on a.movieid=b.movieid;
select t.* from answer7_A t;

B.  從answer7_A按照電影類型中是否包含Comedy和按照評分>=4.0作為where過濾條件,按照評分作為排序條件進行查詢,將結果保存到answer7_B中

create table answer7_B as 
select t.id as id, t.name as name, t.rate as rate 
from answer7_A t 
where t.years=1997 and instr(lcase(t.type),'comedy') >0 
order by rate desc
limit 10;
select * from answer7_B;

8、該影評庫中各種類型電影中評價最高的5部電影(類型,電影名,平均影評分)

(1)分析思路:

  1、需求字段:電影id movieid

          電影名 moviename

          影評分 rate(排序條件)

         電影類型 type(分組條件)

  2、核心SQL:

    A.  需要電影類型,所有需要將answer7_A中的type字段進行裂變,將結果保存到answer8_A中

          需要查詢的字段:電影id answer7_A.id

                  電影名 answer7_A.name(包含年份)

                  上映年份 answer7_A.years

                  影評分 answer7_A.rate

                  電影類型 answer7_A.movietype 

    B.  求TopN,按照type分組,需要添加一列來記錄每組的順序,將結果保存到answer8_B中

row_number() :用來生成 num字段的值

distribute by movietype :按照type進行分組

sort by avgrate desc :每組數據按照rate排降序

num:新列, 值就是每一條記錄在每一組中按照排序規則計算出來的排序值

    C.  從answer8_B中取出num列序號<=5的

(2)完整SQL:

A.  需要電影類型,所有需要將answer7_A中的type字段進行裂變,將結果保存到answer8_A中

create table answer8_A as 
select a.id as id, a.name as name, a.years as years, a.rate as rate, tv.type as type 
from answer7_A a 
lateral view explode(split(a.type,"\\|")) tv as type;
select * from answer8_A; 

B.  求TopN,按照type分組,需要添加一列來記錄每組的順序,將結果保存到answer8_B中

create table answer8_B as 
select id,name,years,rate,type,row_number() over(distribute by type sort by rate desc ) as num
from answer8_A;
select * from answer8_B;

C.  從answer8_B中取出num列序號<=5的

select a.* from answer8_B a where a.num <= 5;

9、各年評分最高的電影類型(年份,類型,影評分)

(1)分析思路:

  1、需求字段:電影id movieid

          電影名 moviename

          影評分 rate(排序條件)

          電影類型 type(分組條件)

         上映年份 years(分組條件)

  2、核心SQL:

    A.  需要按照電影類型和上映年份進行分組,按照影評分進行排序,將結果保存到answer9_A中

          需要查詢的字段:

                  上映年份 answer7_A.years

                  影評分 answer7_A.rate

                  電影類型 answer7_A.movietype 

    B.  求TopN,按照years分組,需要添加一列來記錄每組的順序,將結果保存到answer9_B中

    C.  按照num=1作為where過濾條件取出結果數據

(2)完整SQL:

A.  需要按照電影類型和上映年份進行分組,按照影評分進行排序,將結果保存到answer9_A中

create table answer9_A as 
select a.years as years, a.type as type, avg(a.rate) as rate 
from answer8_A a 
group by a.years,a.type 
order by rate desc;
select * from answer9_A;

B.  求TopN,按照years分組,需要添加一列來記錄每組的順序,將結果保存到answer9_B中

create table answer9_B as 
select years,type,rate,row_number() over (distribute by years sort by rate) as num
from answer9_A;
select * from answer9_B;

C.  按照num=1作為where過濾條件取出結果數據

select * from answer9_B where num=1;

10、每個地區最高評分的電影名,把結果存入HDFS(地區,電影名,影評分)

(1)分析思路:

  1、需求字段:電影id t_movie.movieid

          電影名 t_movie.moviename

          影評分 t_rating.rate(排序條件)

          地區 t_user.zipcode(分組條件)

  2、核心SQL:

    A.  需要把三張表進行聯合查詢,取出電影id、電影名稱、影評分、地區,將結果保存到answer10_A表中

          需要查詢的字段:電影id t_movie.movieid

                    電影名 t_movie.moviename

                    影評分 t_rating.rate(排序條件)

                    地區 t_user.zipcode(分組條件)

    B.  求TopN,按照地區分組,按照平均排序,添加一列num用來記錄地區排名,將結果保存到answer10_B表中

    C.  按照num=1作為where過濾條件取出結果數據

(2)完整SQL:

 A.  需要把三張表進行聯合查詢,取出電影id、電影名稱、影評分、地區,將結果保存到answer10_A表中

create table answer10_A as
select c.movieid, c.moviename, avg(b.rate) as avgrate, a.zipcode
from t_user a 
join t_rating b on a.userid=b.userid 
join t_movie c on b.movieid=c.movieid 
group by a.zipcode,c.movieid, c.moviename;
select t.* from answer10_A t;

B.  求TopN,按照地區分組,按照平均排序,添加一列num用來記錄地區排名,將結果保存到answer10_B表中

create table answer10_B as
select movieid,moviename,avgrate,zipcode, row_number() over (distribute by zipcode sort by avgrate) as num 
from answer10_A; 
select t.* from answer10_B t;

C.  按照num=1作為where過濾條件取出結果數據並保存到HDFS上

insert overwrite directory "/movie/answer10/" select t.* from answer10_B t where t.num=1;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM