1. Hive实战

2. 文件字段说明

视频表

字段 备注 详细描述
video id 视频唯一id(String) 11位字符串
uploader 视频上传者(String) 上传视频的用户名String
age 视频年龄(int) 视频在平台上的整数天
category 视频类别(Array<String> 上传视频指定的视频分类
length 视频长度(Int) 整形数字标识的视频长度
views 观看次数(Int) 视频被浏览的次数
rate 视频评分(Double) 满分5分
Ratings 流量(Int) 视频的流量,整型数字
conments 评论数(Int) 一个视频的整数评论数
related ids 相关视频id(Array<String> 相关视频的id,最多20个

用户表

字段 备注 字段类型
uploader 上传者用户名 string
videos 上传视频数 int
friends 朋友数量 int

3. ETL Mapper 处理

书写ETL Mapper编码

导入坐标

  1. <dependencies>
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.12</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.logging.log4j</groupId>
  9. <artifactId>log4j-slf4j-impl</artifactId>
  10. <version>2.12.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.apache.hadoop</groupId>
  14. <artifactId>hadoop-client</artifactId>
  15. <version>3.1.3</version>
  16. </dependency>
  17. <!-- <dependency>-->
  18. <!-- <groupId>org.apache.hadoop</groupId>-->
  19. <!-- <artifactId>hadoop-client-runtime</artifactId>-->
  20. <!-- <version>3.1.3</version>-->
  21. <!-- </dependency>-->
  22. </dependencies>

Mapper类

package com.atguigu.etl;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class ETLMapper extends Mapper<LongWritable, Text, Text, NullWritable> {


    private Counter pass;
    private Counter fail;

    private StringBuffer sb = new StringBuffer();

    private Text result = new Text();


    @Override
    protected void setup(Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        pass = context.getCounter("ETL", "Pass"); //计数器
        fail = context.getCounter("ETL", "Fail");
    }

    /**
     * 将一行日志进行处理 字段不够的抛弃 第四个字段中的空格去掉 将最后相关视频的分割符改为 &
     *
     * @param key     行号
     * @param value   一行日志
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
        String line = value.toString();

        String[] field = line.split("\t");

        //判断字数是否足够
        if (field.length >= 9) {
            //处理数据
            //去掉第四个字段的空格
            field[3] = field[3].replace(" ", "");  //原本 a & b ==> 变成 a&b

            //拼接成一行
            sb.setLength(0); //清空
            for (int i = 0; i < field.length; i++) {
                //如果当前拼接的字段是我们这一行的最后一个字段 则直接追加
                if (i == field.length - 1) {
                    sb.append(field[i]);
                } else if (i <= 8) { //前面的字段都是用 \t 隔开
                    //如果拼的是前9个字段
                    sb.append(field[i]).append("\t");

                } else {
                    //剩下的分割符为&
                    sb.append(field[i]).append("&");  //最后一个字段为一个数组 元素之间用&隔开
                }


            }
            result.set(sb.toString());
            context.write(result, NullWritable.get()); //写入上下文
            pass.increment(1);
        } else {
            //丢弃数据 此数据不足9个字段
            fail.increment(1);
        }


    }
}

Driver

package com.atguigu.etl;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class ETLDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
//        args = new String[]{"d:/input","d:/output"}; //本地测试

        Configuration configuration = new Configuration();
        configuration.set("hive.execution.engine","yarn-tez"); //改为tez引擎 本地模式不要修改请使用默认引擎
        Job job = Job.getInstance();

        job.setJarByClass(ETLDriver.class);

        job.setMapperClass(ETLMapper.class);
        job.setNumReduceTasks(0);

        job.setMapOutputKeyClass(Text.class);
        job.setOutputValueClass(NullPointerException.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        boolean b = job.waitForCompletion(true);
        System.exit(b ? 0 : 1);
    }
}

打包成jar包上传到集群中 运行自定义mapreduce

yarn jar etltool20211110-1.0-SNAPSHOT.jar com.atguigu.etl.ETLDriver /gulivideo/video /gulivideo/video_etl

4. 创建表

  • 创库
create database gulivideo;
use gulivideo;
  • 外部表
-- video表
create external table video_ori(
    videoId string, 
    uploader string, 
    age int, 
    category array<string>, 
    length int, 
    views int, 
    rate float, 
    ratings int, 
    comments int,
    relatedId array<string>)
row format delimited fields terminated by "\t"
collection items terminated by "&"
location '/gulivideo/video_etl';
-- user表
create external table user_ori(
    uploader string,
    videos int,
    friends int)
row format delimited fields terminated by "\t" 
location '/gulivideo/user';
  • 内部表
-- video_orc表
create table video_orc(
    videoId string, 
    uploader string, 
    age int, 
    category array<string>, 
    length int, 
    views int, 
    rate float, 
    ratings int, 
    comments int,
    relatedId array<string>)
stored as orc
tblproperties("orc.compress"="SNAPPY");
-- user_orc表
create table user_orc(
    uploader string,
    videos int,
    friends int)
stored as orc
tblproperties("orc.compress"="SNAPPY");
  • 插入数据
-- 从外部表中插入数据
insert into table video_orc select * from video_ori;
insert into table user_orc select * from user_ori;

5. 需求实现

5.1. 统计视频观看数top10

使用order by按照views字段做一个全局排序即可,同时我们设置只显示前10条。

SELECT
    videoid,
    views
FROM
    video_orc
ORDER BY
    views DESC
LIMIT 10;

5.2. 统计视频类别热度Top10

  1. 即统计每个类别有多少个视频,显示出包含视频最多的前10个类别
  2. 我们需要按照类别group by聚合,然后count组内的videoId个数即可。
  3. 因为当前表结构为:一个视频对应一个或多个类别。所以如果要group by类别,需要先将类别进行列转行(展开),然后再进行count即可。
-- category列转行
SELECT
    videoid,
    cate
FROM
    video_orc LATERAL VIEW explode(category) tbl as cate;
-- 在上表基础上,统计各个类别有多少视频,并排序取前十
SELECT
    cate,
    COUNT(videoid) n
FROM
    t1
GROUP BY
    cate
ORDER BY
    n desc limit 10;

完整语句

SELECT
    cate,
    COUNT(videoid) n
FROM
    (SELECT
    videoid,
    cate
FROM
    video_orc LATERAL VIEW explode(category) tbl as cate)t1
GROUP BY
    cate
ORDER BY
    n desc limit 10;

5.3. 统计出视频观看数最高的20个视频的所属类别以及类别包含Top20视频的个数

  1. 统计前20视频和类别
SELECT
    videoid,
    views,
    category
FROM
    video_orc
ORDER BY
    views DESC
LIMIT 20;
  1. 打散类别 列转行
SELECT
    videoid,
    cate
FROM
    t1 LATERAL VIEW explode(category) tbl as cate;
  1. 按照类别统计个数
SELECT
    cate,
    COUNT(videoid) n
FROM
    t2
GROUP BY
    cate
ORDER BY
    n DESC;
  1. 完整语句
SELECT
    cate,
    COUNT(videoid) n
FROM
    (
    SELECT
        videoid,
        cate
    FROM
        (
        SELECT
            videoid,
            views,
            category
        FROM
            video_orc
        ORDER BY
            views DESC
        LIMIT 20 ) t1 LATERAL VIEW explode(category) tbl as cate ) t2
GROUP BY
    cate
ORDER BY
    n DESC;

5.4. 统计视频观看数Top50所关联视频的所属类别排序

  1. 统计观看数前50的视频的关联视频
SELECT
    videoid,
    views,
    relatedid
FROM
    video_orc
ORDER BY
    views DESC
LIMIT 50;
  1. 打散关联视频 列转行
SELECT
    explode(relatedid) videoid
FROM
    t1;
  1. 和原表join获取关联视频的类别
SELECT
    DISTINCT t2.videoid,
    v.category
FROM
    t2
JOIN video_orc v on
    t2.videoid = v.videoid;
  1. 打散类别
SELECT
    explode(category) cate
FROM
    t3;
  1. 类别热度表 每个类别出现次数
SELECT
        cate,
        COUNT(videoid) n
    FROM
        (
        SELECT
            videoid,
            cate
        FROM
            video_orc LATERAL VIEW explode(category) tbl as cate) g1
    GROUP BY
        cate
  1. 和类别热度表 join并排序
SELECT
    DISTINCT t4.cate,
    t5.n
FROM
    t4
JOIN t5 ON
    t4.cate = t5.cate
ORDER BY
    t5.n DESC;
  1. 完整语句
SELECT 
    DISTINCT t4.cate,
    t5.n
FROM
    (
    SELECT
        explode(category) cate
    FROM
        (
        SELECT
            DISTINCT t2.videoid,
            v.category
        FROM
            (
            SELECT
                explode(relatedid) videoid
            FROM
                (
                SELECT
                    videoid,
                    views,
                    relatedid
                FROM
                    video_orc
                ORDER BY
                    views DESC
                LIMIT 50 ) t1 ) t2
        JOIN video_orc v on
            t2.videoid = v.videoid ) t3 ) t4
JOIN (
    SELECT
        cate,
        COUNT(videoid) n
    FROM
        (
        SELECT
            videoid,
            cate
        FROM
            video_orc LATERAL VIEW explode(category) tbl as cate) g1
    GROUP BY
        cate ) t5 ON
    t4.cate = t5.cate
ORDER BY
    t5.n DESC;

5.5. 统计每个类别中的视频热度Top10,以Music为例

  1. 把视频表的类别炸开,生成中间表格video_category
CREATE
    TABLE
        video_category STORED AS orc TBLPROPERTIES("orc.compress"="SNAPPY") AS SELECT
            videoid,
            uploader,
            age,
            cate,
            length,
            views,
            rate,
            ratings,
            comments,
            relatedid
        FROM
            video_orc LATERAL VIEW explode(category) tbl as cate;
  1. 从video_category 直接查询Music类的前10视频
SELECT
    videoid,
    views
FROM
    video_category
WHERE
    cate ="Music"
ORDER BY
    views DESC
LIMIT 10;

5.6. 统计每个类别中视频流量Top10,以Music为例

  1. 从video_category直接查询Music类的流量前10视频
SELECT
    videoid,
    ratings
FROM
    video_category
WHERE
    cate ="Music"
ORDER BY
    ratings DESC
LIMIT 10;

5.7. 统计上传视频最多的用户Top10以及他们上传的观看次数在前20的视频

5.7.1. 理解1.上传视频观看数最多前十用户每人前20条视频

  1. 统计上传视频中 观看数量最大的Top10上传用户
SELECT
    uploader,
    videos
FROM
    user_orc
ORDER BY
    videos DESC
LIMIT 10;
  1. 和video_orc联立,找出这些用户上传的视频,并按照热度排名
SELECT
    t1.uploader,
    v.videoid,
    RANK() OVER(PARTITION BY t1.uploader ORDER BY v.views DESC) hot
FROM
    t1
LEFT JOIN video_orc v ON
    t1.uploader = v.uploader;
  1. 求前20
SELECT
    t2.uploader,
    t2.videoid,
    t2.hot
FROM
    t2
WHERE
    hot <= 20;
  1. 完整语句
SELECT
    t2.uploader,
    t2.videoid,
    t2.hot
FROM
    (SELECT
    t1.uploader,
    v.videoid,
    RANK() OVER(PARTITION BY t1.uploader ORDER BY v.views DESC) hot
FROM
    (SELECT
    uploader,
    videos
FROM
    user_orc
ORDER BY
    videos DESC
LIMIT 10)t1
LEFT JOIN video_orc v ON
    t1.uploader = v.uploader)t2
WHERE
    hot <= 20;

5.7.2. 理解2.上传视频数前十的用户 是否存在视频播放数总榜前20

  1. 统计视频上传最多的用户Top10
SELECT
    uploader,
    videos
FROM
    user_orc
ORDER BY
    videos DESC
LIMIT 10;
  1. 观看数前20的视频
SELECT
    videoid,
    uploader,
    views
FROM
    video_orc
ORDER BY
    views DESC
LIMIT 20;
  1. 联立两表,看看有没有他们上传的
SELECT
    t1.uploader,
    t2.videoid
FROM
    t1
LEFT JOIN t2 ON
    t1.uploader = t2.uploader;
  1. 完整语句
SELECT
    t1.uploader,
    t2.videoid
FROM
    (SELECT
    uploader,
    videos
FROM
    user_orc
ORDER BY
    videos DESC
LIMIT 10)t1
LEFT JOIN (SELECT
    videoid,
    uploader,
    views
FROM
    video_orc
ORDER BY
    views DESC
LIMIT 20)t2 ON
    t1.uploader = t2.uploader;

6. 统计每个类别视频观看数Top10

  1. 从video_category表查出每个类别视频观看数排名
SELECT
    cate,
    videoid,
    views,
    RANK() OVER(PARTITION BY cate ORDER BY views DESC) hot
FROM
    video_category;
  1. 取每个类别的Top10
SELECT
    cate,
    videoid,
    views
FROM
    t1
WHERE
    hot <= 10;
  1. 完整语句
SELECT
    cate,
    videoid,
    views
FROM
    (SELECT
    cate,
    videoid,
    views,
    RANK() OVER(PARTITION BY cate ORDER BY views DESC) hot
FROM
    video_category)t1
WHERE
    hot <= 10;