Sqoop是一款Apache社區的開源軟體,支援在Hadoop生態軟體和結構化資料集(例如資料庫)之間進行高效的批量資料轉送。
背景資訊
將MySQL資料匯入HDFS
在Master節點上執行如下命令。
sqoop import --connect <dburi>/<dbname> --username <username> --password <password> --table <tablename> --target-dir <hdfs-dir> --split-by <split-column> --check-column <col> --incremental <mode> --last-value <value>參數 | 描述 |
dburi | 資料庫的訪問連結。例如jdbc:mysql://192.168.**.**:3306/。 |
dbname | 資料庫的名稱。 |
username | 資料庫登入使用者名稱。 |
password | 資料庫登入密碼。 |
tablename | MySQL表的名稱。 |
hdfs-dir | HDFS的寫入目錄。例如/user/hive/result。 |
split-column | 選擇性參數。指定一個用於切分任務的列,預設為表主鍵列。 |
col | 選擇性參數。增量匯入情境的檢查列。 |
mode | 選擇性參數。增量匯入模式,支援append和lastmodified兩種模式。 |
value | 選擇性參數。指定上次增量匯入的檢查列的最大值。 |
詳細的參數資訊請參見Sqoop Import。
將HDFS資料匯入MySQL
建立好對應HDFS中的資料結構的MySQL表後,在叢集的Master節點上執行如下命令。
sqoop export --connect <dburi>/<dbname> --username <username> --password <password> --table <tablename> --export-dir <hdfs-dir>參數 | 描述 |
dburi | 資料庫的訪問連結。例如jdbc:mysql://192.168.**.**:3306/。 |
dbname | 資料庫的名稱。 |
username | 資料庫登入使用者名稱。 |
password | 資料庫登入密碼。 |
tablename | MySQL表的名稱。 |
hdfs-dir | HDFS的寫入目錄。例如/user/hive/result。 |
詳細的參數資訊請參見Sqoop Import。
將MySQL資料匯入Hive
在叢集的Master節點上執行如下命令。
sqoop import --connect <dburi>/<dbname> --username <username> --password <password> --table <tablename> --check-column <col> --incremental <mode> --last-value <value> --fields-terminated-by "\t" --lines-terminated-by "\n" --hive-import --target-dir <hdfs-dir> --hive-table <hive-tablename>參數 | 描述 |
dburi | 資料庫的訪問連結。例如jdbc:mysql://192.168.**.**:3306/。 |
dbname | 資料庫的名稱。 |
username | 資料庫登入使用者名稱。 |
password | 資料庫登入密碼。 |
tablename | MySQL表的名稱。 |
col | 選擇性參數。增量匯入情境的檢查列。 |
mode | 選擇性參數。增量匯入模式,支援append和lastmodified兩種模式。 |
value | 選擇性參數。指定上次增量匯入的檢查列的最大值。 |
hdfs-dir | HDFS的寫入目錄。例如/user/hive/result。 |
hive-tablename | Hive中的表名。 |
詳細的參數資訊請參見Sqoop Import。
將Hive資料匯入MySQL
執行命令與匯入HDFS資料至MySQL一致,但需要指定Hive表對應的HDFS路徑。詳情請參見將HDFS資料匯入MySQL。
將MySQL資料匯入OSS
在叢集的Master節點上執行如下命令。
sqoop import --connect <dburi>/<dbname> --username <username> --password <password> --table <tablename> --target-dir <oss-dir> --temporary-rootdir <oss-tmpdir> --check-column <col> --incremental <mode> --last-value <value>參數 | 描述 |
dburi | 資料庫的訪問連結。例如jdbc:mysql://192.168.**.**:3306/。 |
dbname | 資料庫的名稱。 |
username | 資料庫登入使用者名稱。 |
password | 資料庫登入密碼。 |
tablename | MySQL表的名稱。 |
oss-dir | OSS的寫入目錄。例如oss://<AccessKeyId>:<AccessKeySecret>@<BucketName>.oss-cn-hangzhou-internal.aliyuncs.com/result。 |
oss-tmpdir | 臨時寫入目錄。指定mode為append模式時,需要指定該參數。 採用append模式後,Sqoop會先將資料匯入臨時目錄,然後將檔案重新命名為正常目標目錄。如果目標目錄已經存在於HDFS中,則Sqoop拒絕匯入並覆蓋該目錄的內容。 |
col | 選擇性參數。增量匯入情境的檢查列。 |
mode | 選擇性參數。增量匯入模式,支援append和lastmodified兩種模式。 |
value | 選擇性參數。指定上次增量匯入的檢查列的最大值。 |
詳細的參數資訊請參見Sqoop Import。
將OSS資料匯入MySQL
建立好對應OSS中資料結構的MySQL表後,在叢集的Master節點上執行如下命令。
sqoop export --connect <dburi>/<dbname> --username <username> --password <password> --table <tablename> --export-dir <oss-dir>參數 | 描述 |
dburi | 資料庫的訪問連結。例如jdbc:mysql://192.168.**.**:3306/。 |
dbname | 資料庫的名稱。 |
username | 資料庫登入使用者名稱。 |
password | 資料庫登入密碼。 |
tablename | MySQL表的名稱。 |
oss-dir | OSS的寫入目錄。例如oss://<AccessKeyId>:<AccessKeySecret>@<BucketName>.oss-cn-hangzhou-internal.aliyuncs.com/result。 |
詳細的參數資訊請參見Sqoop Import。
使用SQL作為匯入條件
命令和參數如下所示。
sqoop import --connect <dburi>/<dbname> --username <username> --password <password> --query <query-sql> --split-by <sp-column> --hive-import --hive-table <hive-tablename> --target-dir <hdfs-dir>參數 | 描述 |
dburi | 資料庫的訪問連結。例如jdbc:mysql://192.168.**.**:3306/。 |
dbname | 資料庫的名稱。 |
username | 資料庫登入使用者名稱。 |
password | 資料庫登入密碼。 |
query-sql | 使用的查詢語句。例如 |
sp-column | 進行切分的條件。通常跟MySQL表的主鍵有關。 |
hdfs-dir | HDFS的寫入目錄。例如/user/hive/result。 |
hive-tablename | Hive中的表名。 |
詳細的參數資訊請參見Sqoop Import。