营销型网站建设xywlcn,软件开发平台 devcloud,查域名注册详细信息查询,水果网店网站建设策划书拉链表
一、概念
拉链表是针对数据仓库设计中表存储数据的方式而定义的#xff0c;所谓拉链#xff0c;就是记录历史。记录一个事物从开始#xff0c;一直到当前状态的所有变化的信息。
用处#xff1a; 解决持续增长且存在一定时间时间范围内重复的数据 场景#xff1…拉链表
一、概念
拉链表是针对数据仓库设计中表存储数据的方式而定义的所谓拉链就是记录历史。记录一个事物从开始一直到当前状态的所有变化的信息。
用处 解决持续增长且存在一定时间时间范围内重复的数据 场景 数据规模庞大新数据【在有限的时间】内存在多种状态变化 原来解决方案 采用分区表用户分区存储历史增量数据缺点是重复数据太多 优点 节约空间
二、拉链表的设计
以订单为例:
普通表存每天数据
order_id bigint, -- 订单id
user_id bigint, -- 订单创建时间
order_modify_dt timestamp, --状态更改时间
order_money decimal(10,2), --订单价格
current_status int --订单状态每次存储某一天数据
拉链(分区分桶表)
order_id bigint,
user_id bigint,
order_create_dt timestamp,
order_modify_dt timestamp,
order_money decimal(10,2),
current_status int将初始数据装如拉链表
用某一天部分数据更新拉链表新订单生成旧订单修改
三、拉链表的实现
创建普通表存原始数据
create table hive_zipper_order(order_id bigint,user_id bigint,order_modify_dt timestamp,order_money decimal(10,2),current_status int
)
row format delimited fields terminated by ,;
// 将数据文件导入原始表格
load data local inpath /root/data/order_record.log
overwrite into table hive_zipper_order;创建拉链表
//操作历史全量数据用动态分区
set hive.support.concurrencytrue; -- hive支持
set hive.enforce.bucketingtrue; -- hive强制分桶
set hive.exec.dynamic.partition.modenonstrict; --动态分区 分区表 给首次将大量数据导入使用
set hive.txn.managerorg.apache.hadoop.hive.ql.lockmgr.DbTxnManager; --事务管理器
set hive.compactor.initiator.ontrue; -- 表合并开启
set hive.compactor.worker.threads1; -- 表合并线程必须为一
set hive.auto.convert.joinfalse; -- 关闭 mapjoin只能是reducejoin
set hive.merge.cardinality.checkfalse; -- 关闭检查数据列的基数(列值的差异性)// 创建拉链表
drop table if exists hive_zipper_pc_order;
create table hive_zipper_pc_order(order_id bigint,user_id bigint,order_create_dt timestamp,order_modify_dt timestamp,order_money decimal(10,2),current_status int
) partitioned by(year int,month int,day int)
clustered by(order_create_dt) into 4 buckets
row format delimited fields terminated by ,
stored as orc tblproperties(transactionaltrue);开启动态分区一次性挂载
// 对拉链表的数据进行聚合获取订单信息的创建日期、修改日期和订单状态
with zip_src as (select order_id,user_id,order_money,min(order_modify_dt) as order_create_dt,max(order_modify_dt) as order_modify_dt,max(current_status) as current_statusfrom hive_zipper_ordergroup by order_id,user_id,order_money
)
// 将原始数据灌入拉链表
insert overwrite table hive_zipper_pc_order partition(year,month,day)
select order_id,user_id,order_create_dt,order_modify_dt,order_money,current_status,year(order_create_dt) as year,month(order_create_dt) as month,day(order_create_dt) as day
from zip_src;拉链表查询
// 拉链表查询 查询之前必须先有这两句配置
set hive.txn.managerorg.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
set hive.support.concurrencytrue;select * from hive_zipper_pc_order
where to_date(order_modify_dt)2021-02-04
order by order_modify_dt desc;之后每天增量添加
// 对于追加增量数据将增量数据覆盖在原始数据表中
load data local inpath /root/data/order_record_2021_02_05.log
overwrite into table hive_zipper_order;拉链处理增量数据(新增新数据修改旧数据)
// 将原始数据表中的增量数据插入拉链表
// 利用源数据和目标表的order_id进行匹配若匹配则更新现有订单信息若不匹配则插入新订单
merge into hive_zipper_pc_order as O
using (select order_id,user_id,order_create_dt,order_modify_dt,order_money,current_status,year(order_create_dt) as year,month(order_create_dt) as month,day(order_create_dt) as dayfrom (select order_id,user_id,order_money,min(order_modify_dt) as order_create_dt,max(order_modify_dt) as order_modify_dt,max(current_status) as current_statusfrom hive_zipper_order--where to_date(order_modify_dt)2021-02-05group by order_id,user_id,order_money)T
) as H
on O.order_idH.order_id
when matched then
update set order_modify_dtH.order_modify_dt,current_statusH.current_status
when not matched then
insert values(H.order_id,H.user_id,H.order_create_dt,H.order_modify_dt,H.order_money,H.current_status,H.year,H.month,H.day);验证拉链结果
select * from hive_zipper_pc_order
where to_date(order_modify_dt)to_date(order_create_dt);数据仓库_缓慢渐变维_拉链表全揭秘_拉链表中的代理主键-CSDN博客
拉链表_什么是拉链表-CSDN博客