Sqoop增量导入实战

来源:互联网 发布:超人工程造价软件 编辑:程序博客网 时间:2024/05/16 05:26

在实际的生产环境下,我们常常是要继续数据增量的导入

核心参数

  • –check-column
    用来指定一些列,这些列在增量导入时用来检查这些数据是否作为增量数据进行导入,和关系型数据库中的自增字段及时间戳类似.
    注意:这些被指定的列的类型不能使任意字符类型,如char、varchar等类型都是不可以的,同时–check-column可以去指定多个列
  • –incremental
    用来指定增量导入的模式,两种模式分别为Append和Lastmodified
  • –last-value
    指定上一次导入中检查列指定字段最大值

Append模式实战增量导入

执行以下指令先将我们之前的数据导入

sqoop import \--connect jdbc:mysql://master:3306/test \--username hive \--password 123456 \--table customer \-m 1

使用hdfs dfs -cat查看生成的数据文件,发现数据已经导入.然后我们在mysql的customer中插入2条数据

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值,将会将表的所有数据进行导入,便发生了数据的冗余

Lastmodified导入实战

首先我们要创建一个customer表,指定一个时间戳字段

create table customertest(id int,name varchar(20),last_mod timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);

我们再次插入如下记录:

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,

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的方式进行增量的导入

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字段

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的数据

0 0
原创粉丝点击