用户维度表(拉链表)

步骤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_info
    where 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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值