Hive统计新增,日活和留存率

来源:CSDN博客 作者:jinshiyill

用户行为触发的日志上报,已经存放在Hive的外部分区表中. 结构如下:

blob.png

主要字段内容

dt表示日期,如20160510

platform表示平台,只有两个选项,苹果和安卓

mid是用户机器码,类似于网卡MAC地址什么的

pver是版本

channel是分发渠道

现在产品经理需要统计每天用户的新增,日活和留存率.

其中

留存率的概念是,如果用户在5月1日第一次使用我们的产品。

如果5月2日他还使用了,那么5月1日的“一日留存”加一.

同理5月3日他又使用率,5月1日的“两日留存”加一.

5月1日的“一日留存率”=5月1日“一日留存” / 5月1日新增用户数量.

blob.png

先创建一个表,记录用户首次使用的日期.

dt是用户首次使用的日期,比如 20160510

cver是版本

pcid是用户机器码,就是原始日志表的mid

然后创建一个每天数据的存放表,统计昨天一天的新增,激活和留存.

blob.png

dt是日期

type 1:新增 2:留存 3:日活

num 是用户数量,

dtdiff仅仅用于计算留存,说明用户使用和首次使用的日期间隔多少天.

1.Hive统计每天新增用户

$dt是shell传入的变量

dt=$(date -d last-day +%Y%m%d)

该脚本每天凌晨执行,统计昨天的数据.

每次执行,先清空report_userinfo表

truncate table report_userinfo;

insert into user_login_history   

select platform,min(dt),channel,cver,mid,1 from log_vvim  

where   

mid not in (select pcid from user_login_history where type=1)  

and mid is not null  

and dt=$dt  

group by platform,channel,cver,mid;  

这个意思就是 原来没有记录在user_grouproom_login_history表中的pcid,java如果出现在昨天的日志表中,则说明用户是新增的.

然后将昨天新增的用户数量写入

insert into report_userinfo   

select platform,dt,channel,cver,type,count(*) num,-1 from user_login_history    

where type=1   

and dt=$dt  

group by platform,dt,channel,cver,type; 

2.统计每天激活用户数量

insert into report_userinfo   

select platform,dt,channel,cver,3,count(distinct mid),-1 from log_vvim   

where   

mid is not null and dt=$dt  

group by dt,platform,channel,cver;  

这个倒是简单,根据原始的日志表,统计今天使用过的pcid,经过去重,java培训中的用户就是今天的日活用户量.

3.统计留存率.

insert into report_userinfo   

select   

xinzeng.platform,  

xinzeng.dt,  

xinzeng.channel,  

xinzeng.cver,  

2,  

count(distinct cunliu.pcid),  

datediff(  

    from_unixtime(unix_timestamp(cast(cunliu.dt as string),'yyyyMMdd')),  

    from_unixtime(unix_timestamp(cast(xinzeng.dt as string),'yyyyMMdd'))  

)  

from  

(  

    select * from user_login_history where type=1  

) xinzeng  

inner join   

(  

    select   

    platform,  

    dt,  

    channel,  

    cver,  

    mid pcid   

    from log_vvim   

    where mid is not null and dt=$dt group by platform,dt,channel,cver,mid  

) cunliu on  

(  

    xinzeng.platform=cunliu.platform and  

    xinzeng.channel=cunliu.channel and  

    xinzeng.cver=cunliu.cver and  

    xinzeng.pcid=cunliu.pcid  

)  

where cunliu.dt>xinzeng.dt   

group by   

xinzeng.platform,xinzeng.dt,xinzeng.channel,xinzeng.cver,  

datediff(  

    from_unixtime(unix_timestamp(cast(cunliu.dt as string),'yyyyMMdd')),  

    from_unixtime(unix_timestamp(cast(xinzeng.dt as string),'yyyyMMdd'))  

);  

该SQL主要计算昨天使用过的用户,他的首次使用日期,然后计算差值

blob.png

表示安卓平台,20160425那天首次使用的用户,在8天之后,还使用过的用户数量为20人。

因为计算新增和日活在计算留存之前,

cunliu.dt>xinzeng.dt

主要是确定当天新增的用户不计入留存率计算.

统计完成之后,将hive表导入MySQL

sqoop export --connect jdbc:mysql://IP:端口/report --username uname --password "pwd" --table report_userinfo --export-dir '/user/hive/warehouse/logs.db/report_userinfo' --fields-terminated-by '\001'

最终通过报表展现


相关文档推荐

SRE Copilot大语言模型智能运维框架.PDF

1741936996 王宁 5.04MB 24页 积分6

2024智算运维发展研究报告.PDF

1740033222  1.71MB 30页 积分5

腾讯大数据基于StarRocks的向量检索探索.PDF

1737425434 赵裕隆 3.48MB 34页 积分6

B站一站式大数据集群管理平台.PDF

1737421412 刘明刚 1.37MB 30页 积分6

StarRocks在爱奇艺大数据场景的实践.PDF

1737365327 林豪 3.57MB 27页 积分5

智算平台运维运营技术研究报告.PDF

1736479643  3.95MB 66页 积分5

农业农村大数据平台互联互通规范.PDF

1736163860  0.49MB 11页 积分5

工业大数据管理与治理智能制造的基座.PDF

1733702095 王宏志 3.83MB 54页 积分6

相关文章推荐

运维指标体系在银行业务的应用实践

CIO之家的朋友 CIO之家的朋友 

面向业务应用交易的IT运维监控思路

CIO之家的朋友们 张晓丹 

云计算的成本与价值概要分析

数字地平线 8小时coding 

运维85条军规

CIO之家的朋友们 ANZHIHE 

AIOps之前,运维层面能做什么

嘉为蓝鲸? 赵海兵