拉链表:维护历史状态与最新状态数据的一种表
#1.应用场景:
(1)有一些表的数据量很大,不能经常覆盖
(2)较短有效时间内,同时存在修改和新增两种情况
2.实现流程: · 日期记录最新修改时间,用状态值来表示这一时间节点下的状态
通常在数仓的ods层设计一张历史记录表,这张历史记录表就是按照固定的周期从关系型数据库中,把最新的数据给采过来,这张历史数据表在第一次采集的时候会把所有的历史记录采集过来,以后每次的操作都是基于历史记录向下操作(第一次全量采集,第二次开始增量采集),采用覆盖的模式,不存历史数据。
在dwd 层,设计一张截止到当前的最新记录表,这张表要记录所有截止到当下的最新数据,包括新增数据和对历史记录修改的数据,而且对于拉链表的设计,我们需要这张表是分区表,存储格式可以带压缩格式,同时这张表需要开启事务的支持tblproperties(“transactional”=“true”),此表设计之后在这张表中会出现另一个日期或日期+状态的字段,从而以最小的代价记录每一条数据最新的状态。
在dwd表中加1个字段还是2个字段
3.技术实现细节:代码操作之前要设置hive运行环境,
开启分区(设置动态模式)
set hive.exec.dynamic.partition=true
set hive.exec.dynamic.partition.mode=nonstrict
同时开启分桶
set hive.enforce.bucketing=true;
开启事务的服务:临时把事务开启
采用merge into对历史记录不存在的通过关联字段去做检验(相同主键),例如订单编号存在了就更新,不存在就新增
#拉链表具体实现:
#每日订单全量导入
sqoop import --connect jdbc:mysql://101.132.47.155:3306/db_shop12 --driver com.mysql.jdbc.Driver --username root --password kb12 --query "select O.order_id,O.order_user_id,O.order_create_date,S.order_status,S.order_modify_date from user_order O inner join user_order_status S on O.order_id=S.fk_order_id and date(O.order_create_date)<=adddate(current_date(),-1) and date(S.order_modify_date)<=adddate(current_date(),-1) where $CONDITIONS" -m 4 --delete-target-dir --target-dir '/user/hive/warehouse/ods_shop12/ods_history_order' --split-by order_id --hive-import --hive-table ods_shop12.ods_history_order
#创建拉链表
#开启事务
set mapreduce.job.reduces=4;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.enforce.bucketing=true;
set hive.support.concurrency=true;
set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
set hive.compactor.initiator.on=true;
set hive.compactor.worker.threads=1;
set hive.auto.convert.join=false;
set hive.merge.cardinality.check=false;
#创建dwd库
create database dwd_shop12;
create table dwd_user_order(
order_id int,
order_user_id int ,
order_create_date string ,
order_status int,
order_modify_date string
)
partitioned by(today string)
clustered by(order_id) into 4 buckets
row format delimited
fields terminated by ','
stored as orc
tblproperties("transactional"="true");
#首次将所有历史数据写入分区
insert into table dwd_shop12.dwd_user_order partition(today)
select order_id,order_user_id,order_create_date,order_status,order_modify_date,current_date()today from ods_shop12.ods_history_order
#每日订单增量导入
sqoop import
--connect jdbc:mysql://101.132.47.155:3306/db_shop12
--driver com.mysql.jdbc.Driver
--username root
--password kb12
--query "select O.order_id,O.order_user_id,O.order_create_date,S.order_status,S.order_modify_date from user_order O inner join user_order_status S on O.order_id=S.fk_order_id and date(O.order_create_date)=current_date() and date(S.order_modify_date)=current_date() where $CONDITIONS"
-m 4
--delete-target-dir
--target-dir '/user/hive/warehouse/ods_shop12.db/ods_history_order'
--split-by order_id
--hive-import
--hive-table ods_shop12.ods_history_order
#merge into
merge into dwd_shop12.dwd_user_order as A
using ods_shop12.ods_history_order as B
on date(A.order_create_date)>=date_add(current_date(),-21) and A.order_id=B.order_id and A.order_status


