一、用户信息宽表分析
宽表主要是便于使用,在使用的时候不至于每次都要关联很多张表。
用户信息宽表包括服务端中的user表、user_extend表。
如果有需求的话其实还可以把用户的一些其他维度的数据关联过来,例如:当日的下单数量、消费金额等等指标。
二、思路
对dwd_user表和dwd_user_extend表执行left join操作,通过user_id进行关联即可,将结果数据保存到dws_user_info_all表中。
三、dws层
1、dws_user_info_all
(1)源表
SQL
(2)建表语句
create external table if not exists dws_mall.dws_user_info_all(
user_id bigint,
user_name string,
user_gender tinyint,
user_birthday string,
e_mail string,
mobile string,
register_time string,
is_blacklist tinyint,
is_pregnant_woman tinyint,
is_have_children tinyint,
is_have_car tinyint,
phone_brand string,
phone_cnt int,
change_phone_cnt int,
weight int,
height int
)partitioned by(dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://bigdata01:9000/data/dws/user_info_all/';
(3)映射关系
insert overwrite table dws_mall.dws_user_info_all partition(dt='20260201') select
du.user_id,
du.user_name,
du.user_gender,
du.user_birthday,
du.e_mail,
du.mobile,
du.register_time,
du.is_blacklist,
due.is_pregnant_woman,
due.is_have_children,
due.is_have_car,
due.phone_brand,
due.phone_cnt,
due.change_phone_cnt,
due.weight,
due.height
from dwd_mall.dwd_user as du
left join dwd_mall.dwd_user_extend as due
on du.user_id = due.user_id
where du.dt = '20260201' and due.dt = '20260201';
四、开发脚本
1、表初始化脚本(初始化执行一次)
dws_mall_init_table_1.sh
内容如下:
#!/bin/bash
# 需求一:用户信息宽表
# dws层数据库和表初始化脚本,只需要执行一次即可
hive -e "
create database if not exists dws_mall;
create external table if not exists dws_mall.dws_user_info_all(
user_id bigint,
user_name string,
user_gender tinyint,
user_birthday string,
e_mail string,
mobile string,
register_time string,
is_blacklist tinyint,
is_pregnant_woman tinyint,
is_have_children tinyint,
is_have_car tinyint,
phone_brand string,
phone_cnt int,
change_phone_cnt int,
weight int,
height int
)partitioned by(dt string)
row format delimited
fields terminated by '\t'
location 'hdfs://bigdata01:9000/data/dws/user_info_all/';
"
2、添加分区脚本(每天执行一次)
dws_mall_add_partition_1.sh
内容如下:
#!/bin/bash
# 需求一:用户信息宽表
# 每天凌晨执行一次
# 默认获取昨天的日期,也支持传参指定一个日期
if [ "z$1" = "z" ]
then
dt=`date +%Y%m%d --date="1 days ago"`
else
dt=$1
fi
hive -e "
insert overwrite table dws_mall.dws_user_info_all partition(dt='${dt}') select
du.user_id,
du.user_name,
du.user_gender,
du.user_birthday,
du.e_mail,
du.mobile,
du.register_time,
du.is_blacklist,
due.is_pregnant_woman,
due.is_have_children,
due.is_have_car,
due.phone_brand,
due.phone_cnt,
due.change_phone_cnt,
due.weight,
due.height
from dwd_mall.dwd_user as du
left join dwd_mall.dwd_user_extend as due
on du.user_id = due.user_id
where du.dt = '${dt}' and due.dt = '${dt}';
"
五、执行脚本
1、先执行初始化脚本
sh dws_mall_init_table_1.sh
2、再执行添加分区脚本
sh dws_mall_add_partition_1.sh 20260201
稍等一会,如下就好了:
六、验证结果
select * from dws_mall.dws_user_info_all where dt = '20260201' limit 1;
七、思考一个问题
如何将服务端的数据和客户端的数据通过用户维度关联起来?
之前统计的客户端的数据(用户行为数据)针对用户相关的指标都是使用xaid。
但是在服务端数据中用户信息只有user_id。
这两份数据如果先要关联起来,还需要在用户行为数据仓库提取一个表,表里面只需要有两列:user_id和xaid。
这样就可以把客户端数据和服务端数据中的用户关联起来了。