CentOS7でのSqoopのインストールと使用

バックグラウンド

sqoopを使用して、mysql、hdfs、hive、hbaseなどのビッグデータコンポーネント間でデータを移行できます。

インストール

1.sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tarsをCentOS7にアップロードします

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"

インポートとは、データをインポートし、データベースのURL、ユーザー名とパスワード、インポートされたテーブル、インポートの宛先パス(hdfs)を指定し、宛先パスが存在する場合は削除することを指定することを意味します(- -delete-target-dir)、およびマッパーが使用した数量、フィールドセパレータ

完了後、次のようにファイルの内容を表示できます

[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句の元の条件に加えて、マッパー間で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からハイブにデータをインポートします

[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-overwriteはテーブルの上書きを示し、-hive-tableはインポートされたハイブテーブルを示します。このテーブルが存在しない場合は作成されます

結果を見る

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)

hdfs \ hiveから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は、ファイル内のデータ行の区切り文字を示します。

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実行スクリプト

ジョブディレクトリを作成し、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は試していません。公式ドキュメントは自分で確認できます。

おすすめ

転載: blog.csdn.net/qq_37475168/article/details/107306286