0
点赞
收藏
分享

微信扫一扫

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;

 

 

举报

相关推荐

0 条评论