sqoop數(shù)據(jù)導(dǎo)入導(dǎo)出應(yīng)用案例

sqoop數(shù)據(jù)導(dǎo)入導(dǎo)出應(yīng)用案例

1 sqoop導(dǎo)入數(shù)據(jù)

將RDBMS中的一個(gè)表數(shù)據(jù)導(dǎo)入到hdfs。表中的每一行被視為hdfs的記錄。所有記錄都存儲(chǔ)為文本文件的文本數(shù)據(jù)(或者Avro、sequence文件等二進(jìn)制數(shù)據(jù))。

1.1 語法

下面的命令用于將數(shù)據(jù)導(dǎo)入到hdfs上。

$sqoop import (generic-args) (import-args)

1.2 測(cè)試數(shù)據(jù)

在MySQL有一個(gè)userdb的數(shù)據(jù)庫(kù),其中有一張usertable表,該表結(jié)構(gòu)如下:

id name age
2 tom 15
3 toms 25
4 tomslee 17
5 bob 16

1.3 導(dǎo)入表中數(shù)據(jù)到HDFS

下面的命令用于從MySQL數(shù)據(jù)庫(kù)服務(wù)器中的usertable表導(dǎo)入數(shù)據(jù)到hdfs。

sqoop import \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--table usertable \
--m 1

上面的命令中--connect指的是連接地址,這里面是mysql服務(wù)器的地址;--table usertable是MySQL數(shù)據(jù)庫(kù)的數(shù)據(jù)表;--m 1是指定MapReduce的數(shù)量。

如果執(zhí)行成功,會(huì)顯示如下輸出:

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)會(huì)分配一個(gè)默認(rèn)的地址,該地址根據(jù)當(dāng)前的用戶名和表名生成的。

為了驗(yàn)證在hdfs導(dǎo)入的數(shù)據(jù),使用下面的命令可以查看:

hadoop fs -cat /user/hadoop/userdb/part-m-00000

默認(rèn)情況下hdfs上面的數(shù)據(jù)字段之間用逗號(hào)(,)分割。

sqoop1

1.4 導(dǎo)入到hive表中

sqoop import \
--connect jdbc:mysql://mysqlhost:3306/userdb \
--username root \
--password root \
--table usertable \
--hive-import \
--m 1;

調(diào)用上述命令之前不用先建立hive數(shù)據(jù)表,由于沒有指定hive的數(shù)據(jù)庫(kù),所以系統(tǒng)會(huì)在hive的default數(shù)據(jù)庫(kù)下面建立一張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中的字段之間默認(rèn)是用'\001'進(jìn)行分割的,所以字段之間看起來緊挨著。

1.5 導(dǎo)入到hdfs指定的目錄上

在導(dǎo)入表數(shù)據(jù)到hdfs使用sqoop工具,我們可以指定目標(biāo)目錄。

以下是指定目標(biāo)目錄選項(xiàng)的sqoop導(dǎo)入命令到的語法。

--target-dir<new directory in HDFS>

下面的命令是用來導(dǎo)入MySQL數(shù)據(jù)庫(kù)的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的目錄不能存在,因?yàn)閟qoop會(huì)將這個(gè)目錄作為MapReduce的輸出目錄。
導(dǎo)入到hdfs上的輸出數(shù)據(jù)格式如下:

2,tom,15
3,toms,25
4,tomslee,17
5,bob,16

1.6 導(dǎo)入表數(shù)據(jù)子集

我們可以導(dǎo)入表的"where"子句的一個(gè)子集通過sqoop工具。它執(zhí)行在各自的數(shù)據(jù)庫(kù)服務(wù)器相應(yīng)的sql查詢中,并將結(jié)果儲(chǔ)存在hdfs的目標(biāo)目錄上。

where子句的語法如下:

--where <condition>

下面的命令用來 導(dǎo)入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的目錄不能存在,因?yàn)閟qoop會(huì)將這個(gè)目錄作為MapReduce的輸出目錄。

導(dǎo)入到hdfs上的輸出數(shù)據(jù)格式如下:

2,tom,15

1.7 增量導(dǎo)入數(shù)據(jù)

增量導(dǎo)入是僅導(dǎo)入新添加的表中的行的技術(shù)。

它需要添加'incremental','check-column','last-value'選項(xiàng)來執(zhí)行增量導(dǎo)入。

下面的語法用于sqoop導(dǎo)入命令 增量的選項(xiàng)。

--incremental <mode>
--check_column <column name>
--last-value <last check column value>

下面命令用于在user表執(zhí)行增量導(dǎo)入。

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ù)導(dǎo)出

將數(shù)據(jù)從hdfs導(dǎo)出到RDBMS數(shù)據(jù)庫(kù)。

導(dǎo)出前,目標(biāo)表必須存在于目標(biāo)數(shù)據(jù)庫(kù)中。

默認(rèn)操作是從將文件中的數(shù)據(jù)使用insert語句插入到mysql數(shù)據(jù)表中。
更新模式下,是生成update語句更新表數(shù)據(jù)。

2.1 語法

以下是export命令的語法。

sqoop export (generic-args) (export-args)

2.2 案例一

將hdfs中的數(shù)據(jù)導(dǎo)出到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中的默認(rèn)分隔符為'\001'。

驗(yàn)證:在MySQL中輸入select * from usertable;

3 sqoop數(shù)據(jù)導(dǎo)入導(dǎo)出命令詳解

3.1 sqoop import導(dǎo)入數(shù)據(jù)命令參數(shù)詳解

輸入sqoop import --help命令可以查看所有導(dǎo)入命令的參數(shù)詳解:


常用命令:
   --connect <jdbc-uri>                         JDBC連接字符串                                         
   --connection-manager <class-name>            連接管理者                                             
   --driver <class-name>                        驅(qū)動(dòng)類
   --hadoop-home <dir>                          指定$HADOOP_HOME路徑
   -P                                           從命令行輸入密碼(這樣可以保證數(shù)據(jù)庫(kù)密碼的安全性)
   --password <password>                        密碼
   --username <username>                        用戶名
   --verbose                                    打印信息


Import control arguments:
   --append                        添加到hdfs中已經(jīng)存在的dataset上
                                   直接使用該參數(shù)就可向一個(gè)已經(jīng)存在的目錄追加內(nèi)容了
   --as-avrodatafile               導(dǎo)入數(shù)據(jù)作為avrodata
   --as-sequencefile               導(dǎo)入數(shù)據(jù)作為SequenceFiles
   --as-textfile                   默認(rèn)導(dǎo)入數(shù)據(jù)為文本
   --boundary-query <statement>    Set boundary query for retrieving max
                                   and min value of the primary key
   --columns <col,col,col...>      選擇導(dǎo)入的列
   --compression-codec <codec>     壓縮方式,默認(rèn)是gzip
   --direct                        使用直接導(dǎo)入快速路徑
   --direct-split-size <n>         在快速模式下每n字節(jié)使用一個(gè)split
-e,--query <statement>             通過查詢語句導(dǎo)入
   --fetch-size <n>                一次讀入的數(shù)量                            
   --inline-lob-limit <n>          Set the maximum size for an inline LOB
-m,--num-mappers <n>               通過實(shí)行多少個(gè)map,默認(rèn)是4個(gè),某些數(shù)據(jù)庫(kù)8 or 16性能不錯(cuò)
   --split-by <column-name>        創(chuàng)建split的列,默認(rèn)是主鍵
   --table <table-name>            導(dǎo)入的數(shù)據(jù)表
   --target-dir <dir>              HDFS 目標(biāo)路徑
   --warehouse-dir <dir>           HDFS parent for table destination
   --where <where clause>          WHERE clause to use during import
-z,--compress                      Enable compression


增量導(dǎo)入?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>               設(shè)置字段結(jié)束符號(hào)
   --escaped-by <char>                用哪個(gè)字符來轉(zhuǎn)義
   --fields-terminated-by <char>      輸出字段之間的分隔符
   --lines-terminated-by <char>       輸出行分隔符
   --mysql-delimiters                 使用mysql的默認(rèn)分隔符: , 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表,如果目標(biāo)表存在則失敗

   --hive-delims-replacement <arg>             導(dǎo)入到hive時(shí)用自定義的字符替換掉 \n, \r, and \001

   --hive-drop-import-delims                   導(dǎo)入到hive時(shí)刪除 \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類型對(duì)應(yīng)到hive類型

3.2 sqoop export導(dǎo)出數(shù)據(jù)命令參數(shù)詳解

輸入sqoop export --help命令可以查看所有導(dǎo)入命令的參數(shù)詳解:

export主要參數(shù)
--direct     快速導(dǎo)入
--export-dir <dir>                            HDFS到處數(shù)據(jù)的目錄
-m,--num-mappers <n>                          都少個(gè)map線程
--table <table-name>                          導(dǎo)出哪個(gè)表
--call <stored-proc-name>                     存儲(chǔ)過程
--update-key <col-name>                       通過哪個(gè)字段來判斷更新
--update-mode <mode>                          插入模式,默認(rèn)是只更新,可以設(shè)置為allowinsert.
--input-null-string <null-string>             字符類型null處理
--input-null-non-string <null-string>         非字符類型null處理
--staging-table <staging-table-name>          臨時(shí)表
--clear-staging-table                         清空臨時(shí)表
--batch                                       批量模式

4 參考博文

http://www.cnblogs.com/cenyuhai/p/3306056.html

http://www.aboutyun.com/thread-9983-1-1.html

http://blog.csdn.net/wangyang1354/article/details/52936400

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容