#!/bin/bash source /etc/profile if [ -n "$1" ] then do_date=$1 else do_date=`date -d "-1 day" +%F` fi
sql=" insert overwrite table ads.ads_ad_show partition (dt='$do_date') select count(1), count(distinct uid), count(distinct device_id), ad_action, hour from dwd.dwd_ad where dt='$do_date' group by ad_action, hour; "
hive -e "$sql"
1
sh ads_load_ad_show.sh 2020-07-20
漏斗分析(点击率购买率)
需求分析
分时统计:
点击率 = 点击次数 / 曝光次数
购买率 = 购买次数 / 点击次数
创建ADS层表
1 2 3 4 5 6 7 8
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 ',';
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
-- 方法一 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-08-02' 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-08-02' and hour='01' group by hour ;
#!/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; "
drop table if exists ads.ads_ad_show_place; create table ads.ads_ad_show_place( ad_action tinyint, hour string, place string, product_id int, cnt bigint ) PARTITIONED BY (`dt` string) row format delimited fields terminated by ',';
drop table if exists ads.ads_ad_show_place_window; create table ads.ads_ad_show_place_window( hour string, place string, product_id int, cnt bigint, rank int ) PARTITIONED BY (`dt` string) row format delimited fields terminated by ',';
#!/bin/bash source /etc/profile if [ -n "$1" ] then do_date=$1 else do_date=`date -d "-1 day" +%F` fi
sql=" insert overwrite table ads.ads_ad_show_place_window partition (dt='$do_date') select * from ( select hour, place, product_id, cnt, row_number() over (partition by hour, place, product_id order by cnt desc) rank from ads.ads_ad_show_place where dt='$do_date' and ad_action='0' ) t where rank <= 100; "