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;
相关推荐
XiaoMu_0014 小时前
基于大数据的糖尿病数据分析可视化
大数据·数据挖掘·数据分析
城数派7 小时前
全国乡镇(街道)点位数据2025年(shp格式\excel格式)
arcgis·信息可视化·数据分析
旺仔小拳头..9 小时前
Servlet概念与创建
数据仓库·hive·hadoop
数据科学小丫10 小时前
数据操作案例:RFM 分析
大数据·数据分析·finebi
旺仔小拳头..11 小时前
Filter 过滤器 与Listener 监听器
数据仓库·hive·hadoop
晨晖212 小时前
Servlet的快速入门,请求和响应
hive·hadoop·servlet
易海聚大数据12 小时前
开源情报系统与网络舆情监测系统的核心差异在哪里
数据分析·智能分析·舆情监测系统·开源情报分析系统
Data-Miner12 小时前
Excel-Agent:你的专属 AI 数据分析助手
人工智能·数据分析·excel
guoyunsky12 小时前
Ins爬虫可以抓取到国家,性别和年龄吗?
爬虫·数据分析·rpa
北极九章ArcticData13 小时前
销售管理团队如何用ChatBI实现数据驱动管理?
大数据·人工智能·数据分析·chatbi