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点

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