Hive总结及案例讲解

完整hive总结

  1. hive建立一张表,跟已经存在的结构化的数据文件产生映射关系。映射成功后,就可以通过写HQL来分析这个结构化的数据文件,避免了写mr程序的麻烦。

  2. 数据库:和hdfs中/user/hive/warehouse下的一个文件夹对应;
    表:和数据库文件夹下面的子文件夹/user/hive/warehouse/库名.db/表名 对应;
    表的数据位置目前不能随便存放,一定要在指定的数据库表的文件夹下面;
    建立表的时候,需要指定分隔符,否则可能会映射不成功。

  3. 建表的字段个数和字段类型,要跟结构化数据中的个数类型一致。

  4. 分区表字段不能够在表中已经存在;
    分区字段是一个虚拟的字段,不存放任何数据;
    分区字段的数据来自于装载分区表数据的时候指定的;
    分区表的字段在hdfs上的效果就是在建立表的文件夹下面又创建了子文件夹;
    建立分区表的目的把数据的划分更加细致,减少了查询时候全表扫描的成本,只需要按照指定的分区扫描数据并显示结果即可;
    分区表就是辅助查询,缩小查询范围,加快数据的检索速度。

  5. 分桶表在创建之前需要开启分桶功能;
    分桶表创建时,分桶的字段必须是表中已经存在的字段,即要按照表中的哪个字段进行分开;
    分桶表也是把表所映射的结构数据文件分成更细致的部分,但是更多的是用在join查询提高效率之上,只需要把join的字段在各自表中进行分桶操作。

Hive之影评分析案例

现有三分数据,具体数据如下:

1.users.txt
  1. 数据格式(共有6040条数据)

3:M:25:15:55117

  1. 对应字段
用户id 性别 年龄 职业 邮政编码
user_id gender age work coding
2. movies.txt
  1. 数据格式(共有3883条数据)

3:Grumpier Old Men (1995):Comedy|Romance

  1. 对应字段
电影id 电影名字 电影类型
movie_id name genres
3. ratings.txt
  1. 数据格式(共有1000209条数据)

1:661:3:978392198

  1. 对应字段
用户id 电影id 评分 评分时间戳
user_id movie_id rating times

库表映射实现

  1. 建库
create database movie;
use movie;
  1. 创建t_user表并导入数据
create table t_user(
user_id bigint,
gender string,
age int,
work string,
code string
) row format delimited fields terminated by ':';

load data local inpath '/home/tarena/hivedata/users.txt' into table t_user;
  1. 创建t_movie表并导入数据
create table t_movie(
movie_id bigint,
name string,
genres string
)  row format delimited fields terminated by ':';

load data local inpath '/home/tarena/hivedata/movies.txt' into table t_movie;

4.创建t_rating表并导入数据

create table t_rating(
user_id bigint,
movie_id bigint,
rating double,
times string
) row format delimited fields terminated by ':';

load data local inpath '/home/tarena/hivedata/ratings.txt' into table t_rating;

案例实现

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

  1. 需求字段
    1.1) 电影名:t_movie.name
    1.2) 评分次数:t_rating.rating
  2. 思路
    按照电影名进行分组统计,求出每部电影的评分次数并按照评分次数降序排序。
  3. 实现
create table result1 as
select b.name as name,count(b.name) as total from t_movie b 
inner join t_rating c on b.movie_id=c.movie_id
group by b.name
order by total desc

2. 求movieid=2116这部电影各年龄的平均影评(年龄,影评分)

  1. 需求字段
    1.1) 年龄:t_user.age
    1.2) 影评分:t_rating.rating
  2. 思路
    t_usert_rating表进行联合查询,movie_id=2116过滤条件,年龄分组
  3. 实现
create table result3 as 
select a.age as age, avg(c.rating) as avgrate from t_user a
join t_rating c
on a.user_id=c.user_id 
where c.movie_id=2116
group by a.age;

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

  1. 需求字段
    1.1) 性别:t_user.gender
    1.2) 电影名:t_movie.name
    1.3) 影评分:t.rating.rating
  2. 思路
    2.1) 三表联合查询
    2.2) 按照性别过滤条件,电影名作为分组条件,影评分作为排序条件进行查询
  3. 实现
    3.1) 女性当中评分最高的10部电影
create table result2_F as 
select 'F' as sex, b.name as name, avg(c.rating) as avgrate 
from t_rating c join t_user a on c.user_id=a.user_id 
join t_movie b on c.movie_id=b.movie_id
where a.gender='F' 
group by b.name order by avgrate desc 
limit 10;

3.2) 男性当中评分最高的10部电影

create table result2_M as 
select 'M' as sex, b.name as name, avg(c.rating) as avgrate 
from t_rating c join t_user a on c.user_id=a.user_id 
join t_movie b on c.movie_id=b.movie_id 
where a.gender='M' 
group by b.name order by avgrate desc 
limit 10;

4.求最喜欢看电影(影评次数最多)的那位女性评最高分的10部电影的平均影评分(电影编号,电影名,影评分)

  1. 需求字段
    1.1) 电影编号:t_rating.movie_id
    1.2) 电影名:t_movie.name
    1.3) 影评分:t_rating.rating
  2. 思路
    2.1) 先找出最喜欢看电影的那位女性
    2.2) 根据2.1中的女性user_id作为where过滤条件,以看过的电影影评分rating作为排序条件进行排序,找出评分最高的10部电影
    2.3) 求出2.2中10部电影的平均分
  3. 实现
    3.1) 最喜欢看电影的女性(t_rating.user_id,次数)
create table result4_A as 
select c.user_id,count(c.user_id) as total from t_rating c 
join t_user a on c.user_id=a.user_id 
where a.gender='F' 
group by c.user_id order by total desc limit 1;

3.2) 找出那个女人评分最高的10部电影

create table result4_B as
select c.movie_id, c_rating as rating from t_rating c 
where c.user_id=1150 order by rating desc limit 10;

3.3) 求出10部电影的平均分

select d.movie_id as movie_id, b.name as name, avg(c.rating) 
from result4_B d join t_rating on d.movie_id=c.movie_id 
join t_movie on c.movie_id=b.movie_id 
group by d.movie_id, b.name;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容