1.增量导入
在实际工作当中,数据的导入,很多时候都是只需要导入增量数据即可,并不需要将表中的数据每次都全部导入到hive或者hdfs当 中去,这样会造成数据重复的问题。因此一般都是选用一些字段进行增量的导入, sqoop支持增量的导入数据。
增量导入是仅导入新添加的表中的行的技术。
--check-column (col)
用来指定一些列,这些列在增量导入时用来检查这些数据是否作为增量数据进行导入,和关系型数据库中的自增字段及时间戳类似。
注意:这些被指定的列的类型不能使任意字符类型,如char、varchar等类型都是不可以的,同时-- check-column可以去指定多个列。
--incremental (mode)
append:追加,比如对大于last-value指定的值之后的记录进行追加导入。lastmodified:最后的修改时间,追加last-value指定的日期之后的记录
--last-value (value)
指定自从上次导入后列的最大值(大于该指定的值),也可以自己设定某一值
1.1Append模式增量导入
- 执行以下指令先将我们之前的数据导入:
bin/sqoop import \
--connect jdbc:mysql://node01:3306/userdb \
--username root \
--password hadoop \
--target-dir /appendresult \
--table emp --m 1
- 然后在mysql的emp中插入2条增量数据:
insert into `userdb`.`emp` (`id`, `name`, `deg`, `salary`, `dept`) values ('1206', 'allen', 'admin', '30000', 'tp'); insert into `userdb`.`emp` (`id`, `name`, `deg`, `salary`, `dept`) values ('1207', 'woon', 'admin', '40000', 'tp'); |
- 执行如下的指令,实现增量的导入:
bin/sqoop import \
--connect jdbc:mysql://node-1:3306/userdb \
--username root --password hadoop \
--table emp --m 1 \
--target-dir /appendresult \
--incremental append \
--check-column id \
--last-value 1205
- 最后验证导入数据目录 可以发现多了一个文件 里面就是增量数据
1.2Lastmodified模式增量导入
- 首先创建一个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:
bin/sqoop import \
--connect jdbc:mysql://node-1:3306/userdb \
--username root \
--password hadoop \
--target-dir /lastmodifiedresult \
--table customertest --m 1
- 查看此时导出的结果数据:
- 再次插入一条数据进入customertest表
insert into customertest(id,name) values(6,'james') |
- 使用incremental的方式进行增量的导入:
bin/sqoop import \
--connect jdbc:mysql://node-1:3306/userdb \
--username root \
--password hadoop \
--table customertest \
--target-dir /lastmodifiedresult \
--check-column last_mod \
--incremental lastmodified \
--last-value "2019-05-28 18:42:06" \
--m 1 \
--append
此处已经会导入我们最后插入的一条记录,但是我们却发现此处插入了2条数据,这是为什么呢?
这是因为采用lastmodified模式去处理增量时,会将大于等于last-value值的数据当做增量插入。
1.3Lastmodified模式:append、merge-key
使用lastmodified模式进行增量处理要指定增量数据是以append模式(附加)还是merge-key(合并)模式添加
下面演示使用merge-by的模式进行增量更新,我们去更新 id为1的name字段。
update customertest set name = 'Neil' where id = 1; |
更新之后,这条数据的时间戳会更新为更新数据时的系统时间.
执行如下指令,把id字段作为merge-key:
bin/sqoop import \
--connect jdbc:mysql://node-1:3306/userdb \
--username root \
--password hadoop \
--table customertest \
--target-dir /lastmodifiedresult \
--check-column last_mod \
--incremental lastmodified \
--last-value "2019-05-28 18:42:06" \
--m 1 \
--merge-key id
由于merge-key模式是进行了一次完整的mapreduce操作,
因此最终我们在lastmodifiedresult文件夹下可以看到生成的为part-r-00000这样的文件,会发现id=1的name已经得到修改,同时新增了id=6的数据。
2.更新导出(updateonly模式)
2.1参数说明
-- update-key,更新标识,即根据某个字段进行更新,例如id,可以指定多个更新标识的字段,多个字段之间用逗号分隔。
-- updatemod,指定updateonly(默认模式),仅仅更新已存在的数据记录,不会插入新纪录。
2.2代码
bin/sqoop export \
--connect jdbc:mysql://node-1:3306/userdb \
--username root --password hadoop \
--table updateonly \
--export-dir /updateonly_2/ \
--update-key id \
--update-mode updateonly
日志显示:
3.更新导出(allowinsert模式)
3.1参数说明
-- update-key,更新标识,即根据某个字段进行更新,例如id,可以指定多个更新标识的字段,多个字段之间用逗号分隔。
-- updatemod,指定allowinsert,更新已存在的数据记录,同时插入新纪录。实质上是一个insert & update的操作。
3.2代码
bin/sqoop export \
--connect jdbc:mysql://node-1:3306/userdb \
--username root --password hadoop \
--table allowinsert \
--export-dir /allowinsert_2/ \
--update-key id \
--update-mode allowinsert
日志信息: