首先感谢原文作者给我的启发,我加以修改。
- https://blog.csdn.net/magicharvey/article/details/20692829https://blog.csdn.net/CLKTOY/article/details/120306523
前言
我一直有个困惑,数仓日增量表,历史数据修改了除了拉链表还能怎么办?
因为我们同步过来的的增量数据不止有新增的数据还有修改的数据,这里我们不考虑拉链表,只存放当前最新的状态。
后来看了1和2,惭愧惭愧。
说实话我也看了阿里巴巴中台建设这本书,我也看到了p42页这一块full outer join 合并 + insert overwrite,但是我嫌复杂没仔细看,我觉得书在胡言乱语,不可能实现吧。
当我们增量同步进来的数据是和我们的历史数据进行合并的,这个时候我们就可以使用full join 。
我做了一定的修改,直接进入正题吧。
- 原有学生表student
create table student (
id string,
age string,
name string,
dt string
);
insert into table student values
("1","11","zhao","20140101")
,("2","22","qian","20140102")
,("3","33","sun","20140103")
,("4","44","li","20140104");
select * from student;
- 现有最新的学生表student_temp
数据较原有student的表,数据有新增也有修改
create table student_temp(
id string,
age string,
name string,
dt string
);
insert into table student_temp values
("1","11","zhao","20140101") -- 原本数据
,("2","999","test","20220323") -- 改动
,("3","999","test","20220323") -- 改动
,("4","44","li","20140104") -- 原本数据
,("5","55","wang","20140105") -- 新增数据
;
select * from student_temp;
- 查看full outer join效果
select * from student_temp a full outer join student b on a.id = b.id;
- 开始更新
没有则新增,有则覆盖
SELECt student_temp.id
,coalesce(student_temp.age,student.age) as age
,student_temp.name
,coalesce(student_temp.dt,student.dt) as dt
FROM student_temp
FULL OUTER JOIN student
ON student_temp.id = student.id
;



