文章目录
1、简介
check-column: 用来指定一些列,这些列在增量导入时用来检查这些数据是否作为增量数据进行导入,和关系型数据库中的自增字段及时间戳类似.
注意:这些被指定的列的类型不能使任意字符类型,如char、varchar等类型都是不可以的,同时–check-column可以去指定多个列
incremental:用来指定增量导入的模式,两种模式分别为Append和Lastmodified
last-value:指定上一次导入中检查列指定字段最大值
Error during import: --merge-key or --append is required when using --incremental lastmodified and the output directory exists.
说明:当–incremental lastmodified 并且输出的路径不为空,则需要加上 --append(追加) 或者 --merge-key(合并,当一个mapreduce任务运行完,还会启动另外一个mapreduce任务进行去重)
2、操作说明
在mysql上创建表
create table student
(
id int(10) not null AUTO_INCREMENT,
name char(5),
age int,
gender char(2),
clazz char(4),
primary key (id),
last_mod timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) DEFAULT CHARSET=utf8;
说明:我们要先把数据导入到student这张表中。
2.1指定字段的取值范围,增量导数据
2.1适用于表不断的有新数据插入(时间的改变)
2.3导入id大于等于last-value的数据
以下方式直接在linux中输入即可
sqoop import
–connect ‘jdbc:mysql://master:3306/student?useSSL=false’
–username root
–password 123456
–table student
–target-dir /sqoop/data/student2
–split-by id
–m 2
–fields-terminated-by ‘,’
–incremental append
–check-column id
–last-value 1500100996
说明:如果要再次执行这个任务,即在hdfs上的输出路径不为空,最后的结果是四个文件,因为是append
2.4导入–check-column指定的last_mod列
last_mod列的值大于等于–last-value “2021-12-06 16:46:00”
说明:如果加上–merge-key 会在导入之后在做去重
sqoop import
–connect ‘jdbc:mysql://master:3306/student?useSSL=false’
–username root
–driver com.mysql.jdbc.Driver
–password 123456
–table student
–target-dir /sqoop/data/student6
–split-by id
–m 2
–fields-terminated-by ‘,’
–incremental lastmodified
–check-column last_mod
–last-value “2021-12-06 20:53:00”
–merge-key
id
注意:我们可以通过修改表中的数据,修改上述时间,来做测试。
由于是进行merge操作,所以再次运行会进行两次MapReduce任务,最后变成一个hdfs文件