数据库
首页 > 数据库> > Ambari2.7.4+HDP3.1.4下sqoop增量导入只支持append模式,mysql直接进入hive的lastmodified的不支持。下面是增量的命令。

Ambari2.7.4+HDP3.1.4下sqoop增量导入只支持append模式,mysql直接进入hive的lastmodified的不支持。下面是增量的命令。

作者:互联网

1. 创建mysql表,并创建初始化数据

grant all privileges on *.* to 'root'@'%' identified by 'xxxxxxxxx' with grant option;
flush privileges;

use test;

drop table if exists sqoop_test;
create table sqoop_test (
    id bigint auto_increment primary key,
    name varchar(20),
    last_mod TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

select * from sqoop_test st;

insert into sqoop_test(name) values ('name1');
insert into sqoop_test(name) values ('name2');
insert into sqoop_test(name) values ('name3');

insert into sqoop_test(name) values ('name4');
insert into sqoop_test(name) values ('name5');
insert into sqoop_test(name) values ('name6');
insert into sqoop_test(name) values ('name7');

select * from sqoop_test st;

2. 全量导入表数据脚本

/usr/hdp/current/sqoop-client/bin/sqoop import -m 2 --connect jdbc:mysql://ip:3306/test \
--username root --password xxxxxxxxx \
--table sqoop_test \
--mapreduce-job-name 'testSqoop_mapreduce-job-name' \
--target-dir /tzqtoto_45/sqoop_test_tmp \
--delete-target-dir \
--hive-overwrite \
--hive-database tzqtoto_45 \
--hive-table sqoop_test \
--null-string '\\N' \
--null-non-string '\\N' \
--hive-drop-import-delims \
--lines-terminated-by '\n' \
--input-fields-terminated-by '\001' \
--fields-terminated-by '\001';

3. 以lastmodified和append的方式直接导入数据到hdfs

sqoop import \
--connect jdbc:mysql://ip:3306/test \
--username root \
--password xxxxxxxxx \
--table sqoop_test \
-m 1  \
--target-dir /tzqtoto_45/hiveWarehouse/sqoop_test \
--incremental lastmodified \
--check-column last_mod \
--merge-key id \
--last-value "2020-12-24 14:34:53" \
--null-string '\\N' \
--null-non-string '\\N' \
--hive-drop-import-delims \
--lines-terminated-by '\n' \
--input-fields-terminated-by ',' \
--fields-terminated-by ',';

sqoop import \
--connect jdbc:mysql://ip:3306/test \
--username root \
--password xxxxxxxxx \
--table sqoop_test \
-m 1  \
--target-dir /tzqtoto_45/hiveWarehouse/sqoop_test1 \
--incremental append \
--check-column id \
--last-value 0 \
--null-string '\\N' \
--null-non-string '\\N' \
--hive-drop-import-delims \
--lines-terminated-by '\n' \
--input-fields-terminated-by ',' \
--fields-terminated-by ',';

4. 以append导入数据

sqoop create-hive-table \
--connect jdbc:mysql://ip:3306/test \
--username root \
--password xxxxxxxxx \
--table sqoop_test \
--hive-database tzqtoto_45 \
--hive-table sqoop_test;


sqoop import -m 1 \
--connect jdbc:mysql://ip:3306/test \
--username root \
--password xxxxxxxxx \
--table sqoop_test \
--hive-import \
--hive-database tzqtoto_45 \
--hive-table sqoop_test \
--incremental append \
--check-column id \
--last-value 0;

5. append模式下check-column为时间字段的情况下(功能类似lastmodified)

sqoop create-hive-table \
--connect jdbc:mysql://ip:3306/test \
--username root \
--password xxxxxxxxx \
--table sqoop_test \
--hive-database tzqtoto_45 \
--hive-table sqoop_test;

# 下面的方式不管append方式如何,加了--hive-overwrite之后,历史数据都会被更新,即全量更新,并且会覆盖历史数据。
# 加了--hive-overwrite后,在mysql库中数据不变的情况下,多次执行时最后数据都不变。
# 未加--hive-overwrite时,在mysql库中数据不变的情况下,每次都会增加--last-value '2020-12-24 18:53:10'后面的数据。执行多遍的时候,数据会错误。
sqoop import -m 1 \
--connect jdbc:mysql://ip:3306/test \
--username root \
--password xxxxxxxxx \
--table sqoop_test \
--hive-import \
--hive-overwrite \
--hive-database tzqtoto_45 \
--hive-table sqoop_test \
--incremental append \
--check-column last_mod \
--merge-key id \
--last-value '2020-12-24 18:53:10';

## 此种,mysql中历史数据变化了之后(说的是删除情况下),不会删除hive中的数据,只会每次增加--last-value '2020-12-24 19:02:09';后的数据。
sqoop import -m 1 \
--connect jdbc:mysql://ip:3306/test \
--username root \
--password xxxxxxxxx \
--table sqoop_test \
--hive-import \
--hive-database tzqtoto_45 \
--hive-table sqoop_test \
--incremental append \
--check-column last_mod \
--merge-key id \
--last-value '2020-12-24 19:02:09';

标签:增量,Ambari2.7,sqoop,hive,--,mysql,test,table
来源: https://blog.csdn.net/toto1297488504/article/details/111648723