题目要求

有如此三份数据:
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(地区,电影名,影评分)

一、数据清洗、建表

因为元数据的字段之间用::分割,所以我们使用shell进行一下清洗,将::都转换成逗号

vi change1.sh#!/bin/bash
sed "s/::/,/g" /zgm/movies.dat>/zgm/movies2.dat
sed "s/::/,/g" /zgm/ratings.dat>/zgm/ratings2.dat
sed "s/::/,/g" /zgm/users.dat>/zgm/users2.datchmod +x change1.sh/bin/sh change1.sh

或者在建表时:

 create external table if not exists ratings(userid bigint,movieid bigint,rating double,timestamped string)partitioned by (dt string,city string)row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'with serdeProperties ('input.regex'='([0-9]*)::([0-9]*)::([0-9]*)::([0-9]*)')location '/zgm/ratings';

建表、load数据的完整shell

#! /bin/bash
yesterday=$(date -d 'yesterday' +%Y%m%d)
logdir=/Log/movieLog/$yesterdayif(!-d "$logdir");then
mkdir "$logdir"
fihql1="use myhive;drop table users;create external table if not exists users(userid bigint,gender string,age int,occupation string,zipcode string)partitioned by (dt string,city string)row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'with serdeProperties ('input.regex'='([0-9]*)::([A-Z]*)::([0-9]*)::([0-9]*)::([0-9]*)')location '/zgm/users';load data local inpath '/zgm/users.dat' overwrite into table users partition(dt='${yesterday}',city='shandong');"$HIVE_HOME/bin/hive --database myhive -e "${hql1}"hql2="drop table movies;create external table if not exists movies(movieid bigint,title string,genres string)partitioned by (dt string,city string)row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'with serdeProperties ('input.regex'='([0-9]*)::([^@]*)::([^ ]*)')location '/zgm/movies';load data local inpath '/zgm/movies.dat' overwrite into table movies partition(dt='${yesterday}',city='shandong');"$HIVE_HOME/bin/hive --database myhive -e "${hql2}"
hql3="drop table ratings;create external table if not exists ratings(userid bigint,movieid bigint,rating double,timestamped string)partitioned by (dt string,city string)row format serde 'org.apache.hadoop.hive.serde2.RegexSerDe'with serdeProperties ('input.regex'='([0-9]*)::([0-9]*)::([0-9]*)::([0-9]*)')location '/zgm/ratings';load data local inpath '/zgm/ratings.dat' overwrite into table ratings partition(dt='${yesterday}',city='shandong');"$HIVE_HOME/bin/hive --database myhive -e "${hql3}"

二、实现分析hql

2、求被评分次数最多的10部电影,并给出评分次数(电影名,评分次数)

#!/bin/bashyesterday=$(date -d '1 day ago' +%Y%m%d)
echo $yesterdayruntime=$(date +%Y%m%d-%H:%M:%S)logdir=/Log/movieLog/$yesterday/top10
if [ ! -d "$logdir" ];then
mkdir -p $logdir
echo "${logdir}不存在,创建完毕"
else echo "${logdir}已存在"
fihql1="
create table if not exists dm_rating_Top10
(title string,
rcount int
)
partitioned by (dt string,city string)
row format delimited
fields terminated by ','
location '/zgm/movie_dm/topN';set hive.exec.dynamic.partition.mode=nonstrict;
insert overwrite table dm_rating_Top10
partition (dt,city)
select title,t.rcount,t.dt,t.city
from (
select movieid,count(rating) as rcount ,dt,city
from ratings
where dt='${yesterday}'
group by movieid,dt,city
order by rcount desc
limit 10
) t
join movies
on movies.movieid=t.movieid ;
"$HIVE_HOME/bin/hive --database myhive -e "${hql1}">$logdir/"${runtime}".log

3、分别求男性,女性当中评分最高的10部电影(性别,电影名,影评分)

select t2.gender,a.title,t2.rating
from movies a
join (
select t1.* from
(
select
b.gender ,c.movieid,c.rating,
row_number() over(partition by b.gender order by c.rating desc) as rn,dt
from users b join ratings c on b.userid=c.userid and c.dt='20200111'
) as t1
where t1.rn<=10
)  t2
on t2.movieid=a.movieid
distribute by t2.gender
sort by t2.rating; 

4、求movieid = 2116这部电影各年龄段(因为年龄就只有7个,
就按这个7个分就好了)的平均影评(年龄段,影评分)

select
t1.agelevel,avg(t1.rating)
from
(select
floor(b.age/10) as agelevel,
a.rating
from ratings a
join users b on a.userid=b.userid and a.movieid=2116
)t1
group by t1.agelevel
;

5、求最喜欢看电影(影评次数最多)的那位女性评最高分的10部电影的
平均影评分(观影者,电影名,影评分)

select b.userid,count(1) uc
from ratings b join
(
select
a.userid
from users a where a.gender='F'
) t1 on t1.userid=b.userid
group by b.userid
order by uc desc
limit 1;select c.movieid,c.rating
from ratings c
where c.userid=1150
order by rating desc
limit 10;

6、求好片(评分>=4.0)最多的那个年份的最好看的10部电影

//create view film_view as
//select r.*,u.gender,u.age,m.title,m.genres
//from ratings r
//join users u on r.userid = u.userid
//join movies m on r.movieid = m.movieid;create view film_view2 as
select r.*,m.title,substr(m.title,-5,4) as year
from ratings r
join movies m on r.movieid = m.movieid;
select
f2.year,f2.mtitle,f2.rating
from film_view2 f2
where year=(
select t.year
from(
select
f.year,count(1) as c
from film_view2 f
where f.rating>4
group by year
order by c desc
limit 1) t
)
order by rating desc limit 10;

7、求1997年上映的电影中,评分最高的10部Comedy类电影

8、该影评库中各种类型电影中评价最高的5部电影(类型,电影名,平均影评分)

9、各年评分最高的电影类型(年份,类型,影评分)

10、每个地区(邮政编码)最高评分的电影名,把结果存入HDFS(地区,电影名,影评分)

Hive笔记——影评项目相关推荐

  1. 影评项目(hive)

    现有如此三份数据: 1.users.dat 数据格式为: 2::M::56::16::70072 对应字段为:UserID BigInt,Gender String,Age Int,Occupatio ...

  2. hive实训项目之电商数据分析

    hive实训项目---------电商数据分析 题干: 某大型电商公司从后台服务器收集到30W条的日志用户行为数据,经过数据初步清洗得到数据如下表sale_user.zip,假如你是该公司一员开发工程 ...

  3. hive 笔记(有点乱,凑合看)

    hive 笔记(有点乱,凑合看) set hive.map.aggr=true; set hive.groupby.skewindata=true; set hive.merg.mapfiles=tr ...

  4. python基础学习[python编程从入门到实践读书笔记(连载三)]:django学习笔记web项目

    文章目录 Django项目:学习笔记web网页 项目部署 参考 自己部署的网站,还是小有成就感的,毕竟踩过很多坑,实战技能也有些许进步. 网站链接:http://lishizheng.herokuap ...

  5. JDE910笔记2--OMW项目建立及简单使用

    1.打开JDE的OBJECT MANAGEMENT WORKBENCH.在工作区中选择ADD,建立项目并选择OMW PROJECT,添加相关信息,如下图所示 其中,ProjectID可以对应不同的数据 ...

  6. JDE910笔记2--OMW项目建立及简单使用[转]

    1.打开JDE的OBJECT MANAGEMENT WORKBENCH.在工作区中选择ADD,建立项目并选择OMW PROJECT,添加相关信息,如下图所示 其中,ProjectID可以对应不同的数据 ...

  7. 树莓派魔镜项目——笔记一 项目介绍和内容链接

    佩服这哥们"想到就去做"的精神和动手能力,以及广大coder的开源精神. 原帖: 树莓派磨制"魔镜"全记录 | 树莓派实验室 社区 一键安装的开源树莓派魔镜项目 ...

  8. hive笔记(五):查询、排序-join语句/连接/分区/sort by/distribute by/cluster by

    目录 查询 Join语句 等值Join 表的别名 内连接 左外连接 右外连接 满外连接 多表连接 笛卡尔集 排序 全局排序 按照别名排序 多个列排序 reduce内部排序(sort by) 分区(di ...

  9. hive案例——影评

    现有如此三份数据: 1.users.dat 数据格式为: 2::M::56::16::70072 对应字段为:UserID BigInt, Gender String, Age Int, Occupa ...

最新文章

  1. Memcached全面剖析
  2. python基础教程第二版答案-《Python基础教程》(第2版修订版)中要注意的地方...
  3. April Fools Contest 2017 题解
  4. C语言 main 函数 - C语言零基础入门教程
  5. MySQl中文1001无标题_Mysql中字段类型不一致导致索引无效的处理办法
  6. php程序员述职材料_php程序员述职报告(精选多篇)
  7. Mysql触发器与动态完整性
  8. 翻译连载 | 第 11 章:融会贯通 -《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇...
  9. mock.js那点事(上)
  10. php构造函数里抛出异常_构造函数、析构函数抛出异常的问题
  11. 松下a6伺服电机接线图_松下A6伺服电机说明书Part6.pdf
  12. 软件测试方法_边界值分析法
  13. Windows10系统下电脑时间不对,怎么办?
  14. Oracle练习题(九)
  15. android平板刷成windows,安卓系统的平板电脑可以刷成windows系统吗?
  16. CANopen 7.过程数据对象 PDO Process data object)
  17. HBase的数据模型和存储原理
  18. css3 3D立体相册实现
  19. nodeJs 接收上传文件
  20. 自媒体淘客选好爆文标题轻松月入过万

热门文章

  1. VC++界面编程之--自定义CEdit(编辑框)皮肤
  2. python爬虫练习(豆瓣电影)
  3. 官宣丨“创客集结号”成功接入“广东省教育资源公共服务平台”!
  4. 内蒙古计算机专修学院单招,内蒙古单招考什么科目
  5. oracle主表子表,oracle查询包含在子表中的主表数据
  6. slashdot网站架构:硬件和软件 zz
  7. 对HashMap进行排序的常见方法
  8. PowerDesigner导出word总结
  9. 王传福难摘比亚迪的低端帽子
  10. Hi3559AV100如何调试NVP6324 寄存器