分享

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

levycui 2020-12-29 22:04:01 发表于 连载型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 2533
问题导读:
1、如何统计每日各类别下点击次数top10的商品?
2、如何使用日志数据用户行为宽表作为DWS层表?
3、如何统计点击次数最多的10个用户点击的商品次数top10?
4、如何统计月活跃用户与截止到该月累计的用户总和之间的比例?


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

第18章 需求十二:统计每日各类别下点击次数top10的商品
18.1 DWS层
使用点击日志表作为DWS层数据源
18.2 ADS层
18.2.1 建表语句
  1. drop table if exists ads_goods_display_top10;
  2. create external table ads_goods_display_top10 (
  3.     `dt` string COMMENT '日期',
  4.     `category` string COMMENT '品类',
  5.     `goodsid` string COMMENT '商品id',
  6.     `goods_count` string COMMENT '商品点击次数'
  7. )
  8. row format delimited fields terminated by '\t'
  9. location '/warehouse/gmall/ads/ads_goods_display_top10';
复制代码


18.2.2 导入数据
  1. insert into table ads_goods_display_top10
  2. select
  3.   '2019-10-03',
  4.   category,
  5.   goodsid,
  6.   count
  7. from(
  8.   select
  9.     category,
  10.     goodsid,
  11.     count,
  12.     rank() over(partition by category order by count desc) rk
  13.   from(
  14.     select
  15.       category,
  16.       goodsid,
  17.       count(*) count
  18.     from dwd_display_log
  19.     where dt='2019-10-03' and action=2
  20.     group by category, goodsid
  21.   )t1
  22. )t2
  23. where rk<=10;
复制代码


18.2.3 导入数据脚本
1)在/home/kgg/bin目录下创建脚本ads_goods_display_top10.sh
  1. [kgg@hadoop102 bin]$ vim ads_goods_display_top10.sh
复制代码
在脚本中填写如下内容
  1. #!/bin/bash
  2. db=gmall
  3. hive=/opt/module/hive/bin/hive
  4. hadoop=/opt/module/hadoop/bin/hadoop
  5. if [[ -n $1 ]]; then
  6.     do_date=$1
  7. else
  8.     do_date=`date -d '-1 day' +%F`
  9. fi
  10. sql="
  11. use gmall;
  12. insert into table ads_goods_display_top10
  13. select
  14.   '$do_date',
  15.   category,
  16.   goodsid,
  17.   count
  18. from(
  19.   select
  20.     category,
  21.     goodsid,
  22.     count,
  23.     rank() over(partition by category order by count desc) rk
  24.   from(
  25.     select
  26.       category,
  27.       goodsid,
  28.       count(*) count
  29.     from dwd_display_log
  30.     where dt='$do_date' and action=2
  31.     group by category, goodsid
  32.   )t1
  33. )t2
  34. where rk<=10
  35. "
  36. $hive -e "$sql"
复制代码

2)增加脚本执行权限
  1. [kgg@hadoop102 bin]$ chmod 777 ads_goods_display_top10.sh
复制代码

3)脚本使用
  1. [kgg@hadoop102 module]$ ads_goods_display_top10.sh 2019-02-20
复制代码

4)查询结果
  1. hive (gmall)> select * from ads_goods_display_top10;
复制代码

5)脚本执行时间
企业开发中一般在每天凌晨30分~1点

第19章 需求十三:点击次数最多的10个用户点击的商品次数top10
19.1 DWS层
使用日志数据用户行为宽表作为DWS层表
19.2 ADS层
19.2.1 建表语句
  1. drop table if exists ads_goods_user_count;
  2. create external table ads_goods_user_count(
  3. `dt` string COMMENT '统计日期',
  4. `mid_id` string COMMENT '用户id',
  5.     `u_ct` string COMMENT '用户总点击次数',
  6.     `goodsid` string COMMENT '商品id',
  7.     `d_ct` string COMMENT '各个商品点击次数'
  8. )
  9. row format delimited fields terminated by '\t'
  10. location '/warehouse/gmall/ads/ads_goods_user_count';
复制代码


19.2.2 导入数据
  1. insert into table ads_goods_user_count
  2. select
  3.   '2019-10-03',
  4.   mid_id,
  5.   u_ct,
  6.   goodsid,
  7.   d_ct
  8. from(
  9.   select
  10.     mid_id,
  11.     u_ct,
  12.     goodsid,
  13.     d_ct,
  14.     row_number() over(partition by mid_id order by d_ct desc ) rn
  15.   from(
  16.     select
  17.       dl.mid_id,
  18.       u_ct,
  19.       dl.goodsid,
  20.       count(*) d_ct
  21.     from dwd_display_log dl join (
  22.       select
  23.         mid_id,
  24.         count(*) u_ct
  25.       from dws_user_action_wide_log
  26.       group by mid_id
  27.       order by u_ct desc
  28.       limit 10
  29.     )t1
  30.     on dl.mid_id=t1.mid_id
  31.     group by dl.mid_id, u_ct, dl.goodsid
  32.   ) t2
  33. ) t3
  34. where rn<=10
复制代码

19.2.3 导入数据脚本
1)在/home/kgg/bin目录下创建脚本ads_goods_user_count.sh
  1. [kgg@hadoop102 bin]$ vim ads_goods_user_count.sh
复制代码

在脚本中填写如下内容
  1. #!/bin/bash
  2. db=gmall
  3. hive=/opt/module/hive/bin/hive
  4. hadoop=/opt/module/hadoop/bin/hadoop
  5. if [[ -n $1 ]]; then
  6.     do_date=$1
  7. else
  8.     do_date=`date -d '-1 day' +%F`
  9. fi
  10. sql="
  11. use gmall;
  12. insert into table ads_goods_user_count
  13. select
  14.   '$do_date',
  15.   mid_id,
  16.   u_ct,
  17.   goodsid,
  18.   d_ct
  19. from(
  20.   select
  21.     mid_id,
  22.     u_ct,
  23.     goodsid,
  24.     d_ct,
  25.     row_number() over(partition by mid_id order by d_ct desc ) rn
  26.   from(
  27.     select
  28.       dl.mid_id,
  29.       u_ct,
  30.       dl.goodsid,
  31.       count(*) d_ct
  32.     from dwd_display_log dl join (
  33.       select
  34.         mid_id,
  35.         count(*) u_ct
  36.       from dws_user_action_wide_log
  37.       group by mid_id
  38.       order by u_ct desc
  39.       limit 10
  40.     )t1
  41. on dl.mid_id=t1.mid_id
  42. group by dl.mid_id, u_ct, dl.goodsid
  43.   ) t2
  44. ) t3
  45. where rn<=10
  46. "
  47. $hive -e "$sql"
复制代码

2)增加脚本执行权限
  1. [kgg@hadoop102 bin]$ chmod 777 ads_goods_user_count.sh
复制代码

3)脚本使用
  1. [kgg@hadoop102 module]$ ads_goods_user_count.sh 2019-02-20
复制代码

4)查询结果
  1. hive (gmall)> select * from ads_goods_user_count;
复制代码

5)脚本执行时间
企业开发中一般在每天凌晨30分~1点


第20章 需求十四:月活跃率
月活跃用户与截止到该月累计的用户总和之间的比例
20.1 DWS层
使用DWS层月活表以及ADS新增用户表作为DWS层
20.2 ADS层
20.2.1 建表语句
  1. drop table if exists ads_mn_ratio_count;
  2. create external table ads_mn_ratio_count(
  3.    `dt` string COMMENT '统计日期',
  4.    `mn` string COMMENT '统计月活跃率的月份',
  5.    `ratio` string COMMENT '活跃率'
  6. )
  7. row format delimited fields terminated by '\t'
  8. location '/warehouse/gmall/ads/ads_mn_ratio_count';
复制代码


20.2.2 导入数据
  1. insert into table ads_mn_ratio_count
  2. select
  3.     '2019-10-03',
  4.     date_format('2019-10-03','yyyy-MM'),
  5.     mn_count/sum_user*100 mn_percent
  6. from
  7. (select count(*) mn_count from dws_uv_detail_mn where mn=date_format('2019-10-03','yyyy-MM')) t1,
  8. (select sum(new_mid_count) sum_user from ads_new_mid_count) t2;
复制代码


20.2.3 导入数据脚本
  1. 1)在/home/kgg/bin目录下创建脚本ads_mn_ratio_count.sh
  2. [kgg@hadoop102 bin]$ vim ads_mn_ratio_count.sh
复制代码

在脚本中填写如下内容
  1. #!/bin/bash
  2. db=gmall
  3. hive=/opt/module/hive/bin/hive
  4. hadoop=/opt/module/hadoop/bin/hadoop
  5. if [[ -n $1 ]]; then
  6.     do_date=$1
  7. else
  8.     do_date=`date -d '-1 day' +%F`
  9. fi
  10. sql="
  11. use gmall;
  12. insert into table ads_mn_ratio_count
  13. select
  14.     '$do_date',
  15.     date_format('$do_date','yyyy-MM'),
  16.     mn_count/sum_user*100 mn_percent
  17. from
  18. (select count(*) mn_count from dws_uv_detail_mn where mn=date_format('$do_date','yyyy-MM')) t1,
  19. (select sum(new_mid_count) sum_user from ads_new_mid_count) t2;
  20. "
  21. $hive -e "$sql"
复制代码

2)增加脚本执行权限
  1. [kgg@hadoop102 bin]$ chmod 777 ads_mn_ratio_count.sh
复制代码

3)脚本使用
  1. [kgg@hadoop102 module]$ ads_mn_ratio_count 2019-02-20
复制代码

4)查询结果
  1. hive (gmall)> select * from ads_mn_ratio_count;
复制代码

5)脚本执行时间
企业开发中一般在每天凌晨30分~1点


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


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

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

本版积分规则

关闭

推荐上一条 /2 下一条