hive增量对比后将增量数据插入原表

 

 

 
 
 
工作中,有个业务,需要将这个表每个月增加和修改的记录不断追加到这个表内:
 
 
sql如下:
f_courtannoucement_party:  法院公告
 
fct_pdffilename: pdf文件名称
fct_name: 公告名称
 
insert overwrite table f_courtannoucement_party
select * from f_courtannoucement_party_add_modify  union all
select a.* from  f_courtannoucement_party left out join f_courtannoucement_party_add_modify b
on
1=1  and  a.fct_pdffilename=b.fct_pdffilename and  a.fct_name=b.fct_name
where 1=1  and  b.fct_pdffilename is null  and  b.fct_name is null;
 
这里以
fct_pdffilename: pdf文件名称
fct_name: 公告名称
作为核准原表和增量表(增加和修改的记录)中审核的标准,以下图为例 下图用 stu1原表  stu2增量表模拟上面业务:
 
 
 
 
 
 
 
 
 
以业务主键为关联条件,案例如下:
 
 
 
 
得到原表中没有 修改和新增的记录( 即没有变更的记录):
 
 
然后这个数据在和 增量表 stu2 union all 后在 insert overwrite  table1  得到需要的结果:
 
insert overwrite table stu1 select * from stu2 union all select a.* from stu1 left join stu2 b on 1=1 and a.id=b.id where b.id is null;
 
 
 
 
总结写法规律:
 
insert overwrite  table 原表
    select * from 增量表  union all
    select  a.* from 原表 a  left join 增量表 b on 1=1 and a.业务不会变化的字段=b.业务不会变化的字段 where b.业务不会变化的字段 is null;
 
 
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值