9(23)連續三天活躍用戶數23

第14章 需求八:最近七天內連續三天活躍用戶數
說明:最近7天內連續3天活躍用戶數
14.1 DWS層
使用日活明細表dws_uv_detail_day作爲DWS層數據
14.2 ADS層
1)建表語句
hive (gmall)>
drop table if exists ads_continuity_uv_count;
create external table ads_continuity_uv_count(
dt string COMMENT ‘統計日期’,
wk_dt string COMMENT ‘最近7天日期’,
continuity_count bigint
) COMMENT ‘連續活躍設備數’
row format delimited fields terminated by ‘\t’
location ‘/warehouse/gmall/ads/ads_continuity_uv_count’;
2)寫出導入數據的SQL語句
hive (gmall)>
insert into table ads_continuity_uv_count
select
‘2019-02-12’,
concat(date_add(‘2019-02-12’,-6),’_’,‘2019-02-12’),
count()
from
(
select mid_id
from
(
select mid_id
from
(
select
mid_id,
date_sub(dt,rank) date_dif
from
(
select
mid_id,
dt,
rank() over(partition by mid_id order by dt) rank
from dws_uv_detail_day
where dt>=date_add(‘2019-02-12’,-6) and dt<=‘2019-02-12’
)t1
)t2
group by mid_id,date_dif
having count(
)>=3
)t3
group by mid_id
)t4;
(5)查詢
hive (gmall)> select * from ads_continuity_uv_count;
14.3 編寫腳本
1)在hadoop102的/home/atguigu/bin目錄下創建腳本
[atguigu@hadoop102 bin]$ vim ads_continuity_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".adscontinuityuvcountselectAPP".ads_continuity_uv_count select 'do_date’,
concat(date_add(‘KaTeX parse error: Expected group after '_' at position 15: do_date',-6),'_̲','do_date’) dt,
count()
from
(
select mid_id
from
(
select mid_id
from
(
select
mid_id,
date_sub(dt,rank) date_diff
from
(
select
mid_id,
dt,
rank() over(partition by mid_id order by dt) rank
from "APP".dwsuvdetaildaywheredt>=dateadd(APP".dws_uv_detail_day where dt>=date_add('do_date’,-6) and dt<=’$do_date’
)t1
)t2
group by mid_id,date_diff
having count(
)>=3
)t3
group by mid_id
)t4;
"

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

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