sqoop增量导入问题

一. sqoop支持的两种增量导入
1.根据自增的id号
2.根据updateTime
这样增量更新会遇到一个数据重复的问题,比如我们每天增量更新昨天的数据(可以用cron或者oozie)都会把updateTime在昨天范围内的数据增量导入到hive仓库里面,如果我们源数据库中昨天之前的数据被更改,那么updateTime会被修改为昨天的时间,因为这条数据在之前就已经导入过hive了,但是被修改之后又会被导入hive,这样就会导致hive中数据会重复。

二. 解决增量导入由于数据修改导致数据重复问题
我们可以创建两个hive表,一个为test_temp stored as textfile(hive中的临时表),一个为test stored as orcfile(hive中最终需要的表,试用orcfile格式,因为它的性能高)。
1.创建一个文件

1,lijie,chongqing
2,zhangshan,sz
3,lisi,shanghai
4,wangwu,usa

2.创建表(友情提示:不要用逗号作为分隔符,最好使用‘/001’作为分隔符,如果源数据库中存储的数据中带有逗号那么回造成数据总条数能对应,但是数据列对应不上)

create table test_temp (
    id string,
    name string,
    addr string
) comment '临时表'
partitioned by (y string,m string,d string)
row format delimited fields terminated by ','
stored as textfile

create table test (
    id string,
    name string,
    addr string
) comment '最终表'
row format delimited fields terminated by ','
stored as orcfile

3.load数据到临时表

load data local inpath '/home/hadoop/a.txt' overwrite into table test_temp partition (y='2016',m='10',d='09')

4.把中间表的数据insert到最终表

insert into table test select id,name,addr from test_temp where y='2016' and m='10' and d='09'

5.初始化完成模拟修改的数据

--修改文件
1,lijie,chengdu
2,zhangshan,huoxing
4,wangwu,lalalala
5,xinzeng,hehe
--写入临时表 (相当于从源数据库增量导入的数据1,2,4被修改过)
load data local inpath '/home/hadoop/a.txt' overwrite into table test_temp partition (y='2016',m='10',d='09')

6.合并(必须要有唯一主键)

--把非更新的数据和非增量的数据overwrite into 到test表中
insert overwrite table test
select a.id,a.name,a.addr from test a left outer join (
select * from test_temp where y='2016' and m='10' and d='09'
) b on (a.id = b.id) where b.id is null

--执行之后 查询test表中的数据 结果如下只有一条
OK
3   lisi    shanghai
Time taken: 0.069 seconds, Fetched: 1 row(s)


--把中间表的数据 into 到test表中
insert into table test select id,name,addr from test_temp where y='2016' and m='10' and d='09';

原理图
7.增量更新以及修改更新同步完成结果如下

hive> select * from test;
OK
3   lisi    shanghai
1   lijie   chengdu
2   zhangshan   huoxing
4   wangwu  lalalala
5   xinzeng hehe
Time taken: 0.061 seconds, Fetched: 5 row(s)
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值