天天看點

sqoop 完成與關系型資料庫的互導

sqoop 完成與關系型資料庫的互導

一.安裝SQOOP後可使用如下指令列出mysql資料庫中的所有資料庫,與檢驗是否安裝成功。

# sqoop list-databases --connect jdbc:mysql://localhost:3306/ --username root --password 123456

二. hive與mysql資料互導

一. 使用指令範例:

  1. sqoop import --connect jdbc:mysql://192.168.137.1:3306/test
  2. --username root --password 123456 --table mytabs --fields-terminated-by
  3. '\t' -m 1 [--hive-import] --append --check-column 'id' --incremental
  4. append --last-value 5 --where 'id>2 and id<5'

參數詳解:

1.--fields-terminated-by '\t',表示将資料導入到hadoop中列記錄之間的間隔符,預設符号為英文逗号。這裡通常使用制表符\t來間隔資料,避免資料再次從HDFS到入到關系資料庫時引起分割混亂

2.-m 1,是--num-mappers的縮寫,表示指定MapReduce的個數為1個(預設會自動開啟多個),sqoop轉化的MR程 序不包含reduce

3.--append,表示資料導入到hadoop的方式為追加,否則不允許重複導入

4.--check-column '主鍵列名' --incremental append --last-value 5,表示資料為增量導入,根據--last-value的值來判斷,有大于這個值的記錄則執行導入,否則不執行導入操作

5.表示資料為增量導入,根據--last-value的值來判斷, 有大于這個值的記錄則執行導入,否則不執行導入操作

6.--hive-import,表示将資料導入到Hive中;

7.--where '',資料篩選條件

8.-e 或--query 'select * from table where id>5 and $CONDITIONS',自定義導入資料的sql語句。使用自定義sql語句 需要注意:

① 使用了自定義sql就不能指定--table;

② 自定義sql語句的where條件中必須包含字元串"$CONDITIONS",$CONDITIONS是一個變量,用于給多個map任務劃分任務範 圍;

③ 使用自定義sql時,如果通過參數-m指定多個map任務,由于自定義sql中可能存在多表查詢,是以必須使用參數“--split-by 表名.字段名”指定多個map任務分割資料的根據字段,如--split-by users.id;

9. --target-dir,顯示指定資料導入到HDFS中的位置,預設儲存路徑為:/user/{目前使用者}/{表名}/表資料檔案,

如果導入時需要将已存在的HDFS檔案删除,可使用--delete-target-dir

二. hive資料導入導出資料到mysql

  1. sqoop export -connect jdbc:mysql://localhost:3306/sqoop
  2. -username root -password hadoop -table hive_student -export-dir
  3. /hive/student/student --input-fields-terminated-by '\t'

三.mysql資料庫資料導入hive中

  1. sqoop import --connect jdbc:mysql://localhost:3306/sqoop
  2. -username root -password hadoop -table test -hive-import -m 1

三.hbase與關系型資料庫資料互導

從Mysql導入到Hbase中

參數說明:

test 為mysql中要傳入到hbase表中的表名。

mysql_sqoop_test 傳入hbase中的表名

--column-family hbase表中的列族

--hbase-row-key 在hbase中那一列作為rowkey

使用範例:

  1. sqoop import --connect jdbc:mysql://10.120.10.11:3306/sqoop
  2. --username sqoop --password sqoop --table test --hbase-create-table
  3. --hbase-table mysql_sqoop_test --column-family info --hbase-row-key id -m 1