赞
踩
MR速度慢—引入hive
背景:大量的用户在线,通过对聊天数据的分析,构建用户画像,为用户提供更好的服务、以及实现高ROI的平台运营推广,给公司的发展决策提供精确的数据支撑。
目标:基于Hadoop和Hive实现聊天数据统计分析,构建聊天数据分析报表
需求:
原始数据:业务系统中导出的某一天24小时的用户聊天数据,TSV文件。列分隔符:制表符 \t
在Notepad中可以通过显示所有字符来判断间隔符
打开Datagrip,创建一个hive工程,语言选择hive,并与hive服务器创建连接。
Datagrip中:
--------------1、建库-------------------
--如果数据库已存在就删除
drop database if exists db_msg cascade;
--创建数据库
create database db_msg;
--切换数据库
use db_msg;
--------------2、建表------------------- --如果表已存在就删除 drop table if exists db_msg.tb_msg_source; --建表 create table db_msg.tb_msg_source( msg_time string comment "消息发送时间" , sender_name string comment "发送人昵称" , sender_account string comment "发送人账号" , sender_sex string comment "发送人性别" , sender_ip string comment "发送人ip地址" , sender_os string comment "发送人操作系统" , sender_phonetype string comment "发送人手机型号" , sender_network string comment "发送人网络类型" , sender_gps string comment "发送人的GPS定位" , receiver_name string comment "接收人昵称" , receiver_ip string comment "接收人IP" , receiver_account string comment "接收人账号" , receiver_os string comment "接收人操作系统" , receiver_phonetype string comment "接收人手机型号" , receiver_network string comment "接收人网络类型" , receiver_gps string comment "接收人的GPS定位" , receiver_sex string comment "接收人性别" , msg_type string comment "消息类型" , distance string comment "双方距离" , message string comment "消息内容" ) --指定分隔符为制表符 row format delimited fields terminated by '\t';
--------------3、加载数据-------------------
--上传数据文件到node1服务器本地文件系统(HS2服务所在机器)
--shell: mkdir -p /root/hivedata
--加载数据到表中
load data local inpath '/root/hivedata/data1.tsv' into table db_msg.tb_msg_source;
load data local inpath '/root/hivedata/data2.tsv' into table db_msg.tb_msg_source;
--查询表 验证数据文件是否映射成功
select * from tb_msg_source limit 10;
--统计行数
select count(*) as cnt from tb_msg_source;
加载完数据后,需要判断加载过来的数据是否有效–ETL
问题与解决:
--ETL实现
--如果表已存在就删除
drop table if exists db_msg.tb_msg_etl;
--将Select语句的结果保存到新表中
create table db_msg.tb_msg_etl as
select
*,
substr(msg_time,0,10) as dayinfo, --获取天
substr(msg_time,12,2) as hourinfo, --获取小时
split(sender_gps,",")[0] as sender_lng, --提取经度
split(sender_gps,",")[1] as sender_lat --提取纬度
from db_msg.tb_msg_source
--过滤字段为空的数据
where length(sender_gps) > 0 ;
数据量太多–记得limit 10
--验证ETL结果
select
msg_time,dayinfo,hourinfo,sender_gps,sender_lng,sender_lat
from db_msg.tb_msg_etl
limit 10;
需求1:统计今日总消息量
group by 每日后count计数
create table if not exists tb_rs_total_msg_cnt
comment "今日消息总量"
as
select
dayinfo,
count(*) as total_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo;
select * from tb_rs_total_msg_cnt;--结果验证
需求2:统计今日每小时消息量、发送和接收用户数
按每天,每小时分组,计数
create table if not exists tb_rs_hour_msg_cnt
comment "每小时消息量趋势"
as
select
dayinfo,
hourinfo,
count(*) as total_msg_cnt,
count(distinct sender_account) as sender_usr_cnt,
count(distinct receiver_account) as receiver_usr_cnt
from db_msg.tb_msg_etl
group by dayinfo,hourinfo;
select * from tb_rs_hour_msg_cnt;--结果验证
需求3:统计今日各地区发送消息数据量
按照每日与地区GPS分组,
出现在select后的字段,要么是group by 后的字段,要么是聚合函数字段,所以分组还加了经纬度字段。
case函数:将原本经纬度的string类型转换成double数字类型
cast(sender_lng as double)
create table if not exists tb_rs_loc_cnt
comment "今日各地区发送消息总量"
as
select
dayinfo,
sender_gps,
cast(sender_lng as double) as longitude,
cast(sender_lat as double) as latitude,
count(*) as total_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo,sender_gps,sender_lng,sender_lat;
select * from tb_rs_loc_cnt; --结果验证
需求4:统计今日发送消息和接收消息的用户数
按照天分组,对用户数进行去重统计
create table if not exists tb_rs_usr_cnt
comment "今日发送消息人数、接受消息人数"
as
select
dayinfo,
count(distinct sender_account) as sender_usr_cnt,
count(distinct receiver_account) as receiver_usr_cnt
from db_msg.tb_msg_etl
group by dayinfo;
select * from tb_rs_usr_cnt; --结果验证
需求5:统计今日发送消息最多的Top10用户
按照天,用户分组,计数后排序,limit 10
create table if not exists tb_rs_susr_top10
comment "发送消息条数最多的Top10用户"
as
select
dayinfo,
sender_name as username,
count(*) as sender_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo,sender_name
order by sender_msg_cnt desc
limit 10;
select * from tb_rs_susr_top10; --结果验证
需求6:统计今日接收消息最多的Top10用户
按照天,用户分组,计数后排序,limit 10
create table if not exists tb_rs_rusr_top10
comment "接受消息条数最多的Top10用户"
as
select
dayinfo,
receiver_name as username,
count(*) as receiver_msg_cnt
from db_msg.tb_msg_etl
group by dayinfo,receiver_name
order by receiver_msg_cnt desc
limit 10;
select * from tb_rs_rusr_top10; --结果验证
需求7:统计发送人的手机型号分布情况
按照天,用户手机型号分组,对用户去重计数
create table if not exists tb_rs_sender_phone
comment "发送人的手机型号分布"
as
select
dayinfo,
sender_phonetype,
count(distinct sender_account) as cnt
from tb_msg_etl
group by dayinfo,sender_phonetype;
select * from tb_rs_sender_phone; --结果验证
需求8:统计发送人的设备操作系统分布情况
create table if not exists tb_rs_sender_os
comment "发送人的OS分布"
as
select
dayinfo,
sender_os,
count(distinct sender_account) as cnt
from tb_msg_etl
group by dayinfo,sender_os;
select * from tb_rs_sender_os; --结果验证
进入可视化展示阶段
FineBI:https://www.finebi.com/
FineBI特点:可多人协作、拖拽不需要代码、适合各种分析场景、支持各种图表、支持大数据
已下载安装好
将hive中数据连接到BI上。
FineBI与Hive集成的官方文档:https://help.fanruan.com/finebi/doc-view-301.html
驱动配置、安装插件-----都配置好了,可直接连接hive数据
配置数据操作
FineBI上各种拖拽操作
最后效果:
总结:很简单的一个案例,但把数据分析的整个流程走完了
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。