当前位置:   article > 正文

[Hadoop]Hadoop生态综合案例_hadoop生态圈的子项目非常庞大的例子

hadoop生态圈的子项目非常庞大的例子

陌陌聊天数据分析案例需求

  • 陌陌作为聊天平台每天都会有大量的用户在线,会出现大量的聊天数据,通过对聊天数据的统计分析,可以更好的对用户构建精准的用户画像,为用户提供更好的服务以及实现ROI的平台运营推广,给公司的发展决策提供精确的数据支撑。

  • 基于Hadoop和Hive实现聊天数据统计分析,构建聊天数据分析报表

  • 需求:

    • 统计今日总消息量

    • 统计今日每小时消息量、发送和接收用户数

    • 统计今日各地区发送消息数据量

    • 统计今日发送消息和接收消息的用户数

    • 统计今日发送消息最多的Top10用户

    • 统计今日接收消息最多的Top10用户

    • 统计发送人的手机型号分布情况

    • 统计发送人的设备操作系统分布情况

  • 数据内容

基于Hive数仓实现需求开发

建库建表、加载数据

建库建表

  1. --如果数据库已存在就删除
  2. drop database if exists db_msg cascade;
  3. --创建数据库
  4. create database db_msg;
  5. --切换数据库
  6. use db_msg;
  7. --如果表已存在就删除
  8. drop table if exists db_msg.tb_msg_source;
  9. --建表
  10. create table db_msg.tb_msg_source(
  11. msg_time             string  comment "消息发送时间"
  12.  , sender_name        string  comment "发送人昵称"
  13.  , sender_account     string  comment "发送人账号"
  14.  , sender_sex         string  comment "发送人性别"
  15.  , sender_ip          string  comment "发送人ip地址"
  16.  , sender_os          string  comment "发送人操作系统"
  17.  , sender_phonetype   string  comment "发送人手机型号"
  18.  , sender_network     string  comment "发送人网络类型"
  19.  , sender_gps         string  comment "发送人的GPS定位"
  20.  , receiver_name      string  comment "接收人昵称"
  21.  , receiver_ip        string  comment "接收人IP"
  22.  , receiver_account   string  comment "接收人账号"
  23.  , receiver_os        string  comment "接收人操作系统"
  24.  , receiver_phonetype string  comment "接收人手机型号"
  25.  , receiver_network   string  comment "接收人网络类型"
  26.  , receiver_gps       string  comment "接收人的GPS定位"
  27.  , receiver_sex       string  comment "接收人性别"
  28.  , msg_type           string  comment "消息类型"
  29.  , distance           string  comment "双方距离"
  30.  , message            string  comment "消息内容"
  31. )
  32. --指定分隔符为制表符
  33. row format delimited fields terminated by '\t';

加载数据

  1. --上传数据文件到node1服务器本地文件系统(HS2服务所在机器)
  2. --shell: mkdir -p /root/hivedata
  3. --加载数据到表中
  4. load data local inpath '/root/hivedata/data1.tsv' into table db_msg.tb_msg_source;
  5. load data local inpath '/root/hivedata/data2.tsv' into table db_msg.tb_msg_source;
  6. --查询表 验证数据文件是否映射成功
  7. select * from tb_msg_source limit 10;
  8. --统计行数
  9. select count(*) as cnt from tb_msg_source;

ETL数据清洗

数据问题

  1. 当前数据中,有一些数据的字段为空,不是合法数据

  2. 需求中,需要统计每天、每个小时的消息量,但是数据中没有天和小时字段,只有整体时间字段,不好处理

  3. 需求中,需要对经度和纬度构建地区的可视化地图,但是数据中GPS经纬度为一个字段,不好处理

ETL需求

  • 需求1:对字段为空的不合法数据进行过滤

    • Where过滤

  • 需求2:通过时间字段构建天和小时字段

    • Substr函数

  • 需求3:从GPS的经纬度中提取经度和维度

    • Split函数

  • 需求4:将ETL以后的结果保存到一张新的Hive表中

    • Create table …… as select ……

ETL实现

  1. --如果表已存在就删除
  2. drop table if exists db_msg.tb_msg_etl;
  3. --将Select语句的结果保存到新表中
  4. create table db_msg.tb_msg_etl as
  5. select *,
  6.       substr(msg_time, 0, 10)   as dayinfo,    --获取天
  7.       substr(msg_time, 12, 2)   as hourinfo,   --获取小时
  8.       split(sender_gps, ",")[0] as sender_lng, --提取经度
  9.       split(sender_gps, ",")[1] as sender_lat  --提取纬度
  10. from db_msg.tb_msg_source
  11. --过滤字段为空的数据
  12. where length(sender_gps) > 0;

查看结果

  1. select msg_time,
  2.       dayinfo,
  3.       hourinfo,
  4.       sender_gps,
  5.       sender_lng,
  6.       sender_lat
  7. from db_msg.tb_msg_etl
  8. limit 10;

需求指标统计

  1. 正确解读业务需求,避免歧义

  2. 确定待查询的数据表-->from表

  3. 找出分析的维度-->group by 分组的字段

  4. 找出计算的指标-->聚合的字段

  5. 其他细节点(过滤、排序等)

  • 指标1:统计今日消息总量

  1. --需求:统计今日总消息量
  2. create table if not exists tb_rs_total_msg_cnt
  3.    comment "今日消息总量"
  4. as
  5. select dayinfo,
  6.       count(*) as total_msg_cnt
  7. from db_msg.tb_msg_etl
  8. group by dayinfo;
  9. select *
  10. from tb_rs_total_msg_cnt;
  11. --结果验证
  • 指标2:统计每小时消息量、发送和接收用户数

  1. --需求:统计今日每小时消息量、发送和接收用户数
  2. create table if not exists tb_rs_hour_msg_cnt
  3.    comment "每小时消息量趋势"
  4. as
  5. select dayinfo,
  6.       hourinfo,
  7.       count(*)                         as total_msg_cnt,
  8.       count(distinct sender_account)   as sender_usr_cnt,
  9.       count(distinct receiver_account) as receiver_usr_cnt
  10. from db_msg.tb_msg_etl
  11. group by dayinfo, hourinfo;
  12. select *
  13. from tb_rs_hour_msg_cnt;
  14. --结果验证
  • 指标3:统计今日各地区发送消息总量

  1. --需求:统计今日各地区发送消息数据量
  2. create table if not exists tb_rs_loc_cnt
  3.    comment "今日各地区发送消息总量"
  4. as
  5. select dayinfo,
  6.       sender_gps,
  7.       cast(sender_lng as double) as longitude,
  8.       cast(sender_lat as double) as latitude,
  9.       count(*)                   as total_msg_cnt
  10. from db_msg.tb_msg_etl
  11. group by dayinfo, sender_gps, sender_lng, sender_lat;
  12. select *
  13. from tb_rs_loc_cnt;
  14. --结果验证

注意:出现在select中的字段,要么是分组的字段,要么是被聚合函数应用的字段。

  • 指标4:统计今日发送和接收用户人数

  1. --需求:统计今日发送消息和接收消息的用户数
  2. create table if not exists tb_rs_usr_cnt
  3.    comment "今日发送消息人数、接受消息人数"
  4. as
  5. select dayinfo,
  6.       count(distinct sender_account)   as sender_usr_cnt,
  7.       count(distinct receiver_account) as receiver_usr_cnt
  8. from db_msg.tb_msg_etl
  9. group by dayinfo;
  10. select *
  11. from tb_rs_usr_cnt;
  12. --结果验证
  • 指标5:统计发送消息条数最多的Top10用户

  1. --需求:统计今日发送消息最多的Top10用户
  2. create table if not exists tb_rs_susr_top10
  3.    comment "发送消息条数最多的Top10用户"
  4. as
  5. select dayinfo,
  6.       sender_name as username,
  7.       count(*)    as sender_msg_cnt
  8. from db_msg.tb_msg_etl
  9. group by dayinfo, sender_name
  10. order by sender_msg_cnt desc
  11. limit 10;
  12. select *
  13. from tb_rs_susr_top10;
  14. --结果验证
  • 指标6:统计接收消息条数最多的Top10用户

  1. --需求:统计今日接收消息最多的Top10用户
  2. create table if not exists tb_rs_rusr_top10
  3.    comment "接受消息条数最多的Top10用户"
  4. as
  5. select dayinfo,
  6.       receiver_name as username,
  7.       count(*)      as receiver_msg_cnt
  8. from db_msg.tb_msg_etl
  9. group by dayinfo, receiver_name
  10. order by receiver_msg_cnt desc
  11. limit 10;
  12. select *
  13. from tb_rs_rusr_top10;
  14. --结果验证
  • 指标7:统计发送人的手机型号分布情况

  1. --需求:统计发送人的手机型号分布情况
  2. create table if not exists tb_rs_sender_phone
  3.    comment "发送人的手机型号分布"
  4. as
  5. select dayinfo,
  6.       sender_phonetype,
  7.       count(distinct sender_account) as cnt
  8. from tb_msg_etl
  9. group by dayinfo, sender_phonetype;
  10. select *
  11. from tb_rs_sender_phone;
  12. --结果验证
  • 指标8:统计发送人的操作系统分布

  1. --需求:统计发送人的设备操作系统分布情况
  2. create table if not exists tb_rs_sender_os
  3.    comment "发送人的OS分布"
  4. as
  5. select dayinfo,
  6.       sender_os,
  7.       count(distinct sender_account) as cnt
  8. from tb_msg_etl
  9. group by dayinfo, sender_os;
  10. select *
  11. from tb_rs_sender_os; --结果验证

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/709993
推荐阅读
相关标签
  

闽ICP备14008679号