Updated on 2022-12-09 GMT+08:00

Using Sqoop from Scratch

Sqoop is an open-source tool for transferring data between Hadoop (Hive) and traditional databases (such as MySQL and PostgreSQL). It can transfer data from a relational database (such as MySQL, Oracle, and PostgreSQL) to HDFS of Hadoop and the other way around.

Prerequisites

  • You have selected the Sqoop component when creating a cluster of MRS 3.1.0 or later.
  • You have installed the client. For details, see Installing a Client (MRS 3.x or Later). For example, the installation directory of the client is /opt/client. The client directory in the following operations is an example. Change it to the actual installation directory.
  • The corresponding driver package (for example, MySQL driver package mysql-connector-java-5.1.47.jar) exists in the client directory /Sqoop/sqoop/lib. If it does not exist, download it by referring to 3 in Adapting Sqoop 1.4.7 to MRS 3.x Clusters.

Exporting Data From HDFS to MySQL Using the sqoop export Command

  1. Log in to the node where the client is located.
  2. Run the following command to initialize environment variables:

    source /opt/client/bigdata_env

  3. Run the following command to operate the Sqoop client:

    sqoop export --connect jdbc:mysql://10.100.231.134:3306/test --username root --password xxxxxx --table component13 -export-dir hdfs://hacluster/user/hive/warehouse/component_test3 --fields-terminated-by ',' -m 1

    Table 1 Parameter description

    Parameter

    Description

    -direct

    Imports data to a relational database using a database import tool, for example, mysqlimport of MySQL, more efficient than the JDBC connection mode.

    -export-dir <dir>

    Specifies the source directory for storing data in the HDFS.

    -m or -num-mappers <n>

    Starts n (4 by default) maps to import data concurrently. The value cannot be greater than the maximum number of maps in a cluster.

    -table <table-name>

    Specifies the relational database table to be imported.

    -update-key <col-name>

    Specifies the column used for updating the existing data in a relational database.

    -update-mode <mode>

    Specifies how updates are performed. The value can be updateonly or allowinsert. This parameter is used only when the relational data table does not contain the data record to be imported. For example, if the HDFS data to be imported to the destination table contains a data record id=1 and the table contains an existing data record id=2, the update will fail.

    -input-null-string <null-string>

    This parameter is optional. If it is not specified, null will be used.

    -input-null-non-string <null-string>

    This parameter is optional. If it is not specified, null will be used.

    -staging-table <staging-table-name>

    Creates a table with the same data structure as the destination table for storing data before it is imported to the destination table.

    This parameter ensures the transaction security when data is imported to a relational database table. Due to multiple transactions during an import, this parameter can prevent other transactions from being affected when one transaction fails. For example, the imported data is incorrect or duplicate records exist.

    -clear-staging-table

    Clears data in the staging table before data is imported if the staging-table is not empty.

Importing Data from MySQL to Hive Using the sqoop import Command

  1. Log in to the node where the client is located.
  2. Run the following command to initialize environment variables:

    source /opt/client/bigdata_env

  3. Run the following command to operate the Sqoop client:

    sqoop import --connect jdbc:mysql://10.100.231.134:3306/test --username root --password xxxxxx --table component --hive-import --hive-table component_test2 --delete-target-dir --fields-terminated-by "," -m 1 --as-textfile

    Table 2 Parameter description

    Parameter

    Description

    -append

    Appends data to an existing dataset in the HDFS. Once this parameter is used, Sqoop imports data to a temporary directory, renames the temporary file where the data is stored, and moves the file to a formal directory to avoid duplicate file names in the directory.

    -as-avrodatafile

    Imports data to a data file in the Avro format.

    -as-sequencefile

    Imports data to a sequence file.

    -as-textfile

    Import data to a text file. After the text file is generated, you can run SQL statements in Hive to query the result.

    -boundary-query <statement>

    Specifies the SQL statement for performing boundary query. Before importing data, use a SQL statement to obtain a result set and import the data in the result set. The data format can be -boundary-query 'select id,creationdate from person where id = 3' (indicating a data record whose ID is 3) or select min(<split-by>), max(<split-by>) from <table name>.

    The fields to be queried cannot contain fields whose data type is string. Otherwise, the error message "java.sql.SQLException: Invalid value for getLong()" is displayed.

    -columns<col,col,col...>

    Specifies the fields to be imported. The format is -Column id,Username.

    -direct

    Imports data to a relational database using a database import tool, for example, mysqlimport of MySQL, more efficient than the JDBC connection mode.

    -direct-split-size

    Splits the imported streams by byte. Especially when data is imported from PostgreSQL using the direct mode, a file that reaches the specified size can be divided into several independent files.

    -inline-lob-limit

    Sets the maximum value of an inline LOB.

    -m or -num-mappers

    Starts n (4 by default) maps to import data concurrently. The value cannot be greater than the maximum number of maps in a cluster.

    -query, -e<statement>

    Imports data from the query result. To use this parameter, you must specify the -target-dir and -hive-table parameters and use the query statement containing the WHERE clause as well as $CONDITIONS.

    Example: -query'select * from person where $CONDITIONS' -target-dir /user/hive/warehouse/person -hive-table person

    -split-by<column-name>

    Specifies the column of a table used to split work units. Generally, the column name is followed by the primary key ID.

    -table <table-name>

    Specifies the relational database table from which data is obtained.

    -target-dir <dir>

    Specifies the HDFS path.

    -warehouse-dir <dir>

    Specifies the directory for storing data to be imported. This parameter is applicable when data is imported to HDFS but cannot be used when you import data to Hive directories. This parameter cannot be used together with -target-dir.

    -where

    Specifies the WHERE clause when data is imported from a relational database, for example, -where 'id = 2'.

    -z,-compress

    Compresses sequence, text, and Avro data files using the GZIP compression algorithm. Data is not compressed by default.

    –compression-codec

    Specifies the Hadoop compression codec. GZIP is used by default.

    –null-string <null-string>

    Specifies the string to be interpreted as NULL for string columns.

    –null-non-string<null-string>

    Specifies the string to be interpreted as null for non-string columns. If this parameter is not specified, NULL will be used.

    -check-column (col)

    Specifies the column for checking incremental data import, for example, id.

    -incremental (mode) append

    or last modified

    Incrementally imports data.

    append: appends records, for example, appending records that are greater than the value specified by last-value.

    lastmodified: appends data that is modified after the date specified by last-value.

    -last-value (value)

    Specifies the maximum value (greater than the specified value) of the column after the last import. This parameter can be set as required.

Sqoop Usage Example

  • Importing data from MySQL to HDFS using the sqoop import command

    sqoop import --connect jdbc:mysql://10.100.231.134:3306/test --username root --password xxx --query 'SELECT * FROM component where $CONDITIONS and component_id ="MRS 1.0_002"' --target-dir /tmp/component_test --delete-target-dir --fields-terminated-by "," -m 1 --as-textfile

  • Exporting data from OBS to MySQL using the sqoop export command

    sqoop export --connect jdbc:mysql://10.100.231.134:3306/test --username root --password xxx --table component14 -export-dir obs://obs-file-bucket/xx/part-m-00000 --fields-terminated-by ',' -m 1

  • Importing data from MySQL to OBS using the sqoop import command

    sqoop import --connect jdbc:mysql://10.100.231.134:3306/test --username root --password xxx --table component --target-dir obs://obs-file-bucket/xx --delete-target-dir --fields-terminated-by "," -m 1 --as-textfile

  • Importing data from MySQL to OBS tables outside Hive

    sqoop import --connect jdbc:mysql://10.100.231.134:3306/test --username root --password xxx --table component --hive-import --hive-table component_test01 --fields-terminated-by "," -m 1 --as-textfile