這里假定已經(jīng)準(zhǔn)備好了現(xiàn)成的Hadoop,Hive,Hbase,Zookeeper和一個(gè)postgresql數(shù)據(jù)庫。
下載安裝
從 http://mirror.bit.edu.cn/apache/sqoop/ 地址下載 sqoop 安裝包,這里我使用的是1.4.7版本。
wget -c http://mirror.bit.edu.cn/apache/sqoop/1.4.7/sqoop-1.4.7.bin__hadoop-2.6.0.tar.gz
解壓壓縮包到一個(gè)目錄下
cd /apps
tar zxvf sqoop-1.4.7.bin__hadoop-2.6.0.tar.gz
配置
進(jìn)入 sqoop 的 conf 目錄,復(fù)制 sqoop-env-template.sh 為 sqoop-env.sh
cd /apps/sqoop-1.4.7.bin__hadoop-2.6.0/conf
cp sqoop-env-template.sh sqoop-env.sh
編輯 sqoop-env.sh 文件,其中路徑根據(jù)具體位置填寫
#Set path to where bin/hadoop is available
export HADOOP_COMMON_HOME=/apps/hadoop-2.7.7
#Set path to where hadoop-*-core.jar is available
export HADOOP_MAPRED_HOME=/apps/hadoop-2.7.7
#set the path to where bin/hbase is available
#export HBASE_HOME=/apps/hbase-2.0.4
#Set the path to where bin/hive is available
export HIVE_HOME=/apps/apache-hive-2.3.2-bin
#Set the path for where zookeper config dir is
#export ZOOCFGDIR=/apps/zookeeper-3.4.10/conf
驗(yàn)證安裝配置
$ bin/sqoop-version
...
19/01/08 14:57:19 INFO sqoop.Sqoop: Running Sqoop version: 1.4.7
Sqoop 1.4.7
git commit id 2328971411f57f0cb683dfb79d19d4d19d185dd8
Compiled by maugli on Thu Dec 21 15:59:58 STD 2017
使用
連接 postgresql 數(shù)據(jù)庫
首先需要準(zhǔn)備 postgresql 的 jdbc 驅(qū)動(dòng)包,并放入 sqoop 的根目錄下。
準(zhǔn)備數(shù)據(jù)庫和表
test=> create table users
(
id serial primary key ,
name varchar(128),
password varchar(128)
);
test=> insert into users values(1,'user1','password1');
test=> insert into users values(2,'user2','password2');
test=> insert into users values(3,'user3','password3');
test=> select * from users;
id | name | password
----+-------+-----------
1 | user1 | password1
2 | user2 | password2
3 | user3 | password3
(3 rows)
查看數(shù)據(jù)庫
bin/sqoop list-databases --connect jdbc:postgresql://localhost:5432 --username test --password test
...
postgres
hive
test
查看數(shù)據(jù)庫中表
bin/sqoop list-tables --connect jdbc:postgresql://localhost:5432/test --username test --password test
...
users
查看數(shù)據(jù)表中數(shù)據(jù)
bin/sqoop eval --connect jdbc:postgresql://localhost:5432/test --username test --password test -e 'select * from users'
-------------------------------------------------------------
| id | name | password |
-------------------------------------------------------------
| 1 | user1 | password1 |
| 2 | user2 | password2 |
| 3 | user3 | password3 |
-------------------------------------------------------------
從 postgresql 向 HDFS 導(dǎo)入數(shù)據(jù)
# 導(dǎo)入數(shù)據(jù)到默認(rèn)目錄
$ bin/sqoop import --connect jdbc:postgresql://localhost:5432/test --username test --password test --table users --m 1
# 查看hdfs文件系統(tǒng)
$ hdfs dfs -ls /user/kongxx/
drwxr-xr-x - kongxx supergroup 0 2019-01-09 00:06 /user/kongxx/users
# 查看hdfs文件內(nèi)容
$ hdfs dfs -cat /user/kongxx/users/*
1,user1,password1
2,user2,password2
3,user3,password3
# 導(dǎo)入數(shù)據(jù)到指定目錄
$ bin/sqoop import --connect jdbc:postgresql://localhost:5432/test --username test --password test --table users --target-dir /user/kongxx/users2 --m 1
# 查看hdfs文件系統(tǒng)
$ hdfs dfs -ls /user/kongxx/
drwxr-xr-x - kongxx supergroup 0 2019-01-09 00:06 /user/kongxx/users
drwxr-xr-x - kongxx supergroup 0 2019-01-09 00:21 /user/kongxx/users2
# 查看hdfs文件內(nèi)容
$ hdfs dfs -cat /user/kongxx/users2/*
1,user1,password1
2,user2,password2
3,user3,password3
# 導(dǎo)入使用查詢語句查詢的數(shù)據(jù)到指定目錄,并指定分隔符
$ bin/sqoop import --connect jdbc:postgresql://localhost:5432/test --username test --password test --query 'select * from users where $CONDITIONS and 1=1' --target-dir /user/kongxx/users3 --fields-terminated-by '\t' --m 1
# 查看hdfs文件內(nèi)容
$ hdfs dfs -cat /user/kongxx/users3/*
1 user1 password1
2 user2 password2
3 user3 password3
從 postgresql 向 Hive導(dǎo)入數(shù)據(jù)
在使用Hive前,需要在 sqoop 的根目錄下創(chuàng)建一個(gè) hive-exec.jar 的軟連接,如下:
ln -s /apps/apache-hive-2.3.2-bin/lib/hive-exec-2.3.2.jar
向 Hive 中導(dǎo)入數(shù)據(jù)
# 導(dǎo)入數(shù)據(jù)到 hive 中 (也可以指定 Hive 中的數(shù)據(jù)庫,表和使用增量導(dǎo)入方式)
$ bin/sqoop import --connect jdbc:postgresql://localhost:5432/test --username test --password test --table users --hive-import --hive-overwrite --lines-terminated-by "\n" --fields-terminated-by "\t" --m 1
# 查看數(shù)據(jù)文件
$ bin/hdfs dfs -cat /user/hive/warehouse/users/*
1 user1 password1
2 user2 password2
3 user3 password3
在 Hive 中查看數(shù)據(jù)
$ hive
hive> show tables;
OK
users
hive> select * from users;
OK
1 user1 password1
2 user2 password2
3 user3 password3