1. 程式人生 > >[總結]----Sqoop 幾種匯入匯出模式

[總結]----Sqoop 幾種匯入匯出模式

測試連線 bin/sqoop list-databases \ --connect jdbc:mysql://com.james:3306 \ --username root \ --password-file file:///home/taojiamin/data/passwd.pwd 密碼檔案 本地檔案: file:///home/user/password hdfs檔案: /user/user/password 注意: 密碼檔案直接vim編輯會報錯, 應採用重定向追加或覆蓋寫入操作 echo -n "password" >> /home/taojiamin/data/passwd.pwd && chmod 400 /home/taojiamin/data/passwd.pwd
echo -n 不換行輸出; bin/sqoop import \ --connect jdbc:mysql://apache.bigdata.com:3306/testdb \ --username root \ -P \ --table user \ --append \ //將mysql表的資料追加到HDFS上已存在的資料集 --target-dir /input/sqoop/import \ -m 1 \ --fields-terminated-by "," bin/sqoop import \ --connect jdbc:mysql://com.hadoop05:3306/testdb \ --username root \ --password-file file:///home/hadoop/mypasswd \ --table user \ --target-dir /sqoop/input \ -m 1 \ --fields-terminated-by '\t' \ --delete-target-dir 全量匯入
例如: bin/sqoop import \ (輸入命令) --connect jdbc:mysql://bigdata.ibeifeng.com:3306/testdb \ (指定連線jdbc埠和資料庫名稱) --username root \ (資料庫使用者名稱) --password root123 \ (密碼 若不適用明文指定資料庫密碼 則可以用-P) --table user \ (指定資料庫中的一張表) --target-dir /input/import \ (指定資料匯入到HDFS上的目錄) --delete-target-dir \ //如果目標目錄已存在,則先刪除 --num-mappers 1 \ (指定使用匯入資料的map個數,mapreduce(V1)中的方式可以用-m 1 代替(過時))
--fields-terminated-by "," (目標檔案的分隔符, 預設情況下,匯入HDFS的每行資料分隔符是逗號) 部分欄位匯入: bin/sqoop import \ --connect jdbc:mysql://com.apache.bigdata:3306/sqoop \ --username root \ -P \ --table user \ --columns "id,account" \ --target-dir /sqoop/query1 \ -m 1 \ --delete-target-dir \ --fields-terminated-by "\t" 查詢匯入://待複習 query,where子句必須有$CONDITONS(固定寫法) 不能使用 --table bin/sqoop import \ --connect jdbc:mysql://bigdata.ibeifeng.com:3306/testdb \ --username root \ -P \ --query 'select id,account from user where account="fff" and $CONDITIONS' \ --target-dir /input/query \ -m 1 \ --delete-target-dir \ --fields-terminated-by "\t" 增量匯入3個引數 注意: 1.--append and --delete-target-dir can not be used together. 2.--check-column 不是使用CHAR/NCHAR/VARCHAR/VARNCHAR/ LONGVARCHAR/LONGNVARCHAR這樣的資料型別 後面跟 唯一 不重複的列 類似主鍵 3.--incremental 支援兩種模式 append 告訴sqoop是整型資料自增長的方式來區分從哪裡開始增量匯入 lastmodified 告訴sqoop是最後一次修改檔案的時間戳來區分從哪裡開始增量匯入 bin/sqoop import \ --connect jdbc:mysql://com.apache.bigdata:3306/sqoop \ --username root \ -P \ --table user \ --num-mappers 1 \ --target-dir /sqoop/incremental \ --fields-terminated-by "|" \ --check-column id \ 選擇ID 作為主鍵 --incremental append \ 選擇ID來區分從哪裡開始增量匯入 --last-value 3 選擇從id為3之後的行開始匯入資料 通過【--options-file】指定檔案,執行程式 可以將Sqoop的命令選項寫在檔案,通過【--options-file】指定檔案,進行執行程式。 vim sqoop_script export --connect jdbc:mysql://bigdata.ibeifeng.com:3306/testdb --username root --password root123 --table hive2mysql --num-mappers 1 --export-dir /user/hive/warehouse/db01.db/dept --fields-terminated-by "\t" $ bin/sqoop --options-file ~/sqoop_script 列出mysql資料庫中的所有資料庫 sqoop list-databases –connect jdbc:mysql://localhost:3306/ –username root –password 123456 連線mysql並列出test資料庫中的表 sqoop list-tables –connect jdbc:mysql://localhost:3306/test –username root –password 123456 命令中的test為mysql資料庫中的test資料庫名稱 username password分別為mysql資料庫的使用者密碼 將關係型資料的表結構複製到hive中,只是複製表的結構,表中的內容沒有複製過去。 sqoop create-hive-table –connect jdbc:mysql://localhost:3306/test –table sqoop_test –username root –password 123456 –hive-table test 其中 –table sqoop_test為mysql中的資料庫test中的表 –hive-table test 為hive中新建的表名稱