本文主要是介绍用户维度表(拉链表),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
步骤0:初始化拉链表(首次独立执行)
(1)建立拉链表
hive (gmall)>
drop table if exists dwd_dim_user_info_his;
create external table dwd_dim_user_info_his(
id
string COMMENT ‘用户id’,
name
string COMMENT ‘姓名’,
birthday
string COMMENT ‘生日’,
gender
string COMMENT ‘性别’,
email
string COMMENT ‘邮箱’,
user_level
string COMMENT ‘用户等级’,
create_time
string COMMENT ‘创建时间’,
operate_time
string COMMENT ‘操作时间’,
start_date
string COMMENT ‘有效开始日期’,
end_date
string COMMENT ‘有效结束日期’
) COMMENT ‘用户拉链表’
stored as parquet
location ‘/warehouse/gmall/dwd/dwd_dim_user_info_his/’
tblproperties (“parquet.compression”=“lzo”);
(2)初始化拉链表
hive (gmall)>
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
insert overwrite table dwd_dim_user_info_his
select
id,
name,
birthday,
gender,
email,
user_level,
create_time,
operate_time,
‘2020-06-14’,
‘9999-99-99’
from ods_user_info oi
where oi.dt=‘2020-06-14’;
步骤1:制作当日变动数据(包括新增,修改)每日执行
(1)如何获得每日变动表
a.最好表内有创建时间和变动时间(Lucky!)
b.如果没有,可以利用第三方工具监控比如canal,监控MySQL的实时变化进行记录(麻烦)。
c.逐行对比前后两天的数据,检查md5(concat(全部有可能变化的字段))是否相同(low)
d.要求业务数据库提供变动流水(人品,颜值)
(2)因为ods_user_info本身导入过来就是新增变动明细的表,所以不用处理
a)数据库中新增2020-06-15一天的数据
b)通过Sqoop把2020-06-15日所有数据导入
mysql_to_hdfs.sh all 2020-06-15
c)ods层数据导入
hdfs_to_ods_db.sh all 2020-06-15
步骤2:先合并变动信息,再追加新增信息,插入到临时表中
1)建立临时表
hive (gmall)>
drop table if exists dwd_dim_user_info_his_tmp;
create external table dwd_dim_user_info_his_tmp(
id
string COMMENT ‘用户id’,
name
string COMMENT ‘姓名’,
birthday
string COMMENT ‘生日’,
gender
string COMMENT ‘性别’,
email
string COMMENT ‘邮箱’,
user_level
string COMMENT ‘用户等级’,
create_time
string COMMENT ‘创建时间’,
operate_time
string COMMENT ‘操作时间’,
start_date
string COMMENT ‘有效开始日期’,
end_date
string COMMENT ‘有效结束日期’
) COMMENT ‘订单拉链临时表’
stored as parquet
location ‘/warehouse/gmall/dwd/dwd_dim_user_info_his_tmp/’
tblproperties (“parquet.compression”=“lzo”);
2)导入脚本
hive (gmall)>
SET hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
insert overwrite table dwd_dim_user_info_his_tmp
select * from
(
select
id,
name,
birthday,
gender,
email,
user_level,
create_time,
operate_time,
‘2020-06-15’ start_date,
‘9999-99-99’ end_date
from ods_user_info where dt=‘2020-06-15’
union all
select uh.id,uh.name,uh.birthday,uh.gender,uh.email,uh.user_level,uh.create_time,uh.operate_time,uh.start_date,if(ui.id is not null and uh.end_date='9999-99-99', date_add(ui.dt,-1), uh.end_date) end_date
from dwd_dim_user_info_his uh left join
(select*from ods_user_infowhere dt='2020-06-15'
) ui on uh.id=ui.id
)his
order by his.id, start_date;
步骤3:把临时表覆盖给拉链表
1)导入数据
hive (gmall)>
insert overwrite table dwd_dim_user_info_his
select * from dwd_dim_user_info_his_tmp;
2)查询导入数据
hive (gmall)>
select id, start_date, end_date from dwd_dim_user_info_his limit 2;
这篇关于用户维度表(拉链表)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!