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';lastmofied通過外部表的方式導入數據
–check-column和–merge-key相同的時候,會替換掉已存在的數據。
DROP TABLE IF EXISTS sqoop_test; CREATE EXTERNAL TABLE `sqoop_test`( `id` bigint, `name` string, `last_mod` TIMESTAMP ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS TEXTFILE LOCATION 'hdfs://tqHadoopCluster/toto_2/hiveWarehouse/sqoop_test';sqoop import -m 1 \ --connect jdbc:mysql://ip:3306/test \ --username root \ --password xxxxxxxxx \ --table sqoop_test \ --target-dir /toto_2/hiveWarehouse/sqoop_test \ --columns "id,name,last_mod" \ --map-column-java "id=Long,name=String,last_mod=java.sql.Timestamp" \ --map-column-hive "id=BIGINT,name=STRING,last_mod=TIMESTAMP" \ --incremental lastmodified \ --check-column last_mod \ --merge-key id \ --last-value "2019-12-25 09:23:10" \ --null-string '\\N' \ --null-non-string '\\N' \ --hive-drop-import-delims \ --lines-terminated-by '\n' \ --input-fields-terminated-by ',' \ --fields-terminated-by ',';如果–merge-key id和–check-column last_mod字段都沒有變化,name變化之后,更新最新的數據,此種情況下會覆蓋掉歷史的該條記錄的數據。
注意:
1、數據庫中刪除記錄之后,不會同步的刪除外部表中的此條記錄。
2、外部表的時候,注意指定分割符為’,’ ,否則導入的數據將會是帶有NULL值的
3、外部表的時候,也要指定列和列的類型。
總結
以上是生活随笔為你收集整理的Ambari2.7.4+HDP3.1.4下sqoop增量导入只支持append模式,mysql直接进入hive的lastmodified的不支持。下面是增量的命令。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sqoop(四)增量导入、全量导入、减量
- 下一篇: 微粒贷额度怎么提升 完善个人信息就有希望