一.參數說明
1.import/export通用
選項 含義說明
--connect <jdbc-uri> 指定JDBC連接字符串
--connection-manager <class-name> 指定要使用的連接管理器類
--driver <class-name> 指定要使用的JDBC驅動類
--hadoop-mapred-home <dir> 指定$HADOOP_MAPRED_HOME路徑
--help 打印用法幫助信息
--password-file 設置用於存放認證的密碼信息文件的路徑
-P 從控制台讀取輸入的密碼
--password <password> 設置認證密碼
--username <username> 設置認證用戶名
--verbose 打印詳細的運行信息
--connection-param-file <filename> 可選,指定存儲數據庫連接參數的屬性文件
2.import
選項 含義說明
--append 將數據追加到HDFS上一個已存在的數據集上
--as-avrodatafile 將數據導入到Avro數據文件
--as-sequencefile 將數據導入到SequenceFile
--as-textfile 將數據導入到普通文本文件(默認)
--boundary-query <statement> 邊界查詢,用於創建分片(InputSplit)
--columns <col,col,col…> 從表中導出指定的一組列的數據
--delete-target-dir 如果指定目錄存在,則先刪除掉
--direct 使用直接導入模式(優化導入速度)
--direct-split-size <n> 分割輸入stream的字節大小(在直接導入模式下)
--fetch-size <n> 從數據庫中批量讀取記錄數
--inline-lob-limit <n> 設置內聯的LOB對象的大小
-m,--num-mappers <n> 使用n個map任務並行導入數據
-e,--query <statement> 導入的查詢語句
--split-by <column-name> 指定按照哪個列去分割數據
--table <table-name> 導入的源表表名
--target-dir <dir> 導入HDFS的目標路徑
--warehouse-dir <dir> HDFS存放表的根路徑
--where <where clause> 指定導出時所使用的查詢條件
-z,--compress 啟用壓縮
--compression-codec <c> 指定Hadoop的codec方式(默認gzip)
--null-string <null-string> 果指定列為字符串類型,使用指定字符串替換值為null的該類列的值
--null-non-string <null-string> 如果指定列為非字符串類型,使用指定字符串替換值為null的該類列的值
3.export
選項 含義說明
--validate <class-name> 啟用數據副本驗證功能,僅支持單表拷貝,可以指定驗證使用的實現類
--validation-threshold <class-name> 指定驗證門限所使用的類
--direct 使用直接導出模式(優化速度)
--export-dir <dir> 導出過程中HDFS源路徑
-m,--num-mappers <n> 使用n個map任務並行導出
--table <table-name> 導出的目的表名稱
--call <stored-proc-name> 導出數據調用的指定存儲過程名
--update-key <col-name> 更新參考的列名稱,多個列名使用逗號分隔
--update-mode <mode> 指定更新策略,包括:updateonly(默認)、allowinsert
--input-null-string <null-string> 使用指定字符串,替換字符串類型值為null的列
--input-null-non-string <null-string> 使用指定字符串,替換非字符串類型值為null的列
--staging-table <staging-table-name> 在數據導出到數據庫之前,數據臨時存放的表名稱
--clear-staging-table 清除工作區中臨時存放的數據
--batch 使用批量模式導出
二.導入import
參照上表,列舉常用的命令,使用 sql 語句查詢時,需要指定 $CONDITIONS
$ sqoop import --connect jdbc:mysql://192.168.56.121:3306/metastore --username hiveuser --password redhat --query 'SELECT * from TBLS where \$CONDITIONS ' --split-by tbl_id -m 4 --target-dir /user/hive/result
上面命令通過 -m 1 控制並發的 map 數。
使用 direct 模式:
$ sqoop import --connect jdbc:mysql://192.168.56.121:3306/metastore --username hiveuser --password redhat --table TBLS --delete-target-dir --direct --default-character-set UTF-8 --target-dir /user/hive/result
指定文件輸出格式:
$ sqoop import --connect jdbc:mysql://192.168.56.121:3306/metastore --username hiveuser --password redhat --table TBLS --fields-terminated-by "\t" --lines-terminated-by "\n" --delete-target-dir --target-dir /user/hive/result
指定空字符串:
$ sqoop import --connect jdbc:mysql://192.168.56.121:3306/metastore --username hiveuser --password redhat --table TBLS --fields-terminated-by "\t" --lines-terminated-by "\n" --delete-target-dir --null-string '\\N' --null-non-string '\\N' --target-dir /user/hive/result
如果需要指定壓縮:
$ sqoop import --connect jdbc:mysql://192.168.56.121:3306/metastore --username hiveuser --password redhat --table TBLS --fields-terminated-by "\t" --lines-terminated-by "\n" --delete-target-dir --null-string '\\N' --null-non-string '\\N' --compression-codec "com.hadoop.compression.lzo.LzopCodec" --target-dir /user/hive/result
三.導出export
在增量導出模式下,無論是allowinsert模式還是updateonly模式,都需要設置update-key:
1.allowinsert模式:該模式下生成的是insert語句,從這個角度講update-key是沒有作用的,但是在CDH Sandbox上測試時發現,如果不指定update-key則會導致reduce執行失敗。
2. updateonly模式:該模式下生成的是update語句,update-key中指定的字段用於構成update語句中的where字句,因此是必須的,同時也可以看出選擇的update-key字段必須是未被更新的字段,這樣才能確定一條記錄導出前后是否一致,而如果將update-key設置為被更新過的字段,則在目標表中通過where條件篩選不到匹配的記錄,從而造成數據無法被更新。此外,如果update-key中指定了所有字段,也會報錯。
參數介紹
--update-key 后面也可以接多個關鍵字列名,可以使用逗號隔開,Sqoop將會匹配多個關鍵字后再執行更新操作。
--export-dir 參數配合--table或者--call參數使用,指定了HDFS上需要將數據導入到MySQL中的文件集目錄。
--update-mode updateonly和allowinsert。 默認模式為updateonly,如果指定--update-mode模式為allowinsert,可以將目標數據庫中原來不存在的數據也導入到數據庫表中。即將存在的數據更新,不存在數據插入。
組合測試及說明
1、當指定update-key,且關系型數據庫表存在主鍵時:
A、allowinsert模式時,為更新目標數據庫表存的內容,並且原來不存在的數據也導入到數據庫表;
B、updateonly模式時,為更新目標數據庫表存的內容,並且原來不存在的數據也不導入到數據庫表;
2、當指定update-key,且關系型數據庫表不存在主鍵時:
A、allowinsert模式時,為全部數據追加導入到數據庫表;
B、updateonly模式時,為更新目標數據庫表存的內容,並且原來不存在的數據也不導入到數據庫表;
3、當不指定update-key,且關系型數據庫表存在主鍵時:
A、allowinsert模式時,報主鍵沖突,數據無變化;
B、updateonly模式時,報主鍵沖突,數據無變化;
4、當不指定update-key,且關系型數據庫表不存在主鍵時:
A、allowinsert模式時,為全部數據追加導入到數據庫表;
B、updateonly模式時,為全部數據追加導入到數據庫表;
所以再用 不同的 --update-mode 模式時,注意關系型數據庫的主鍵設置,否則會影響數據的導出效果。
四.常見錯誤:
1.使用 --update-mode allowinsert 導出時,報錯
Error during export:
Mixed update/insert is not supported against the target database yet
原因:指定的Jdbc 連接器不支持 allowinsert;
解決:刪除 --driver com.mysql.jdbc.Driver (如果出現新的報錯 刪除 --direct ) 問題可以解決;
2.sqoop導出hive數據到MySQL出現字符編碼問題解決
ERROR [Thread-11] org.apache.sqoop.mapreduce.AsyncSqlOutputFormat: Got exception in update thread: java.sql.SQLException: Incorrect string value: '\xF0 'itemName' at row 70;
java.sql.SQLException: Incorrect string value: '\xF0\xA1\xB7\x8A\xF0\xA1...' for column 'itemName' at row 70
原因:由於MySQL表編碼位數導致,解決
解決 : 1.修改表的編碼格式:
alter table 表名 convert to character set utf8mb4 collate utf8mb4_bin;
ALTER TABLE 表名 CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2.sqoop導命令指定編碼格式:
export --connect "jdbc:mysql://ip:port/庫名?useUnicode=true&characterEncoding=utf-8" --username 用戶民--password 密碼 --table 表名 --columns "字段1,字段2,..,字段n" --export-dir "hdfs:// 路徑" --fields-terminated-by '\t' --input-null-string '\\N' --input-null-non-string '\\N' -m 1 --driver com.mysql.jdbc.Driver;
重新執行導出命令,成功!(前提確保數據庫編碼是UTF8);
(遇到問題不斷更新。。。)
————————————————
版權聲明:本文為CSDN博主「D·Virus」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_35281775/article/details/92794295