分享

大数据项目之电商数仓(总结)(十):用户行为数据仓库

问题导读:
1、如何设计每日新增设备明细表?
2、如何使用每日新增设备表?
3、如何设计用户留存主题表?
4、如何设计留存用户数表?



上一篇:大数据项目之电商数仓(总结)(九):用户行为数据仓库

第7章 需求二:用户新增主题

首次联网使用应用的用户。如果一个用户首次打开某APP,那这个用户定义为新增用户;卸载再安装的设备,不会被算作一次新增。新增用户包括日新增用户、周新增用户、月新增用户。

7.1 DWS层(每日新增设备明细表)
2020-12-15_193314.jpg

2020-12-15_193342.jpg

1)建表语句
  1. hive (gmall)>
  2. drop table if exists dws_new_mid_day;
  3. create external table dws_new_mid_day
  4. (
  5.     `mid_id` string COMMENT '设备唯一标识',
  6.     `user_id` string COMMENT '用户标识',
  7.     `version_code` string COMMENT '程序版本号',
  8.     `version_name` string COMMENT '程序版本名',
  9.     `lang` string COMMENT '系统语言',
  10.     `source` string COMMENT '渠道号',
  11.     `os` string COMMENT '安卓系统版本',
  12.     `area` string COMMENT '区域',
  13.     `model` string COMMENT '手机型号',
  14.     `brand` string COMMENT '手机品牌',
  15.     `sdk_version` string COMMENT 'sdkVersion',
  16.     `gmail` string COMMENT 'gmail',
  17.     `height_width` string COMMENT '屏幕宽高',
  18.     `app_time` string COMMENT '客户端日志产生时的时间',
  19.     `network` string COMMENT '网络模式',
  20.     `lng` string COMMENT '经度',
  21.     `lat` string COMMENT '纬度',
  22.     `create_date`  string  comment '创建时间'
  23. )  COMMENT '每日新增设备信息'
  24. stored as parquet
  25. location '/warehouse/gmall/dws/dws_new_mid_day/';
复制代码

2)导入数据
用每日活跃用户表Left Join每日新增设备表,关联的条件是mid_id相等。如果是每日新增的设备,则在每日新增设备表中为null。
  1. hive (gmall)>
  2. insert into table dws_new_mid_day
  3. select  
  4.     ud.mid_id,
  5.     ud.user_id ,
  6.     ud.version_code ,
  7.     ud.version_name ,
  8.     ud.lang ,
  9.     ud.source,
  10.     ud.os,
  11.     ud.area,
  12.     ud.model,
  13.     ud.brand,
  14.     ud.sdk_version,
  15.     ud.gmail,
  16.     ud.height_width,
  17.     ud.app_time,
  18.     ud.network,
  19.     ud.lng,
  20.     ud.lat,
  21.     '2020-10-14'
  22. from dws_uv_detail_day ud left join dws_new_mid_day nm on ud.mid_id=nm.mid_id
  23. where ud.dt='2020-10-14' and nm.mid_id is null;
复制代码

3)查询导入数据
  1. hive (gmall)> select count(*) from dws_new_mid_day ;
复制代码

4)导入数据脚本
  1. [kgg@hadoop102 bin]$ vi dws_new_log.sh
  2. #!/bin/bash
  3. # 定义变量方便修改
  4. APP=gmall
  5. hive=/opt/module/hive/bin/hive
  6. hadoop=/opt/module/hadoop-2.7.2/bin/hadoop
  7. # 如果是输入的日期按照取输入日期;如果没输入日期取当前时间的前一天
  8. if [ -n "$1" ] ;then
  9.    do_date=$1
  10. else
  11.    do_date=`date -d "-1 day" +%F`
  12. fi
  13. echo "===日志日期为 $do_date==="
  14. sql="
  15. insert into table "$APP".dws_new_mid_day
  16. select  
  17.     ud.mid_id,
  18.     ud.user_id ,
  19.     ud.version_code ,
  20.     ud.version_name ,
  21.     ud.lang ,
  22.     ud.source,
  23.     ud.os,
  24.     ud.area,
  25.     ud.model,
  26.     ud.brand,
  27.     ud.sdk_version,
  28.     ud.gmail,
  29.     ud.height_width,
  30.     ud.app_time,
  31.     ud.network,
  32.     ud.lng,
  33.     ud.lat,
  34.     '$do_date'
  35. from "$APP".dws_uv_detail_day ud left join "$APP".dws_new_mid_day nm on ud.mid_id=nm.mid_id
  36. where ud.dt='$do_date' and nm.mid_id is null;
  37. "
  38. $hive -e "$sql"
复制代码


7.2 ADS层(每日新增设备表)
2020-12-15_193436.jpg
1)建表语句
  1. hive (gmall)>
  2. drop table if exists ads_new_mid_count;
  3. create external table ads_new_mid_count
  4. (
  5.     `create_date` string comment '创建时间',
  6.     `new_mid_count` BIGINT comment '新增设备数量'
  7. ) COMMENT '每日新增设备信息数量'
  8. row format delimited fields terminated by '\t'
  9. location '/warehouse/gmall/ads/ads_new_mid_count/';
复制代码


2)导入数据
  1. hive (gmall)>
  2. insert into table ads_new_mid_count
  3. select
  4. create_date,
  5. count(*)
  6. from dws_new_mid_day
  7. where create_date='2020-10-14'
  8. group by create_date;
复制代码


3)查询导入数据
  1. hive (gmall)> select * from ads_new_mid_count;
复制代码

4)导入数据脚本
  1. [kgg@hadoop102 bin]$ vim ads_new_log.sh
  2. #!/bin/bash
  3. # 定义变量方便修改
  4. APP=gmall
  5. hive=/opt/module/hive/bin/hive
  6. hadoop=/opt/module/hadoop-2.7.2/bin/hadoop
  7. # 如果是输入的日期按照取输入日期;如果没输入日期取当前时间的前一天
  8. if [ -n "$1" ] ;then
  9.    do_date=$1
  10. else
  11.    do_date=`date -d "-1 day" +%F`
  12. fi
  13. echo "===日志日期为 $do_date==="
  14. sql="
  15. insert into table "$APP".ads_new_mid_count
  16. select
  17. create_date,
  18. count(*)
  19. from "$APP".dws_new_mid_day
  20. where create_date='$do_date'
  21. group by create_date;"
  22. $hive -e "$sql"
复制代码



第8章 需求三:用户留存主题
8.1 需求目标
8.1.1 用户留存概念

2020-12-15_193528.jpg
8.1.2 需求描述
2020-12-15_193557.jpg

8.2 DWS层
8.2.1 DWS层(每日留存用户明细表)

2020-12-15_193627.jpg

1)建表语句
  1. hive (gmall)>
  2. drop table if exists dws_user_retention_day;
  3. create external table dws_user_retention_day
  4. (
  5.     `mid_id` string COMMENT '设备唯一标识',
  6.     `user_id` string COMMENT '用户标识',
  7.     `version_code` string COMMENT '程序版本号',
  8.     `version_name` string COMMENT '程序版本名',
  9.     `lang` string COMMENT '系统语言',
  10.     `source` string COMMENT '渠道号',
  11.     `os` string COMMENT '安卓系统版本',
  12.     `area` string COMMENT '区域',
  13.     `model` string COMMENT '手机型号',
  14.     `brand` string COMMENT '手机品牌',
  15.     `sdk_version` string COMMENT 'sdkVersion',
  16.     `gmail` string COMMENT 'gmail',
  17.     `height_width` string COMMENT '屏幕宽高',
  18.     `app_time` string COMMENT '客户端日志产生时的时间',
  19.     `network` string COMMENT '网络模式',
  20.     `lng` string COMMENT '经度',
  21.     `lat` string COMMENT '纬度',
  22.    `create_date`    string  comment '设备新增时间',
  23.    `retention_day`  int comment '截止当前日期留存天数'
  24. )  COMMENT '每日用户留存情况'
  25. PARTITIONED BY (`dt` string)
  26. stored as parquet
  27. location '/warehouse/gmall/dws/dws_user_retention_day/';
复制代码

2)导入数据(每天计算前1天的新用户访问留存明细)
  1. hive (gmall)>
  2. insert overwrite table dws_user_retention_day
  3. partition(dt="2020-10-15")
  4. select  
  5.     nm.mid_id,
  6.     nm.user_id ,
  7.     nm.version_code ,
  8.     nm.version_name ,
  9.     nm.lang ,
  10.     nm.source,
  11.     nm.os,
  12.     nm.area,
  13.     nm.model,
  14.     nm.brand,
  15.     nm.sdk_version,
  16.     nm.gmail,
  17.     nm.height_width,
  18.     nm.app_time,
  19.     nm.network,
  20.     nm.lng,
  21.     nm.lat,
  22. nm.create_date,
  23. 1 retention_day
  24. from dws_uv_detail_day ud join dws_new_mid_day nm on ud.mid_id=nm.mid_id where ud.dt='2020-10-15' and nm.create_date=date_add('2020-10-15',-1);
复制代码

3)查询导入数据(每天计算前1天的新用户访问留存明细)
  1. hive (gmall)> select count(*) from dws_user_retention_day;
复制代码



8.2.2 DWS层(1,2,3,n天留存用户明细表)
1)导入数据(每天计算前1,2,3,n天的新用户访问留存明细)
  1. hive (gmall)>
  2. insert overwrite table dws_user_retention_day
  3. partition(dt="2019-02-11")
  4. select
  5.     nm.mid_id,
  6.     nm.user_id,
  7.     nm.version_code,
  8.     nm.version_name,
  9.     nm.lang,
  10.     nm.source,
  11.     nm.os,
  12.     nm.area,
  13.     nm.model,
  14.     nm.brand,
  15.     nm.sdk_version,
  16.     nm.gmail,
  17.     nm.height_width,
  18.     nm.app_time,
  19.     nm.network,
  20.     nm.lng,
  21.     nm.lat,
  22.     nm.create_date,
  23.     1 retention_day
  24. from dws_uv_detail_day ud join dws_new_mid_day nm  on ud.mid_id =nm.mid_id
  25. where ud.dt='2019-02-11' and nm.create_date=date_add('2019-02-11',-1)
  26. union all
  27. select  
  28.     nm.mid_id,
  29.     nm.user_id ,
  30.     nm.version_code ,
  31.     nm.version_name ,
  32.     nm.lang ,
  33.     nm.source,
  34.     nm.os,
  35.     nm.area,
  36.     nm.model,
  37.     nm.brand,
  38.     nm.sdk_version,
  39.     nm.gmail,
  40.     nm.height_width,
  41.     nm.app_time,
  42.     nm.network,
  43.     nm.lng,
  44.     nm.lat,
  45.     nm.create_date,
  46.     2 retention_day
  47. from  dws_uv_detail_day ud join dws_new_mid_day nm   on ud.mid_id =nm.mid_id
  48. where ud.dt='2019-02-11' and nm.create_date=date_add('2019-02-11',-2)
  49. union all
  50. select  
  51.     nm.mid_id,
  52.     nm.user_id,
  53.     nm.version_code,
  54.     nm.version_name,
  55.     nm.lang,
  56.     nm.source,
  57.     nm.os,
  58.     nm.area,
  59.     nm.model,
  60.     nm.brand,
  61.     nm.sdk_version,
  62.     nm.gmail,
  63.     nm.height_width,
  64.     nm.app_time,
  65.     nm.network,
  66.     nm.lng,
  67.     nm.lat,
  68.     nm.create_date,
  69.     3 retention_day
  70. from  dws_uv_detail_day ud join dws_new_mid_day nm   on ud.mid_id =nm.mid_id
  71. where ud.dt='2019-02-11' and nm.create_date=date_add('2019-02-11',-3);
复制代码

2)查询导入数据(每天计算前1,2,3天的新用户访问留存明细)
  1. hive (gmall)> select retention_day , count(*) from dws_user_retention_day group by retention_day;
复制代码


8.2.3 Union与Union all区别
1)准备两张表
  1. tableA                              tableB
  2. id  name  score             id  name  score
  3. 1   a    80               1    d    48
  4. 2    b    79               2   e    23
  5. 3    c     68               3   c    86
复制代码

2)采用union查询
  1. select name from tableA             
  2. union                        
  3. select name from tableB             
  4. 查询结果
  5. name
  6. a
  7. d
  8. b
  9. e
  10. c
复制代码

3)采用union all查询
  1. select name from tableA
  2. union all
  3. select name from tableB
  4. 查询结果
  5. name
  6. a
  7. b
  8. c
  9. d
  10. e
  11. c
复制代码

4)总结
(1)union会将联合的结果集去重,效率较union all差
(2)union all不会对结果集去重,所以效率高


8.3 ADS层
8.3.1 留存用户数

2020-12-15_193721.jpg

1)建表语句
  1. hive (gmall)>
  2. drop table if exists ads_user_retention_day_count;
  3. create external table ads_user_retention_day_count
  4. (
  5. `create_date` string  comment '设备新增日期',
  6. `retention_day` int comment '截止当前日期留存天数',
  7. `retention_count` bigint comment  '留存数量'
  8. )  COMMENT '每日用户留存情况'
  9. row format delimited fields terminated by '\t'
  10. location '/warehouse/gmall/ads/ads_user_retention_day_count/';
复制代码

2)导入数据
  1. hive (gmall)>
  2. insert into table ads_user_retention_day_count
  3. select
  4.     create_date,
  5.     retention_day,
  6.     count(*) retention_count
  7. from dws_user_retention_day
  8. where dt='2020-10-15'
  9. group by create_date,retention_day;
复制代码

3)查询导入数据
  1. hive (gmall)> select * from ads_user_retention_day_count;
复制代码




8.3.2 留存用户比率

2020-12-15_193755.jpg

1)建表语句
  1. hive (gmall)>
  2. drop table if exists ads_user_retention_day_rate;
  3. create external table ads_user_retention_day_rate
  4. (
  5. `stat_date` string comment '统计日期',
  6. `create_date` string  comment '设备新增日期',
  7. `retention_day` int comment '截止当前日期留存天数',
  8. `retention_count` bigint comment  '留存数量',
  9. `new_mid_count` bigint comment '当日设备新增数量',
  10. `retention_ratio` decimal(10,2) comment '留存率'
  11. )  COMMENT '每日用户留存情况'
  12. row format delimited fields terminated by '\t'
  13. location '/warehouse/gmall/ads/ads_user_retention_day_rate/';
复制代码

2)导入数据
  1. hive (gmall)>
  2. insert into table ads_user_retention_day_rate
  3. select
  4.     '2020-10-14',
  5.     ur.create_date,
  6.     ur.retention_day,
  7.     ur.retention_count,
  8.     nc.new_mid_count,
  9.     ur.retention_count/nc.new_mid_count*100
  10. from ads_user_retention_day_count ur join ads_new_mid_count nc
  11. on nc.create_date=ur.create_date;
复制代码

3)查询导入数据
  1. hive (gmall)>select * from ads_user_retention_day_rate;
复制代码


最新经典文章,欢迎关注公众号


本帖被以下淘专辑推荐:

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条