使用 sqoop从MySQL增量导出数据到hive


使用 sqoop从MySQL增量导出数据到hive
本文主要解释incremental参数下append模式和lastmodified模式的区别,想要了解具体其他参数请参考官方文档:
http://sqoop.apache.org/docs/1.4.6/SqoopUserGuide.html#_importing_data_into_hive

- 首先测试append模式
/usr/bin/sqoop import --connect 'jdbc:mysql://172.17.1.103:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull' --username root --password 123456 -table incr_test -hive-database test -hive-table test_incr_test_20171130 -hive-import -m -1 --incremental append --check-column time_update --last-value "2017-12-01"
1
MySQL原始数据如下图所示:
使用 sqoop从MySQL增量导出数据到hive


执行上述命令后,查看hive数据如下:
使用 sqoop从MySQL增量导出数据到hive

只把‘2017-12-01’之后的数据导过来了,然后在MySQL添加数据之后再次导入。

MySQL数据如图:

使用 sqoop从MySQL增量导出数据到hive
再次执行上述命令结果如图:

使用 sqoop从MySQL增量导出数据到hive


把id为1的数据时间改为2017-12-01,模拟数据被更新,之后再导入hive(注:2017-11-01那条记录是之前就导入进来的)

append模式


结果表明append模式每次导入数据都会把last-value之后的数据全部导过来,不管是否超过当前时间。   
1
-再来测试一下lastmodified模式
/usr/bin/sqoop import --connect 'jdbc:mysql://172.17.1.103:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull' --username root --password 123456 -table incr_test -hive-database test -hive-table test_incr_test_20171130 -hive-import -m -1 --incremental lastmodified --check-column time_update --last-value "2017-12-01" 
1
原始MySQL数据如下:

使用 sqoop从MySQL增量导出数据到hive
执行命令后查看hive数据如下:

使用 sqoop从MySQL增量导出数据到hive
修改原始MySQL数据:

使用 sqoop从MySQL增量导出数据到hive
再次执行命令:

使用 sqoop从MySQL增量导出数据到hive
把id为1的数据时间改为2017-12-01,模拟数据被更新,之后再导入hive注:2017-11-01那条记录是之前就导入进来的)
lastmodified模式

使用 sqoop从MySQL增量导出数据到hive


测试结果表明lastmodified模式只会导入当前时间之前,大于当前时间不会导入,其他的与append模式无差异。
从sqoop执行日志里也可以看出来
lastmodified模式

使用 sqoop从MySQL增量导出数据到hive

append模式

使用 sqoop从MySQL增量导出数据到hive
做这个测试的原因就是因为看不太懂官方网站对这两个模式的解释,但是从测试结果来看好像区别不大,如果有错误还请小伙伴指出来,共同学习。
 

上一篇:sqoop的安装与配置


下一篇:大数据同步工具sqoop的上手操作