Hive 案例分析(B站用户行为大数据分析)

Hive 案例分析(B站用户行为大数据分析)

  • 一、案例需求
  • 二、设计数据表结构
    • [2.1 user 表结构](#2.1 user 表结构)
    • [2.2 video 表结构](#2.2 video 表结构)
  • 三、创建数据表
    • [3.1 创建 video 数据库](#3.1 创建 video 数据库)
    • [3.2 创建外表](#3.2 创建外表)
      • [3.1.2 创建 external_user](#3.1.2 创建 external_user)
      • [3.1.3 创建 external_video](#3.1.3 创建 external_video)
    • [3.2 创建内表](#3.2 创建内表)
      • [3.2.1 创建 orc_user](#3.2.1 创建 orc_user)
      • [3.2.2 创建 orc_video](#3.2.2 创建 orc_video)
  • 四、准备工作
    • [4.1 准备user和video 数据](#4.1 准备user和video 数据)
    • [4.2 数据导入外表](#4.2 数据导入外表)
      • [4.2.1 导入 external_user 表](#4.2.1 导入 external_user 表)
      • [4.2.1 导入 external_video 表](#4.2.1 导入 external_video 表)
    • [4.3 数据导入 内表](#4.3 数据导入 内表)
      • [4.3.1 导入 ocl_user](#4.3.1 导入 ocl_user)
      • [4.3.1 导入 ocl_video](#4.3.1 导入 ocl_video)
  • 五、统计分析
    • [5.1 统计B站视频观看数前 top 10](#5.1 统计B站视频观看数前 top 10)
    • [5.2 统计B站视频分类热度 top 10](#5.2 统计B站视频分类热度 top 10)
    • [5.3 统计每个类别视频观看人数前 top 3](#5.3 统计每个类别视频观看人数前 top 3)
    • [5.4 统计上传B站视频最多的用户前 top 10,以及这些用户上传的视频观看次数在前10的视频](#5.4 统计上传B站视频最多的用户前 top 10,以及这些用户上传的视频观看次数在前10的视频)
    • [5.5 统计B站视频不同评分等级(等级作为字段显示)的视频数量](#5.5 统计B站视频不同评分等级(等级作为字段显示)的视频数量)

一、案例需求

b站现在积累有用户数据和视频列表数据,为了配合市场部门做好用户运营工作,需要对b站的用户行为进行分析,其具体需求如下所示:

  • 统计b站视频不同评分等级(行转列)的视频数。
  • 统计上传b站视频最多的用户Top 10,以及这些用户上传的视频观看次数在前10的视频。
  • 统计b站每个类别视频观看数top 10。
  • 统计b站视频分类热度top 10。
  • 统计b站视频观看数top 10。

二、设计数据表结构

2.1 user 表结构

2.2 video 表结构

三、创建数据表

3.1 创建 video 数据库

javascript 复制代码
create database if not exists video;

3.2 创建外表

用于保存原始数据

3.1.2 创建 external_user

javascript 复制代码
create external table if not exists external_user
(uid int,
name string,
regtime string,
visitnum int,
lastvisit string,
gender int,
birthday string,
country string,
province string,
city string,
uploadvideos int)
row format delimited fields terminated by ","
stored as textfile;

3.1.3 创建 external_video

javascript 复制代码
create external table if not exists  external_video
(vid string,
uid int,
vday int,
vtype string,
vlength int,
visit int,
score int,
comments int,
collection int,
fabulous int,
forward int)
row format delimited fields terminated by ","
stored as textfile;

3.2 创建内表

用于计算获取结果

3.2.1 创建 orc_user

javascript 复制代码
create table if not exists orc_user
(uid int,
name string,
regtime string,
visitnum int,
lastvisit string,
gender int,
birthday string,
country string,
province string,
city string,
uploadvideos int)
row format delimited fields terminated by ","
stored as orc
tblproperties("orc.compress"="SNAPPY");

3.2.2 创建 orc_video

javascript 复制代码
create table if not exists orc_video
(vid string,
uid int,
vday int,
vtype string,
vlength int,
visit int,
score int,
comments int,
collection int,
fabulous int,
forward int)
row format delimited fields terminated by ","
stored as orc
tblproperties("orc.compress"="SNAPPY");

四、准备工作

4.1 准备user和video 数据

4.2 数据导入外表

4.2.1 导入 external_user 表

javascript 复制代码
load data local inpath '/usr/local/data/tmp/user.txt' overwrite into table external_user;

4.2.1 导入 external_video 表

javascript 复制代码
load data local inpath '/usr/local/data/tmp/video.txt' overwrite into table external_video;

4.3 数据导入 内表

4.3.1 导入 ocl_user

javascript 复制代码
insert into table orc_user select * from external_user;

4.3.1 导入 ocl_video

javascript 复制代码
insert into table orc_video select * from external_video;

五、统计分析

5.1 统计B站视频观看数前 top 10

javascript 复制代码
select vid,visit from orc_video order by visit desc limit 10;

5.2 统计B站视频分类热度 top 10

javascript 复制代码
select vtype,count(vid) hot from orc_video group by vtype order by hot desc limit 10;

5.3 统计每个类别视频观看人数前 top 3

javascript 复制代码
select v.vtype,v.vid,v.visit from (select vtype,vid,visit,rank() over(partition by vtype order by visit desc) rk ) v where rk <= 3;

5.4 统计上传B站视频最多的用户前 top 10,以及这些用户上传的视频观看次数在前10的视频

javascript 复制代码
select v.vid,v.visit,v.uid from (select uid,uploadvideos from orc_user order by uploadvideos desc limit 10) u join orc_video v on u.uid=v.uid order by v.visit desc limit 10;

5.5 统计B站视频不同评分等级(等级作为字段显示)的视频数量

javascript 复制代码
select
max(case v.score when 1 then v.num else 0 end) 1star,
max(case v.score when 2 then v.num else 0 end) 2star,
max(case v.score when 3 then v.num else 0 end) 3star,
max(case v.score when 4 then v.num else 0 end) 4star,
max(case v.score when 5 then v.num else 0 end) 5star
from (select score,count(*) as num from orc_video group by score) v;
相关推荐
SelectDB14 小时前
强行拍平?全表扫描? AI Agent 动态 JSON 的观测分析
数据库·人工智能·数据分析
白日与明月16 小时前
Hive分桶机制应用
数据仓库·hive·hadoop
Msshu12317 小时前
XSP25全协议 100W PD快充诱骗芯片_串口读电压电流信息
hive·云计算·json·database·memcached
水火既济__17 小时前
大数据hive_mr压缩问题
大数据·hive·mr
水火既济__17 小时前
hive中加载json数据建表(大规模)
hive·hadoop·json
bzmK1DTbd17 小时前
ClickHouse列式存储:海量数据分析利器
clickhouse·oracle·数据分析
千瓜17 小时前
“小赛”掀“大浪”,小红书种草野生玩法
大数据·人工智能·数据分析·生活·新媒体
70asunflower18 小时前
5.4 分布分析
人工智能·算法·机器学习·数据挖掘·数据分析
2zcode18 小时前
基于图像处理与数据分析的智能答题卡识别与阅卷系统设计与实现
图像处理·人工智能·数据分析
互联科技报18 小时前
能做表格的 AI 软件:数以轻舟Agent,AI 原生重构表格数据分析全流程
人工智能·重构·数据分析