sqoop數(shù)據(jù)導入導出應用案例
1 sqoop導入數(shù)據(jù)
將RDBMS中的一個表數(shù)據(jù)導入到hdfs。表中的每一行被視為hdfs的記錄宦言。所有記錄都存儲為文本文件的文本數(shù)據(jù)(或者Avro换薄、sequence文件等二進制數(shù)據(jù))。
1.1 語法
下面的命令用于將數(shù)據(jù)導入到hdfs上跨算。
$sqoop import (generic-args) (import-args)
1.2 測試數(shù)據(jù)
在MySQL有一個userdb的數(shù)據(jù)庫,其中有一張usertable表椭懊,該表結構如下:
id | name | age |
---|---|---|
2 | tom | 15 |
3 | toms | 25 |
4 | tomslee | 17 |
5 | bob | 16 |
1.3 導入表中數(shù)據(jù)到HDFS
下面的命令用于從MySQL數(shù)據(jù)庫服務器中的usertable表導入數(shù)據(jù)到hdfs诸蚕。
sqoop import \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--table usertable \
--m 1
上面的命令中--connect
指的是連接地址,這里面是mysql服務器的地址;--table usertable
是MySQL數(shù)據(jù)庫的數(shù)據(jù)表背犯;--m 1
是指定MapReduce的數(shù)量坏瘩。
如果執(zhí)行成功,會顯示如下輸出:
14/12/22 15:24:54 INFO sqoop.Sqoop: Running Sqoop version: 1.4.5
14/12/22 15:24:56 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-hadoop/compile/cebe706d23ebb1fd99c1f063ad51ebd7/emp.jar
-----------------------------------------------------
O mapreduce.Job: map 0% reduce 0%
14/12/22 15:28:08 INFO mapreduce.Job: map 100% reduce 0%
14/12/22 15:28:16 INFO mapreduce.Job: Job job_1419242001831_0001 completed successfully
-----------------------------------------------------
-----------------------------------------------------
14/12/22 15:28:17 INFO mapreduce.ImportJobBase: Transferred 145 bytes in 177.5849 seconds (0.8165 bytes/sec)
14/12/22 15:28:17 INFO mapreduce.ImportJobBase: Retrieved 5 records.
上述的過程由于沒有指定hdfs的保存位置漠魏,所以系統(tǒng)會分配一個默認的地址桑腮,該地址根據(jù)當前的用戶名和表名生成的。
為了驗證在hdfs導入的數(shù)據(jù)蛉幸,使用下面的命令可以查看:
hadoop fs -cat /user/hadoop/userdb/part-m-00000
默認情況下hdfs上面的數(shù)據(jù)字段之間用逗號(,)分割破讨。
1.4 導入到hive表中
sqoop import \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--table usertable \
--hive-import \
--m 1;
調用上述命令之前不用先建立hive數(shù)據(jù)表,由于沒有指定hive的數(shù)據(jù)庫奕纫,所以系統(tǒng)會在hive的default數(shù)據(jù)庫下面建立一張usertable數(shù)據(jù)表提陶。
數(shù)據(jù)傳輸過程:
1 從MySQL到hdfs上(通過MapReduce)
2 從hdfs遷移到hive中
hive> select * from usertable;
OK
2 tom 15
3 toms 25
4 tomslee 17
5 bob 16
Time taken: 0.191 seconds, Fetched: 4 row(s)
hive> dfs -cat /user/hive/warehouse/usertable/part-m-00000;
2tom15
3toms25
4tomslee17
5bob16
hive>
通過查看hdfs上的數(shù)據(jù)可知hive中的字段之間默認是用'\001'
進行分割的,所以字段之間看起來緊挨著匹层。
1.5 導入到hdfs指定的目錄上
在導入表數(shù)據(jù)到hdfs使用sqoop工具隙笆,我們可以指定目標目錄。
以下是指定目標目錄選項的sqoop導入命令到的語法升筏。
--target-dir<new directory in HDFS>
下面的命令是用來導入MySQL數(shù)據(jù)庫的user數(shù)據(jù)表到hdfs的/user/test目錄撑柔。
sqoop import \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--target-dir /uer/test \
--table usertable \
--m 1;
注意指定的hdfs的目錄不能存在,因為sqoop會將這個目錄作為MapReduce的輸出目錄您访。
導入到hdfs上的輸出數(shù)據(jù)格式如下:
2,tom,15
3,toms,25
4,tomslee,17
5,bob,16
1.6 導入表數(shù)據(jù)子集
我們可以導入表的"where"子句的一個子集通過sqoop工具铅忿。它執(zhí)行在各自的數(shù)據(jù)庫服務器相應的sql查詢中,并將結果儲存在hdfs的目標目錄上灵汪。
where子句的語法如下:
--where <condition>
下面的命令用來 導入usertable表的數(shù)據(jù)子集檀训。子集查詢用戶的姓名和年齡。
sqoop import \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--table usertable \
--where "name='tom' and age=15" \
--target-dir /user/test \
--m 1;
注意指定的hdfs的目錄不能存在享言,因為sqoop會將這個目錄作為MapReduce的輸出目錄峻凫。
導入到hdfs上的輸出數(shù)據(jù)格式如下:
2,tom,15
1.7 增量導入數(shù)據(jù)
增量導入是僅導入新添加的表中的行的技術。
它需要添加'incremental','check-column','last-value'選項來執(zhí)行增量導入览露。
下面的語法用于sqoop導入命令 增量的選項荧琼。
--incremental <mode>
--check_column <column name>
--last-value <last check column value>
下面命令用于在user表執(zhí)行增量導入。
sqoop import \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--table usertable \
--incremental append \
--check-column id \
--last-value 2 \
--target-dir /user/test \
--m 1;
注意: 這里面指定的hdfs路徑不但可以存在而且在該目錄下還可以有文件存在差牛。
2 sqoop數(shù)據(jù)導出
將數(shù)據(jù)從hdfs導出到RDBMS數(shù)據(jù)庫命锄。
導出前达罗,目標表必須存在于目標數(shù)據(jù)庫中谴餐。
默認操作是從將文件中的數(shù)據(jù)使用insert語句插入到mysql數(shù)據(jù)表中。
更新模式下,是生成update語句更新表數(shù)據(jù)夹孔。
2.1 語法
以下是export命令的語法。
sqoop export (generic-args) (export-args)
2.2 案例一
將hdfs中的數(shù)據(jù)導出到MySQL的usertable表中。
sqoop export \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--table usertable \
-export-dir /user/hive/warehouse/usertable \
--input-fields-terminated-by '\001'
--m 1;
上述命令中的input-fields-terminated-by '\001'
指的是輸入的字段之間的分隔符搭伤,在hive中的默認分隔符為'\001'只怎。
驗證:在MySQL中輸入select * from usertable;
3 sqoop數(shù)據(jù)導入導出命令詳解
3.1 sqoop import導入數(shù)據(jù)命令參數(shù)詳解
輸入sqoop import --help
命令可以查看所有導入命令的參數(shù)詳解:
常用命令:
--connect <jdbc-uri> JDBC連接字符串
--connection-manager <class-name> 連接管理者
--driver <class-name> 驅動類
--hadoop-home <dir> 指定$HADOOP_HOME路徑
-P 從命令行輸入密碼(這樣可以保證數(shù)據(jù)庫密碼的安全性)
--password <password> 密碼
--username <username> 用戶名
--verbose 打印信息
Import control arguments:
--append 添加到hdfs中已經存在的dataset上
直接使用該參數(shù)就可向一個已經存在的目錄追加內容了
--as-avrodatafile 導入數(shù)據(jù)作為avrodata
--as-sequencefile 導入數(shù)據(jù)作為SequenceFiles
--as-textfile 默認導入數(shù)據(jù)為文本
--boundary-query <statement> Set boundary query for retrieving max
and min value of the primary key
--columns <col,col,col...> 選擇導入的列
--compression-codec <codec> 壓縮方式,默認是gzip
--direct 使用直接導入快速路徑
--direct-split-size <n> 在快速模式下每n字節(jié)使用一個split
-e,--query <statement> 通過查詢語句導入
--fetch-size <n> 一次讀入的數(shù)量
--inline-lob-limit <n> Set the maximum size for an inline LOB
-m,--num-mappers <n> 通過實行多少個map怜俐,默認是4個身堡,某些數(shù)據(jù)庫8 or 16性能不錯
--split-by <column-name> 創(chuàng)建split的列,默認是主鍵
--table <table-name> 導入的數(shù)據(jù)表
--target-dir <dir> HDFS 目標路徑
--warehouse-dir <dir> HDFS parent for table destination
--where <where clause> WHERE clause to use during import
-z,--compress Enable compression
增量導入?yún)?shù):
--check-column <column> Source column to check for incremental
change
--incremental <import-type> Define an incremental import of type
'append' or 'lastmodified'
--last-value <value> Last imported value in the incremental
check column
輸出行格式參數(shù):
--enclosed-by <char> 設置字段結束符號
--escaped-by <char> 用哪個字符來轉義
--fields-terminated-by <char> 輸出字段之間的分隔符
--lines-terminated-by <char> 輸出行分隔符
--mysql-delimiters 使用mysql的默認分隔符: , lines: \n escaped-by: \ optionally-enclosed-by: '
--optionally-enclosed-by <char> Sets a field enclosing character
輸入?yún)?shù)解析:
--input-enclosed-by <char> Sets a required field encloser
--input-escaped-by <char> Sets the input escape
character
--input-fields-terminated-by <char> 輸入字段之間的分隔符
--input-lines-terminated-by <char> 輸入行分隔符
char
--input-optionally-enclosed-by <char> Sets a field enclosing
character
Hive arguments:
--create-hive-table 創(chuàng)建hive表,如果目標表存在則失敗
--hive-delims-replacement <arg> 導入到hive時用自定義的字符替換掉 \n, \r, and \001
--hive-drop-import-delims 導入到hive時刪除 \n, \r, and \001
--hive-home <dir> 重寫$HIVE_HOME
--hive-import Import tables into Hive
(Uses Hive's default
delimiters if none are
set.)
--hive-overwrite Overwrite existing data in
the Hive table
--hive-partition-key <partition-key> hive分區(qū)的key
--hive-partition-value <partition-value> hive分區(qū)的值
--hive-table <table-name> Sets the table name to use
when importing to hive
--map-column-hive <arg> 類型匹配拍鲤,sql類型對應到hive類型
3.2 sqoop export導出數(shù)據(jù)命令參數(shù)詳解
輸入sqoop export --help
命令可以查看所有導入命令的參數(shù)詳解:
export主要參數(shù)
--direct 快速導入
--export-dir <dir> HDFS到處數(shù)據(jù)的目錄
-m,--num-mappers <n> 都少個map線程
--table <table-name> 導出哪個表
--call <stored-proc-name> 存儲過程
--update-key <col-name> 通過哪個字段來判斷更新
--update-mode <mode> 插入模式贴谎,默認是只更新,可以設置為allowinsert.
--input-null-string <null-string> 字符類型null處理
--input-null-non-string <null-string> 非字符類型null處理
--staging-table <staging-table-name> 臨時表
--clear-staging-table 清空臨時表
--batch 批量模式
4 參考博文
http://www.cnblogs.com/cenyuhai/p/3306056.html