9(21)流失用戶數21

第12章 需求六:流失用戶數
流失用戶:最近7天未登錄我們稱之爲流失用戶
12.1 DWS層
使用日活明細表dws_uv_detail_day作爲DWS層數據
12.2 ADS層
1)建表語句
hive (gmall)>
drop table if exists ads_wastage_count;
create external table ads_wastage_count(
dt string COMMENT ‘統計日期’,
wastage_count bigint COMMENT ‘流失設備數’
)
row format delimited fields terminated by ‘\t’
location ‘/warehouse/gmall/ads/ads_wastage_count’;
2)導入2019-02-20數據
hive (gmall)>
insert into table ads_wastage_count
select
‘2019-02-20’,
count(*)
from
(
select mid_id
from dws_uv_detail_day
group by mid_id
having max(dt)<=date_add(‘2019-02-20’,-7)
)t1;
12.3 編寫腳本
1)在hadoop102的/home/atguigu/bin目錄下創建腳本
[atguigu@hadoop102 bin]$ vim ads_wastage_log.sh
在腳本中編寫如下內容
#!/bin/bash

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

hive=/opt/module/hive/bin/hive
APP=gmall

echo “-----------導入日期$do_date-----------”

sql="
insert into table "APP".adswastagecountselectAPP".ads_wastage_count select 'do_date’,
count(*)
from
(
select mid_id
from "APP".dwsuvdetaildaygroupbymididhavingmax(dt)<=dateadd(APP".dws_uv_detail_day group by mid_id having max(dt)<=date_add('do_date’,-7)
)t1;
"

hivee"hive -e "sql"
2)增加腳本執行權限
[atguigu@hadoop102 bin]$ chmod 777 ads_wastage_log.sh
3)腳本使用
[atguigu@hadoop102 module]$ ads_wastage_log.sh 2019-02-20
4)查詢結果
hive (gmall)> select * from ads_wastage_count;
5)腳本執行時間
企業開發中一般在每日凌晨30分~1點

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章