|NO.Z.00038|——————————|BigDataEnd|——|Hadoop&PB级数仓.V05|——|PB数仓.v05|广告分析|漏斗分析|

一、漏斗分析(点击率购买率)
### --- 需求分析

~~~     # 分时统计:
~~~     点击率 = 点击次数 / 曝光次数
~~~     购买率 = 购买次数 / 点击次数
二、创建ADS层表
### --- 创建ADS层表

~~~     # 语法:创建ADS层表
drop table if exists ads.ads_ad_show_rate;

create table ads.ads_ad_show_rate(
hour string,
click_rate double,
buy_rate double
) PARTITIONED BY (`dt` string)
row format delimited fields terminated by ',';
### --- 操作实例:创建ADS层表

hive (default)> drop table if exists ads.ads_ad_show_rate;

hive (default)> 
              > create table ads.ads_ad_show_rate(
              > hour string,
              > click_rate double,
              > buy_rate double
              > ) PARTITIONED BY (`dt` string)
              > row format delimited fields terminated by ',';
三、曝光 点击 购买 时间(HH)行转列
### --- 数据分析

15075 15075 15075 0 01 2020-08-02 4349 4349 4349 1 01 
2020-08-02 1245 1245 1245 2 01 2020-08-02 15075 4349 1245 01 2020-08-02
### --- 行转列的方法
~~~     # 方法一

select sum(case when ad_action='0' then cnt end) show_cnt,
sum(case when ad_action='1' then cnt end) click_cnt,
sum(case when ad_action='2' then cnt end) buy_cnt,
hour
from ads.ads_ad_show
where dt='2020-07-21' and hour='01'
group by hour ;
~~~     # 方法二

select max(case when ad_action='0' then cnt end) show_cnt,
max(case when ad_action='1' then cnt end) click_cnt,
max(case when ad_action='2' then cnt end) buy_cnt,
hour
from ads.ads_ad_show
where dt='2020-07-21' and hour='01'
group by hour ;
四、加载ADS层数据
### --- 加载ADS层数据脚本

[root@hadoop02 ~]# vim /data/yanqidw/script/advertisement/ads_load_ad_show_rate.sh
#!/bin/bash

source /etc/profile

if [ -n "$1" ] ;
then
    do_date=$1
else
    do_date=`date -d "-1 day" +%F`
fi

sql="
with tmp as(
select max(case when ad_action='0' then cnt end) show_cnt,
        max(case when ad_action='1' then cnt end) click_cnt,
        max(case when ad_action='2' then cnt end) buy_cnt,
        hour
    from ads.ads_ad_show
  where dt='$do_date'
group by hour
)
insert overwrite table ads.ads_ad_show_rate
partition (dt='$do_date')
select hour,
        click_cnt / show_cnt as click_rate,
        buy_cnt / click_cnt as buy_rate
 from tmp;
"

hive -e "$sql"
### --- 加载ADS层数据:计算购买率

[root@hadoop02 ~]# sh /data/yanqidw/script/advertisement/ads_load_ad_show_rate.sh 2020-07-21
### --- 查看购买率

hive (default)> show partitions ads.ads_ad_show_rate;
partition
dt=2020-07-21

hive (default)> select * from ads.ads_ad_show_rate  where dt='2020-07-21' limit 3;
ads_ad_show_rate.hour   ads_ad_show_rate.click_rate ads_ad_show_rate.buy_rate   ads_ad_show_rate.dt
00  0.2553191489361702  0.25    2020-07-21
01  0.3055555555555556  0.36363636363636365 2020-07-21
02  0.2631578947368421  0.26666666666666666 2020-07-21

hive (default)> select count(*) from ads.ads_ad_show_rate  where dt='2020-07-21';
24

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(17)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示