综合项目:电商数据仓库之用户行为数仓4
1 | 截止目前hdfs上,用户行为数据目录结构的样子 |


用户行为数据数仓开发

1 | 数据仓库分为 4层:ods层、dwd层、dws层、app层, |
1 | 注意: |
ods 层
1 | 创建ods层的表 |
1 | create external table if not exists ods_mall.ods_user_active( # 都使用的数据库.表名,这样后期就不用来回切换当前数据库 |
ods层抽取脚本
1 | 针对ods层抽取脚本 |
ods_mall_init_table.sh
1 | #!/bin/bash |
add_partition.sh
1 | #!/bin/bash |
ods_mall_add_partition.sh
1 | #!/bin/bash |
dwd层
1 | 针对 ods层表中的数据进行清洗,参考数据清洗规则,按照实际情况对数据进行清洗 |
1 | 创建dwd层的表 |
1 | 2:hive中的timestamp只能解析yyyy-MM-dd HH:MM:SS格式的数据,所以针对这里面的acttime字段我们使用bigint类型 |
1 | create external table if not exists dwd_mall.dwd_user_active( |
dwd层抽取脚本
1 | 1:表初始化脚本(初始化执行一次) |
dwd_mall_init_table.sh
1 | #!/bin/bash |
dwd_mall_add_partition.sh
1 | #!/bin/bash |
需求分析
1 | 前面的两层中的表和需求一般没什么关系,就是把已有的数据接入进来,然后对数据进行清洗处理 |
tmp_load_ods_data.sh
1 | 1:执行ods层的脚本 |
1 | #!/bin/bash |
tmp_load_dwd_data.sh
1 | #!/bin/bash |

需求一:每日新增用户相关指标
1 | 在统计新增用户时,用户是以设备标识(xaid字段)来判断的,每一个设备都有一个唯一设备码,因为会存在用户不登录的情况,以及多人共用一个账号的情况,所以根据用户id进行统计是不准确的。 |
1 | 先详细分析第1个指标,每日新增用户量 |
每日新增用户量
1 | 举个例子: |
每日新增用户量的日环比和周同比
1 | 接下来是第2个指标,每日新增用户量的日环比和周同比 |
1 | 总结: |
dws
1 | create table if not exists dws_mall.dws_user_active_20260201_tmp( |
1 | 针对 dws 层抽取脚本 |
dws_mall_init_table_1.sh
1 | !/bin/bash |
dws_mall_add_partition_1.sh
1 | !/bin/bash |
app




1 | create external table if not exists app_mall.app_user_new_count( |
app_mall_init_table_1.sh
1 | !/bin/bash |
app_mall_add_partition_1.sh
1 | !/bin/bash |


需求二:每日活跃用户(主活)相关指标
1 | 活跃用户的定义是指只要当天使用过APP就算是活跃用户,使用APP这种操作属于主动操作,所以这种活跃我们也会称为主动活跃,简称主活 |
1 | create external table if not exists app_mall.app_user_new_count( |
1 | 针对 app层抽取脚本 |
app_mall_init_table_2.sh
1 | !/bin/bash |
app_mall_add_partition_2.sh
1 | !/bin/bash |



需求扩展
1 | 如何统计每周主活?每月主活? |
需求三:用户7日流失push提醒
1 | 什么是流失呢? |
1 | 实现思路: |
dws
1 | create external table if not exists dws_mall.dws_user_lost_item( |
提取脚本
1 | 针对 dws 层抽取脚本 |
dws_mall_init_table_3.sh
1 | !/bin/bash |
dws_mall_add_partition_3.sh
1 | !/bin/bash |
app
1 | create external table if not exists app_mall.app_user_lost_count( |
提取脚本
1 | 针对 app层抽取脚本 |
app_mall_init_table_3.sh
1 | !/bin/bash |
app_mall_add_partition_3.sh
1 | !/bin/bash |



需求四:每日启动App次数相关指标
1 | 这个需求就是对每日打开 app上报的数据进行统计 |
app
1 | create external table if not exists app_mall.app_user_open_app_count( |
开发脚本
1 | 针对 app 层抽取脚本 |
app_mall_init_table_4.sh
1 | !/bin/bash |
app_mall_add_partition_4.sh
1 | !/bin/bash |


需求五:操作系统活跃用户相关指标]
1 | 这个需求是统计一下我们产品的目前主要用户群体是使用什么类型的操作系统 |
1 | 实现思路如下: |
1 | 注意了,咱们前面保存的有每天聚合的数据,如果后期有需求要统计一段时间内的这些维度的指标,那也很简单,直接基于dws层的表进行统计即可,从这也体现出来了数据分层的好处。 |
dws

1 | create external table if not exists dws_mall.dws_user_platform_distrib( |
开发脚本
dws_mall_init_table_5.sh
1 | !/bin/bash |
dws_mall_add_partition_5.sh
1 | !/bin/bash |


app
1 | create external table if not exists app_mall.app_user_platform_distrib( |
开发脚本
1 | 针对 app层抽取脚本 |
app_mall_init_table_5.sh
1 | !/bin/bash |
app_mall_add_partition_5.sh
1 | !/bin/bash |



需求六:APP 崩溃相关指标
1 | 这个需求是统计在不同平台系统不同版本下APP崩溃的情况,统计这个数据可以方便排查定位问题,如果发现某一个版本的APP崩溃频繁,则需要及时修复问题,推送新版本,提升用户体验。 |
1 | 基于dws_app_close_platform_vercode表中的数据就可以计算出来这三个指标了。 |
dws层
1 | create external table if not exists dws_mall.dws_app_close_platform_vercode( |
开发脚本
1 | 针对 dws 层抽取脚本 |
dws_mall_init_table_6.sh
1 | !/bin/bash |
dws_mall_add_partition_6.sh
1 | !/bin/bash |

app层
1 | create external table if not exists app_mall.app_app_close_platform_all( |
开发脚本
app_mall_init_table_6.sh
1 | !/bin/bash |
app_mall_add_partition_6.sh
1 | !/bin/bash |




用户行为数据数仓总结
数据库和表梳理

任务脚本梳理
