hive練習之[影評案例]
現有如此三份資料:
1、users.dat 資料格式為: 2::M::56::16::70072
對應欄位為:UserID BigInt, Gender String, Age Int, OccupationString, 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.首先使用命令對資料進行資料清洗
[[email protected] movierating]$ sed -i 's/::/,/g'movies.dat
[[email protected] movierating]$ sed -i 's/::/,/g'ratings.dat
建表語句:
create table ratings(UserID BigInt, MovieIDBigInt, Rating Double, Timestamped String) row format delimited fieldsterminated by "," location "/hive/movie/ratings";
create table movies(MovieID BigInt, TitleString, Genres String) row format delimited fields terminated by ","location "/hive/movie/movies";
create table users(UserID BigInt, GenderString, Age Int, Occupation String, Zipcode String) row format delimited fieldsterminated by "," location "/hive/movie/user"
2.求被評分次數最多的10部電影,並給出評分次數(電影名,評分次數)
create table top10 as select MovieId,count(*)cc fromratings group by MovieId order by cc desc limit 0,10;
首先建一個臨時表記錄評分數最多的前10部電影
select t.MovieId,t.cc,m.Title from top10 t,movies mwhere m.MovieId=t.MovieId;
聯合movies表查出電影名稱
3.分別求男性,女性當中評分最高的10部電影(性別,電影名,影評分)
最後的資料形式:
GendermovieId Rating
M
create table newrating as select * from ratings limit0,1000;
Select u.Gender, n.MovieID,avg(n.Rating) avr
from newrating n
left join users u on u.UserId=n.UserId
where u.Gender='M'
group by n.MovieId ,u.Gender
order by avr desc
limit 10;
//把男女評分前10的都合成一個表
create table MF10as (select * from m10 union select * from f10);
再和movie表連接獲取電影名稱
select u.gender,m.title,u.avr from MF10 u,movies m where m.movieid=u.movieid;
4.求movieid =2116這部電影各年齡段(因為年齡就只有7個,就按這個7個分就好了)的平均影評(年齡段,影評分)
結果:
Movieid Age rating
2116 1 4.0
Select avg(r.rating),u.age
From ratings r
Left join users u
Onr.UserId=u.UserId
Group by u.age
Order by u.age;
5.求最喜歡看電影(影評次數最多)的那位女性評最高分的10部電影的平均影評分(觀影者,電影名,影評分)
Select r.UserId, count(*)c
From ratings r
Left join users u
Onr.UserId=u.UserId
Where u.Gender= 'F'
Group by r.UserId
Order by c desc
Limit 1;
//先求出影評次數最多的女性1150 1302
Create table g10mas Select userId, movieId ,avg(rating) av
From ratings
Where UserId=1150
Group bymovieId,userid
Order by av desc
Limit 10;
//建為臨時表 g10m
//求出了10部電影的id
Selectg.userid,movieid,avg(rating) avg
from g10m g
left join ratings r
ong.movieid=r.movieid
group byr.movieid,g.userid;
6. 求好片(評分>=4.0)最多的那個年份的最好看的10部電影
首先寫一個py指令碼 把時間戳轉成年份
#!/bin/python
import sys
import datetime
for line insys.stdin:
line = line.strip()
UserID,MovieID,Rating,Timestamped =line.split(',')
timeArray = time.localtime(Timestamped)
year = time.strftime("%Y",timeArray)
print ','.join([UserID, MovieID,Rating,str(year)])
然後建立臨時表 用來存放新資料
8.影評庫中各種型別電影中評價最高的5部電影(型別,電影名,平均影評分)
用到的表
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,評分,評分時間戳
create table t_bi_reg(id string,name string) |
建立一個表
Create table movies2(MovieID string, Title String,type string)
row format serde'org.apache.hadoop.hive.serde2.RegexSerDe'
with serdeproperties('input.regex'='(.*)::(.*)::(.*)','output.format.string'='%1$s%2$s %3$s');
記得記得記得 每個型別都只能是string
把資料炸開
selectmovieid,title,type1 from movies2 lateral viewexplode(split(type,"\\|")) mytable as type1;
新的hive提供了幾個函式可以進行分組topN排序
連線ratings表
userid bigint
movieid bigint
rating double
timestamped string
typemovie
movieid string
title string
type1 string
selecta.t1,a.t2,a.avf from (select *,row_number() over(partition by t1 order by avf desc) as ro from toptype) a wherero<=10;
9.各年評分最高的電影型別(年份,型別,影評分)
思路:需要用到的表
Ratings
userid bigint
movieid bigint
rating double
timestamped string gs
movies
movieid bigint
title string
genres string
首先建立一個表儲存年份
create table yearmovie as select*,substr(title,-5,4) as year from movies limit 20;
字串擷取
資料格式
12 Dracula: Dead and Loving It (1995) Comedy|Horror 1995
13 Balto (1995) Animation|Children's 1995
14 Nixon (1995) Drama 1995
15 Cutthroat Island(1995) Action|Adventure|Romance 1995
16 Casino (1995) Drama|Thriller 1995
17 Sense and Sensibility (1995) Drama|Romance 1995
18 Four Rooms (1995) Thriller 1995
19 Ace Ventura:When Nature Calls (1995) Comedy 1995
年份 型別 評分
接著要炸開型別