一啃奴、簡介
Sqoop是一個用來將Hadoop和關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)相互轉(zhuǎn)移的工具淆衷,可以將一個關(guān)系型數(shù)據(jù)庫(例如 : MySQL ,Oracle ,Postgres等)中的數(shù)據(jù)導(dǎo)進(jìn)到Hadoop的HDFS中调煎,也可以將HDFS的數(shù)據(jù)導(dǎo)進(jìn)到關(guān)系型數(shù)據(jù)庫中五慈。Sqoop專為大數(shù)據(jù)批量傳輸設(shè)計蔫磨,能夠分割數(shù)據(jù)集并創(chuàng)建Hadoop任務(wù)來處理每個區(qū)塊淘讥。
- 把MySQL、Oracle等數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)入到HDFS堤如、Hive蒲列、HBase中。
- 把HDFS搀罢、Hive蝗岖、HBase中的數(shù)據(jù)導(dǎo)出到MySQL、Oracle等數(shù)據(jù)庫中榔至。
- 1.4 為sqoop1, 1.9 為sqoop2 剪侮,sqoop1與sqoop2是不兼容的梅桩。
實(shí)現(xiàn)需要
數(shù)據(jù)庫:
- driver
- URL县钥、username、password
- database、table
hadoop:
- type (hdfp琉用、hive峭范、hbase)
- path 存儲到哪里墓造?
- 數(shù)據(jù)分隔符
- mappers 數(shù)量杏愤,也就是使用多少線程。
二驼仪、命令
查看 sqoop 支持的命令
- sqoop help
![sqoop 命令列表][1]
顯示所有庫名
sqoop list-databases \
--connect jdbc:mysql://192.168.66.4:3306 \
--username sendi \
--password 1234
![list-databases 例子][2]
顯示某個數(shù)據(jù)庫里所有表
sqoop list-tables \
--connect jdbc:mysql://192.168.66.4:3306/networkmanagement \
--username sendi \
--password 1234
![list-tables 例子][3]
MYSQL 導(dǎo)入數(shù)據(jù)到 HIVE
sqoop import
--connect jdbc:mysql://192.168.66.4:3306/networkmanagement \
--username sendi \
--password 1234 \
--table people
--hive-import
--create-hive-table
--fields-terminated-by "\t"
-m 5
hive 參數(shù)
- --hive-import 必須參數(shù)掸犬,指定導(dǎo)入hive
- --hive-database default hive庫名
- --hive-table people hive表名
- --fields-terminated-by hive的分隔符
- --hive-overwrite 重寫重復(fù)字段
- --create-hive-table 幫創(chuàng)建好 hive 表,但是表存在會出錯绪爸。不建議使用這個參數(shù)湾碎,因為到導(dǎo)入的時候,會與我們的字段類型有出入奠货。
- --hive-partition-key "dt" 指定分區(qū)表的字段
- --hive-partition-value "2018-08-08" 指定分區(qū)表的值
導(dǎo)出沒有主鍵的表
可以使用兩種方式:
- --split-by 指定切分的字段
- -m 1 : 設(shè)置只使用一個map進(jìn)行數(shù)據(jù)遷移
過濾條件
- --where "age>18" 匹配條件
- --columns "name,age" 選擇要導(dǎo)入的指定列
- --query ‘select * from people where age>18 and $CONDITIONS’: sql語句查詢的結(jié)果集
- 不能 --table 一起使用
- 需要指定 --target-dir 路徑
當(dāng)數(shù)據(jù)庫中字符為空時的處理
- --null-non-string '0' 當(dāng)不是字符串的數(shù)據(jù)為空的時候介褥,用 0 替換
- --null-string 'string' 當(dāng)字符串為空的時候,使用string 字符替換
提高傳輸速度
- --direct 提高數(shù)據(jù)庫到hadoop的傳輸速度
支持的數(shù)據(jù)庫類型與版本:
- myslq 5.0 以上
- oracle 10.2.0 以上
增量導(dǎo)入
增量導(dǎo)入對應(yīng)递惋,首先需要知監(jiān)控那一列柔滔,這列要從哪個值開始增量
* check-column id 用來指定一些列
* 這些被指定的列的類型不能使任意字符類型,如char萍虽、varchar等類型都是不可以的,常用的是指定主鍵id.
* –check-column 可以去指定多個列
- last-value 10 從哪個值開始增量
- incremental 增量的模式
- append id 是獲取大于某一列的某個值睛廊。
- lastmodified "2016-12-15 15:47:30" 獲取某個時間后修改的所有數(shù)據(jù)
- --append 附加模式
- --merge-key id 合并模式
注意:增量導(dǎo)入不能與 --delete-target-dir 一起使用,還有必須指定增量的模式