溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Hadoop學習之第八章節(jié):Sqoop安裝配置

發(fā)布時間:2020-06-01 04:51:16 來源:網(wǎng)絡 閱讀:1725 作者:iqdutao 欄目:數(shù)據(jù)庫



一、Sqoop介紹

 

  Sqoop是一個用來將Hadoop(Hive、HBase)和關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)相互轉(zhuǎn)移的工具,可以將一個關(guān)系型數(shù)據(jù)庫(例如:MySQL ,Oracle ,Postgres等)中的數(shù)據(jù)導入到Hadoop的HDFS中,也可以將HDFS的數(shù)據(jù)導入到關(guān)系型數(shù)據(jù)庫中。


Sqoop目前已經(jīng)是Apache的頂級項目了,目前版本是1.4.4 和 Sqoop2 1.99.3,本文以1.4.4的版本為例講解基本的安裝配置和簡單應用的演示。


版本為:

sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz


  1. 環(huán)境變量配置


 Hadoop學習之第八章節(jié):Sqoop安裝配置


2.Sqoop參數(shù)配置


# Set Hadoop-specific environment variables here.
#Set path to where bin/hadoop is available
#export HADOOP_COMMON_HOME=
#Set path to where hadoop-*-core.jar is available
#export HADOOP_MAPRED_HOME=
#set the path to where bin/hbase is available
#export HBASE_HOME=
#Set the path to where bin/hive is available
#export HIVE_HOME=


3.驅(qū)動jar包


下面測試演示以MySQL為例,則需要把mysql對應的驅(qū)動lib文件copy到<SQOOP_HOME>/lib 目錄下。


4.Mysql中測試數(shù)據(jù)


CREATE TABLE `demo_blog` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `blog` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;



CREATE TABLE `demo_log` (
  `operator` varchar(16) NOT NULL,
  `log` varchar(100) NOT NULL
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;



插入測試數(shù)據(jù):



insert into demo_blog (id, blog) values (1, "micmiu.com");
insert into demo_blog (id, blog) values (2, "ctosun.com");
insert into demo_blog (id, blog) values (3, "baby.micmiu.com");
insert into demo_log (operator, log) values ("micmiu", "create");
insert into demo_log (operator, log) values ("micmiu", "update");
insert into demo_log (operator, log) values ("michael", "edit");
insert into demo_log (operator, log) values ("michael", "delete");



二 .Sqoop命令操作


1.Sqoop基本命令


(1)列出Mysql中的數(shù)據(jù)庫


sqoop list-databases   --connect  jdbc:mysql://Master-Hadoop:3306  --username  root   --password rootroot




 Hadoop學習之第八章節(jié):Sqoop安裝配置



(2)列出test數(shù)據(jù)庫中所有的表


 
 
 sqoop list-databases   --connect  jdbc:mysql://Master-Hadoop:3306  --username  root   --password rootroot



 Hadoop學習之第八章節(jié):Sqoop安裝配置



(3)從Mysql導入HDFS文件中


sqoop import --connect  jdbc:mysql://Master-Hadoop:3306/test --username root --password rootroot --table demo_log  --split-by operator  --target-dir /usr/sqoop/other



顯示錯誤:找不到表中的類  Class demo_log not found

 Hadoop學習之第八章節(jié):Sqoop安裝配置


1.將/tmp/sqoop-root/compile/………下的編譯文件復制到  /usr/local/sqoop/lib/下


2.需要以下注釋配置

bin/configure-sqoop文件中把以下內(nèi)容注釋掉就可以了

## Moved to be a runtime check in sqoop.
#if [ ! -d "${HBASE_HOME}" ]; then
#  echo "Warning: $HBASE_HOME does not exist! HBase imports will fail."
#  echo 'Please set $HBASE_HOME to the root of your HBase installation.'
#fi
## Moved to be a runtime check in sqoop.
#if [ ! -d "${HCAT_HOME}" ]; then
# echo "Warning: $HCAT_HOME does not exist! HCatalog jobs will fail."
#  echo 'Please set $HCAT_HOME to the root of your HCatalog installation.'
#fi





(4)從HDFS中導入到Mysql中


sqoop export   --connect jdbc:mysql://Master-Hadoop:3306/test --username root  --password rootroot --table   dutao --export-dir /usr/other/part-2 --input-fields-terminated-by '\t'   -m 1



(5)從mysql中導入到Hive中

sqoop import --connect jdbc:mysql://Master-Hadoop:3306/test --username root --password rootroot --table demo_log --warehouse-dir  /user/sqoop/demo_log   --hive-import --create-hive-table  -m 1



直接導入hive表 


     sqoop import --connect jdbc:postgresql://ip/db_name--username user_name  --table table_name  --hive-import -m 5 


    內(nèi)部執(zhí)行實際分三部,1.將數(shù)據(jù)導入hdfs(可在hdfs上找到相應目錄),2.創(chuàng)建hive表名相同的表,3,將hdfs上數(shù)據(jù)傳入hive表中 


sqoop根據(jù)postgresql表創(chuàng)建hive表 

    sqoop create-hive-table --connect jdbc:postgresql://ip/db_name --username user_name  --table table_name  --hive-table hive_table_name( --hive-partition-key partition_name若需要分區(qū)則加入分區(qū)名稱)

 

導入hive已經(jīng)創(chuàng)建好的表中 
    sqoop import --connect jdbc:postgresql://ip/db_name --username user_name  --table table_name  --hive-import -m 5 --hive-table hive_table_name  (--hive-partition-key partition_name --hive-partition-value partititon_value); 

  

使用query導入hive表 

    sqoop import --connect jdbc:postgresql://ip/db_name --username user_name  --query "select ,* from retail_tb_order where \$CONDITIONS"  --hive-import -m 5 --hive-table hive_table_name  (--hive-partition-key partition_name --hive-partition-value partititon_value); 
    注意:$CONDITIONS條件必須有,query子句若用雙引號,則$CONDITIONS需要使用\轉(zhuǎn)義,若使用單引號,則不需要轉(zhuǎn)義。 





(6)將hive中的表數(shù)據(jù)導入到mysql數(shù)據(jù)庫表中


sqoop export --connect jdbc:mysql://Master-Hadoop:3306/test --username root --password rootroot --table demo_log --export-dir /user/hive/warehouse/demo_log/000000_1 --input-fields-terminated-by '\0001'


在進行導入之前,mysql中的表 demo_log 必須已經(jīng)提起創(chuàng)建好了。




  1.將數(shù)據(jù)從關(guān)系數(shù)據(jù)庫導入文件到hive表中,--query 語句使用

 
 
 sqoop import --append --connect jdbc:mysql://192.168.20.118:3306/test --username dyh --password 000000 --query "select id,age,name from userinfos where \$CONDITIONS"  -m 1  --target-dir /user/hive/warehouse/userinfos2 --fields-terminated-by ",";


  2.將數(shù)據(jù)從關(guān)系數(shù)據(jù)庫導入文件到hive表中,--columns  --where 語句使用

 
 
 sqoop import --append --connect jdbc:mysql://192.168.20.118:3306/test --username dyh --password 000000 --table userinfos --columns "id,age,name"  --where "id > 3 and (age = 88 or age = 80)"  -m 1  --target-dir /user/hive/warehouse/userinfos2 --fields-terminated-by ",";


注意:--target-dir /user/hive/warehouse/userinfos2   可以用  --hive-import --hive-table   userinfos2 進行替換




(6)Hdfs與Hive之間數(shù)據(jù)


1.從Hive導出到本地系統(tǒng)


hive> insert overwrite local directory '/home/wyp/wyp'
    > select * from wyp;


2.從本地系統(tǒng)到Hive中


  •   先在Hive里面創(chuàng)建好表,

hive> create table wyp
    > (id int, name string,
    > age int, tel string)
    > ROW FORMAT DELIMITED
    > FIELDS TERMINATED BY '\t'
    > STORED AS TEXTFILE;
OK
Time taken: 2.832 seconds
  •   本地文件系統(tǒng)里面有個/home/wyp/wyp.txt文件

[wyp@master ~]$ cat wyp.txt
1       wyp     25      13188888888888
2       test    30      13888888888888
3       zs      34      899314121


  • wyp.txt文件中的數(shù)據(jù)列之間是使用\t分割的,可以通過下面的語句將這個文件里面的數(shù)據(jù)導入到wyp表里面,操作如下:

hive> load data local inpath 'wyp.txt' into table wyp;
Copying data from file:/home/wyp/wyp.txt
Copying file: file:/home/wyp/wyp.txt
Loading data to table default.wyp
Table default.wyp stats:
[num_partitions: 0, num_files: 1, num_rows: 0, total_size: 67]
OK
Time taken: 5.967 seconds




2.從Hive導出到HDFS


hive> insert overwrite directory '/home/wyp/hdfs'
    > select * from wyp;



向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI