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;
相关推荐
大鳥4 分钟前
数据仓库知识体系
hive·hadoop
talle20211 小时前
Hive | 行列转换
数据仓库·hive·hadoop
忘忧记2 小时前
某小说数据分析过程
windows·数据挖掘·数据分析
talle20214 小时前
Hive | json数据处理
hive·hadoop·json
CTO Plus技术服务中4 小时前
Hive开发与运维教程
数据仓库·hive·hadoop
Gain_chance4 小时前
28-学习笔记尚硅谷数仓搭建-DWD层交易域加购事务事实表建表语句及详细分析
数据仓库·hive·笔记·学习·datagrip
小邓睡不饱耶4 小时前
Hive 实战:数据仓库建模、SQL 进阶与企业级案例
数据仓库·hive·sql
辰宇信息咨询14 小时前
3D自动光学检测(AOI)市场调研报告-发展趋势、机遇及竞争分析
大数据·数据分析
地球资源数据云18 小时前
中国90米分辨率土壤质地含量数据集
数据分析·遥感数据·卫星遥感
AC赳赳老秦20 小时前
科研数据叙事:DeepSeek将实验数据转化为故事化分析框架
开发语言·人工智能·数据分析·r语言·时序数据库·big data·deepseek