第10章 需求四:沉默用戶數
沉默用戶:指的是隻在安裝當天啓動過,且啓動時間是在一週前
10.1 DWS層
使用日活明細表dws_uv_detail_day作爲DWS層數據
10.2 ADS層
1)建表語句
hive (gmall)>
drop table if exists ads_slient_count;
create external table ads_slient_count(
dt
string COMMENT ‘統計日期’,
slient_count
bigint COMMENT ‘沉默設備數’
)
row format delimited fields terminated by ‘\t’
location ‘/warehouse/gmall/ads/ads_slient_count’;
2)導入2019-02-20數據
hive (gmall)>
insert into table ads_slient_count
select
‘2019-02-20’ dt,
count() slient_count
from
(
select mid_id
from dws_uv_detail_day
where dt<=‘2019-02-20’
group by mid_id
having count()=1 and min(dt)<date_add(‘2019-02-20’,-7)
) t1;
3)查詢導入數據
hive (gmall)> select * from ads_slient_count;
10.3 編寫腳本
1)在hadoop102的/home/atguigu/bin目錄下創建腳本
[atguigu@hadoop102 bin]$ vim ads_slient_log.sh
在腳本中編寫如下內容
#!/bin/bash
hive=/opt/module/hive/bin/hive
APP=gmall
if [ -n “$1” ];then
do_date=$1
else
do_date=date -d "-1 day" +%F
fi
echo “-----------導入日期$do_date-----------”
sql="
insert into table “do_date’ dt,
count() slient_count
from
(
select
mid_id
from "do_date’
group by mid_id
having count()=1 and min(dt)<=date_add(’$do_date’,-7)
)t1;”
sql"
2)增加腳本執行權限
[atguigu@hadoop102 bin]$ chmod 777 ads_slient_log.sh
3)腳本使用
[atguigu@hadoop102 module]$ ads_slient_log.sh 2019-02-20
4)查詢結果
hive (gmall)> select * from ads_slient_count;
5)腳本執行時間
企業開發中一般在每日凌晨30分~1點