执行如下指令先将咱们以前的数据导入mysql
sqoop import \ --connect jdbc:mysql://master:3306/test \ --username hive \ --password 123456 \ --table customer \ -m 1
使用hdfs dfs -cat查看生成的数据文件,发现数据已经导入.而后咱们在mysql的customer中插入2条数据sql
insert into customer values(6,'james'); insert into customer values(7,'luna');
执行以下的指令,实现增量的导入数据库
sqoop import \ --connect jdbc:mysql://master:3306/test \ --username hive \ --password 123456 \ --table customer \ --check-column id \ --incremental append \ --last-value 5
在数据库的表字段中经常会设置一个自增的字段来做为数据表的主键,咱们这里以id字段来做为判断数据行是否为增量数据的依据.last-value设置上次导入的id的最大值,所以sqoop就只会将id值在5~7之间的数据进行导入,实现了增量的导入
注意:若是不指定last-value值,将会将表的全部数据进行导入,便发生了数据的冗余app
首先咱们要建立一个customer表,指定一个时间戳字段oop
create table customertest(id int,name varchar(20),last_mod timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
咱们再次插入以下记录:spa
insert into customertest(id,name) values(1,'neil'); insert into customertest(id,name) values(2,'jack'); insert into customertest(id,name) values(3,'martin'); insert into customertest(id,name) values(4,'tony'); insert into customertest(id,name) values(5,'eric');
此处的时间戳设置为在数据的产生和更新时都会发生改变.
咱们此时执行sqoop指令将数据导入hdfs,code
sqoop import \ --connect jdbc:mysql://master:3306/test \ --username hive \ --password 123456 \ --table customertest -m 1
咱们再次插入一条数据进入customertest表图片
insert into customertest(id,name) values(6,'james')
咱们使用incremental的方式进行增量的导入rem
sqoop import \ --connect jdbc:mysql://master:3306/test \ --username hive \ --password 123456 \ --table customertest \ --check-column last_mod \ --incremental lastmodified \ --last-value "2016-12-15 15:47:29" \ -m 1 \ --append
此处已经会导入咱们最后插入的一条记录,可是咱们却发现此处插入了2条数据,这是为何呢?
这是由于采用lastmodified模式去处理增量时,会将大于等于last-value值的数据当作增量插入.
注意:
使用lastmodified模式进行增量处理要指定增量数据是以append模式(附加)仍是merge-key(合并)模式添加
咱们演示使用merge-by的模式进行增量更新,咱们去update id为1的name字段table
update customertest set name = 'Neil' where id = 1;
更新以后,这条数据的时间戳会更新为咱们更新数据时的系统时间,咱们执行以下指令,把id字段做为merge-key
sqoop import \ --connect jdbc:mysql://master:3306/test \ --username hive \ --password 123456 \ --table customertest \ --check-column last_mod \ --incremental lastmodified \ --last-value "2016-12-15 15:47:30" \ -m 1 \ --merge-key id
因为merge-key这种模式是进行了一次完整的maoreduce操做,所以最终咱们在customertest文件夹下能够看到生成的为part-r-00000这样的文件,会发现id=1的name已经获得修改,同时新增了id=6的数据