我们在开发画像标签的过程中会遇到如下的维度:
- 客户首次进入app**模块的日期
- 客户最后一次进入app**模块的日期
- 客户在app**模块的累计的活跃时长
- 客户在app**模块的累计活跃次数
- 客户在app**模块的累计活跃天数
核心思想是用用今天的数据和历史数据滚动式迭代的方式计算,示例代码如下:
任务A存储着用户当天的数据;
create table A(
phone_no string comment ‘客户手机号码’
,active_duration_1day string comment ‘当天活跃时长’
,active_cnt_1day string comment '当天活跃次数'
) partition by (day string );
任务B从任务A中取数,实现以上的标签计算:
insert overwrite table B partition(day='date' )
select
phone_no as phone_no --客户手机号码
,min(first_enter_date ) as first_enter_date --首次进入日期
,max(last_enter_date ) as last_enter_date --最后一次进入日期
,sum( active_duration_acc) as active_duration_acc --累计活跃时长
,sum(active_cnt_acc ) as active_cnt_acc --累计活跃次数
,sum( active_days_acc ) as active_days_acc --累计活跃天数
from
(select
phone_no
,first_enter_date
,last_enter_date
,active_duration_acc
,active_cnt_acc
,active_days_acc
FROM B where day=date_sub(date,1)
union all
select
phone_no
'date' as first_enter_date
,'date' as last_enter_date
,active_duration_1day as active_duration_acc
,active_cnt_1day as active_cnt_acc
,'1' as active_days_acc
from A where day='date'
)t group by phone_no