SqoopのCentOS 7でのインストール使用

11291 ワード

背景
sqoopはmysql、hdfs、hive、hbaseなどのビッグデータコンポーネント間でデータを移行できます.
インストール
1、sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tarsをCentOS 7にアップロード
2、解凍、名前変更
[root@localhost szc]# tar -zxvf sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz

[root@localhost szc]# mv sqoop-1.4.6.bin__hadoop-2.0.4-alpha sqoop-1.4.6

3、sqoop-1.4に入る.6ディレクトリ、プロファイルの変更
[root@localhost szc]# cd sqoop-1.4.6/

[root@localhost sqoop-1.4.6]# mv conf/sqoop-env-template.sh conf/sqoop-env.sh

[root@localhost sqoop-1.4.6]# vim conf/sqoop-env.sh

いくつかの環境変数を追加するには、自分がインストールしたコンポーネントを追加するだけです.
#Set path to where bin/hadoop is available

export HADOOP_COMMON_HOME=/home/szc/cdh/hadoop-2.5.0-cdh5.3.6

#Set path to where hadoop-*-core.jar is available

export HADOOP_MAPRED_HOME=/home/szc/cdh/hadoop-2.5.0-cdh5.3.6


#set the path to where bin/hbase is available

#export HBASE_HOME=


#Set the path to where bin/hive is available

export HIVE_HOME=/home/szc/apache-hive-2.3.7


#Set the path for where zookeper config dir is

export ZOOCFGDIR=/home/szc/zookeeper/zookeeper-3.4.9/conf

4、mysqlドライバをlibディレクトリにアップロードする
5、sqoopを検証する
[root@localhost sqoop-1.4.6]# bin/sqoop help
Warning: /home/szc/sqoop-1.4.6/bin/../../hbase does not exist! HBase imports will fail.
Please set $HBASE_HOME to the root of your HBase installation.
Warning: /home/szc/sqoop-1.4.6/bin/../../hcatalog does not exist! HCatalog jobs will fail.
Please set $HCAT_HOME to the root of your HCatalog installation.
Warning: /home/szc/sqoop-1.4.6/bin/../../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
20/05/07 07:39:50 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6

usage: sqoop COMMAND [ARGS]

Available commands:
  codegen            Generate code to interact with database records
  create-hive-table  Import a table definition into Hive
  eval               Evaluate a SQL statement and display the results
  export             Export an HDFS directory to a database table
  help               List available commands
  import             Import a table from a database to HDFS
  import-all-tables  Import tables from a database to HDFS
  import-mainframe   Import datasets from a mainframe server to HDFS
  job                Work with saved jobs
  list-databases     List available databases on a server
  list-tables        List available tables in a database
  merge              Merge results of incremental imports
  metastore          Run a standalone Sqoop metastore
  version            Display version information

See 'sqoop help COMMAND' for information on a specific command.

mysqlにどのデータベースがあるかをクエリー
[root@localhost sqoop-1.4.6]# bin/sqoop list-database --connect jdbc:mysql://192.168.0.102 --username root --password root

Warning: /home/szc/sqoop-1.4.6/bin/../../hbase does not exist! HBase imports will fail.
Please set $HBASE_HOME to the root of your HBase installation.
Warning: /home/szc/sqoop-1.4.6/bin/../../hcatalog does not exist! HCatalog jobs will fail.
Please set $HCAT_HOME to the root of your HCatalog installation.
Warning: /home/szc/sqoop-1.4.6/bin/../../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
No such sqoop tool: list-database. See 'sqoop help'.

[root@localhost sqoop-1.4.6]# bin/sqoop list-databases --connect jdbc:mysql://192.168.0.102 --username root --password root

Warning: /home/szc/sqoop-1.4.6/bin/../../hbase does not exist! HBase imports will fail.
Please set $HBASE_HOME to the root of your HBase installation.
Warning: /home/szc/sqoop-1.4.6/bin/../../hcatalog does not exist! HCatalog jobs will fail.
Please set $HCAT_HOME to the root of your HCatalog installation.
Warning: /home/szc/sqoop-1.4.6/bin/../../accumulo does not exist! Accumulo imports will fail.
Please set $ACCUMULO_HOME to the root of your Accumulo installation.
20/05/07 07:45:11 INFO sqoop.Sqoop: Running Sqoop version: 1.4.6
20/05/07 07:45:11 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P instead.
20/05/07 07:45:11 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset.
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

information_schema
azkaban
ke
knowlegegraph
mysql
oozie
performance_schema
sys
test

使用例
mysqlからテーブルのすべてのデータをhdfsにインポート
[root@localhost sqoop-1.4.6]# bin/sqoop import --connect jdbc:mysql://192.168.0.102:3306/test --username root --password root --table users --target-dir /user/root/sqoop/users --delete-target-dir --num-mappers 1 --fields-terminated-by "\t"

importは、インポートデータを表し、データベースのurl、ユーザー名パスワード、インポートされたテーブル、インポートされた宛先パス(hdfs)を指定し、宛先パスが存在する場合に削除(--delete-target-dir)、使用するmapper数、フィールド区切り記号を指定します.
完了したら、ファイルの内容を次のように表示できます.
[root@localhost sqoop-1.4.6]# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -cat /user/root/sqoop/users/part-m-00000

20/05/07 07:58:43 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

1    songzeceng    szc    [email protected]
2    zeceng    szc    [email protected]
3    szc    sda    fd

mysqlからhdfsに一部のデータをインポートし、クエリーを使用してインポート
[root@localhost sqoop-1.4.6]# bin/sqoop import --connect jdbc:mysql://192.168.0.102:3306/test --username root --password root --target-dir /user/root/sqoop/users --delete-target-dir --num-mappers 1 --fields-terminated-by "\t" --query 'select * from users where id <=2 and $CONDITIONS;'

--queryはクエリー文を指定します.where句には元の条件のほかに、mapper間のwhere条件の伝達に$CONDITIONSを追加する必要があります.--queryパラメータ値が「囲まれている場合、$CONDITIONSの前に、すなわち--query」select*from users where id<=2 and$CONDITIONSを追加します.「--tableと--queryは同時に存在しない
結果の表示
[root@localhost sqoop-1.4.6]# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -cat /user/root/sqoop/users/part-m-00000

20/05/07 08:05:57 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

1    songzeceng    szc    [email protected]
2    zeceng    szc    [email protected]

mysqlからhdfsに一部のデータをインポートし、指定した列をインポート
--columnsを使用してインポートするカラムを指定できます.
[root@localhost sqoop-1.4.6]# bin/sqoop import --connect jdbc:mysql://192.168.0.102:3306/test --username root --password root --table users --target-dir /user/root/sqoop/users --delete-target-dir --num-mappers 1 --fields-terminated-by "\t" --columns username,email

結果の表示
[root@localhost sqoop-1.4.6]# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -cat /user/root/sqoop/users/part-m-00000

20/05/07 08:12:03 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

songzeceng    [email protected]
zeceng    [email protected]
szc    fd

mysqlからhdfsに一部のデータをインポートし、クエリー条件をインポート
--whereを使用して条件を指定
[root@localhost sqoop-1.4.6]# bin/sqoop import --connect jdbc:mysql://192.168.0.102:3306/test --username root --password root --table users --target-dir /user/root/sqoop/users --delete-target-dir --num-mappers 1 --fields-terminated-by "\t" --where "id=1"

結果の表示
[root@localhost sqoop-1.4.6]# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -cat /user/root/sqoop/users/part-m-00000

20/05/07 08:14:11 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

1    songzeceng    szc    [email protected]

--whereは--columnsと組み合わせて使用することもできます
[root@localhost sqoop-1.4.6]# bin/sqoop import --connect jdbc:mysql://192.168.0.102:3306/test --username root --password root --table users --target-dir /user/root/sqoop/users --delete-target-dir --num-mappers 1 --fields-terminated-by "\t" --where "id=1" --columns=username,email

結果の表示
[root@localhost sqoop-1.4.6]# /home/szc/cdh/hadoop-2.5.0-cdh5.3.6/bin/hadoop fs -cat /user/root/sqoop/users/part-m-00000

20/05/07 08:15:04 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

songzeceng    [email protected]

ただし--whereは--queryと共有できません
mysqlからhiveにデータをインポート
[root@localhost sqoop-1.4.6]# bin/sqoop import --connect jdbc:mysql://192.168.0.102:3306/test --username root --password root --table users --hive-import --hive-overwrite --hive-table users --num-mappers 1 --fields-terminated-by "\t"

--hive-importはhiveにインポートすることを示し、--hive-overwriteはテーブルの上書きを示し、--hive-tableはインポートしたhiveテーブルを示し、このテーブルが存在しない場合は作成されます
結果の表示
hive> select * from users;
OK

1    songzeceng    szc    [email protected]
2    zeceng    szc    [email protected]
3    szc    sda    fd

Time taken: 0.741 seconds, Fetched: 3 row(s)

hdfshiveからmysqlへのデータのエクスポート
[root@localhost sqoop-1.4.6]# bin/sqoop export --connect jdbc:mysql://192.168.0.102:3306/test --username root --password root --table users_sqoop --num-mappers 1 --export-dir /user/hive/warehouse/users --input-fields-terminated-by "\t"

exportはこれがエクスポートコマンドであることを示し、--export-dirはエクスポートするhdfsディレクトリを示し、--input-fields-terminated-byはファイル内の1行のデータの区切り記号を示す
mysqlのテーブルはデフォルトで作成されず、プライマリ・キーが重複しないことを保証するには、次のように結果を表示します.
mysql> select * from users_sqoop;

+----+------------+----------+------------+
| id | name       | password | email      |
+----+------------+----------+------------+
|  1 | songzeceng | szc      | [email protected] |
|  2 | zeceng     | szc      | [email protected] |
|  3 | szc        | sda      | fd         |
+----+------------+----------+------------+

3 rows in set

sqoop実行スクリプト
jobディレクトリを作成し、sqoop_を記述します.test.optファイル
[root@localhost sqoop-1.4.6]# mkdir job

[root@localhost sqoop-1.4.6]# vim job/sqoop_test.opt

パラメータとパラメータの間を改行で区切る
export
--connect
jdbc:mysql://192.168.0.102:3306/test
--username
root
--password
root
--table
users_sqoop
--num-mappers
1
--export-dir
/user/hive/warehouse/users
--input-fields-terminated-by
"\t"

スクリプトを実行します.--options-fileスクリプトファイルを指定します.
[root@localhost sqoop-1.4.6]# bin/sqoop --options-file job/sqoop_test.opt

mysqlの結果を表示(users_sqoopテーブルを事前に空にしておく)
スクリプトの実行前
mysql> select * from users_sqoop;
Empty set

スクリプト実行後
mysql> select * from users_sqoop;

+----+------------+----------+------------+
| id | name       | password | email      |
+----+------------+----------+------------+
|  1 | songzeceng | szc      | [email protected] |
|  2 | zeceng     | szc      | [email protected] |
|  3 | szc        | sda      | fd         |
+----+------------+----------+------------+

3 rows in set

締めくくり
以上、HBAseは試していませんが、公式ドキュメントを自分で確認することができます.