hive拉链表实现的四种方式_拉链表实现方式(3)

inner join(select id,max(h_update_dt) as h_update_dt from t20240306 where h_update_dt<=20230108 group by id) t2
on t1.id=t2.id and t1.h_update_dt=t2.h_update_dt


![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/037e3999786c4565b0dbc0a1a9c8368c.png)  
 这种方法对开发人员来说是友好的,但是对分析人员来说,使用起来就比较麻烦点,需要两两关联才能得到想要结果。


**4.以方案3为数据存储,方案2为对外查询** 这种方案针对存储要求不是很苛刻的应用场景,可以兼顾方案2和方案3的优点.创建一个增加时间戳的表ods.t20240312\_ht,用于记录所有变更的数据,以这个为主表,每天全量更新增加生命周期的拉链表ods.t20240312\_hs



------------------创建表和插入测试数据
create table ods.t20240312(id int,name string,mark string,l_batch_date string);
create table ods.t20240312_ht(id int,name string,mark string,l_is_del string,l_batch_date string);
create table ods.t20240312_hs(id int,name string,mark string,l_is_del string,h_start_dt date,h_end_dt date);

insert into ods.t20240312 values(1,‘张三1’,‘test1’,‘20240101’),(2,‘张三2’,‘test2’,‘20240102’),(3,‘张三3’,‘test3’,‘20240103’),(4,‘张三4’,‘test4’,‘20240104’),(5,‘张三5’,‘test5’,‘20240105’),(6,‘张三6’,‘test6’,‘20240106’);
insert overwrite table ods.t20240312 values(1,‘张三1’,‘test1 01’,‘20240108’),(7,‘张三7’,‘test7’,‘20240110’),(4,‘张三4’,‘test4’,‘20240104’),(5,‘张三5’,‘test5’,‘20240105’),(6,‘张三6’,‘test6’,‘20240106’);

--------------------------执行ods层执行包含时间戳表的文件sql
—获取前一天数据
drop table if exists ods.t_t20240312_ht_01;

create table ods.t_t20240312_ht_01 as
select t1.* from ods.t20240312_ht t1
inner join(select id,max(l_batch_date) as l_batch_date from ods.t20240312_ht where l_batch_date<‘20240109’ group by id) t2
on t1.id=t2.id and t1.l_batch_date=t2.l_batch_date and t1.l_is_del<>‘1’;

—获取最新的拉链表
drop table if exists ods.t_t20240312_ht_02;
create table ods.t_t20240312_ht_02 as
select t1.* from ods.t20240312_ht t1 where t1.l_batch_date<>‘20240109’
union all
select nvl(t1.id,t2.id),nvl(t1.name,t2.name),nvl(t1.mark,t2.mark),case when t1.id is null then ‘1’ else ‘0’ end as l_is_del,‘20240109’ as l_batch_date from ods.t20240312 t1
full join ods.t_t20240312_ht_01 t2 on t1.id=t2.id and t1.l_batch_date<=‘20240109’
where t2.id is null
or t1.id is null
or t1.name<>t2.name
or t1.mark<>t2.mark
;

—替换更新拉链表
insert overwrite table ods.t20240312_ht
select *from ods.t_t20240312_ht_02;

--------------------------执行ods层执行包含生命周期表的文件sql
insert overwrite table ods.t20240312_hs
select
b.id,b.name,b.mark,b.l_is_del
, to_date(from_unixtime(unix_timestamp(cast(b.l_batch_date as string), ‘yyyyMMdd’))) as h_start_dt
,case when b.l_is_del=‘1’ then to_date(from_unixtime(unix_timestamp(cast(b.l_batch_date as string), ‘yyyyMMdd’))) when b.lead_end_dt is null then to_date(from_unixtime(unix_timestamp(‘29991231’, ‘yyyyMMdd’))) else to_date(from_unixtime(unix_timestamp(cast(b.lead_end_dt as string), ‘yyyyMMdd’))) end as h_end_dt
from(
SELECT a.*, LAG(l_batch_date) OVER (PARTITION BY id ORDER BY l_batch_date ASC) AS lag_start_dt,
lead(l_batch_date) OVER (PARTITION BY id ORDER BY l_batch_date ASC) AS lead_end_dt
FROM ods.t20240312_ht a
) b;






**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

**深知大多数大数据工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**

**因此收集整理了一份《2024年大数据全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**
![img](https://img-blog.csdnimg.cn/img_convert/7d492b7f2525ad8de828af2b217d9961.png)
![img](https://img-blog.csdnimg.cn/img_convert/00491f89777a538176d51755d55405b1.png)
![img](https://img-blog.csdnimg.cn/img_convert/c13e502428d9dad4758082d1563267da.png)
![img](https://img-blog.csdnimg.cn/img_convert/5dea4503bc39c708edf2f13530f3e7f0.png)
![img](https://img-blog.csdnimg.cn/img_convert/9f7bda5597c4a1983e67551d95c2495e.png)

**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上大数据开发知识点,真正体系化!**

**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新**

**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)**
![img](https://img-blog.csdnimg.cn/img_convert/9c7a2da52bcd00590743e76d21a3a292.png)

觉得这些内容对你有帮助,可以添加VX:vip204888 (备注大数据获取)**
[外链图片转存中...(img-kRT6qdHZ-1712846899009)]

  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值