Updated on 2024-11-29 GMT+08:00

Interconnecting Sqoop with External Storage Systems

Before you start operations in this section, you have interconnected the HDFS client with OBS by referring to Configuring a Storage-Compute Decoupled Cluster (Agency). You also need to download the MySQL driver package of the required version from the MySQL official website https://downloads.mysql.com/archives/c-j/, decompress the package, and upload it to Client installation directory/Sqoop/sqoop/lib directory on the node where the Sqoop client is installed.

Exporting Data from HDFS to MySQL

  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.xxx.xxx:3306/test --username root --password xxx --table component13 -export-dir hdfs://hacluster/user/hive/warehouse/component_test3 --fields-terminated-by ',' -m 1

    Table 1 Parameter description

    Parameter

    Description

    --connect

    Specifies the URL for connecting to JDBC. The value is in jdbc:mysql://IP address of the MySQL database:MySQL Port/Database name format.

    --username

    Specifies the username for connecting to the MySQL database.

    -password

    Specifies the password for connecting to the MySQL database. There can be security risks if a command contains the authentication password. You are advised to disable the command recording function (history) before running the command.

    -table <table-name>

    Specifies the name of the MySQL table used to store exported data.

    -export-dir <dir>

    Specifies the HDFS path of the Sqoop table to be exported.

    --fields-terminated-by

    Specifies the delimiter of the exported data, which must be the same as that in the HDFS data table to be exported.

    -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.

    -direct

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

    -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

  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.xxx.xxx:3306/test --username root --password xxx --table component --hive-import --hive-table component_test2 --delete-target-dir --fields-terminated-by "," -m 1 --as-textfile

    Table 2 Parameter description

    Parameter

    Description

    --hive-import

    Imports data from a relational database to MRS Hive.

    --delete-target-dir

    Deletes the existing target file (if any) from Hive and imports again.

    -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

MySQL Driver Package Is Missing During Data Import or Export

If the error "Could not load db driver class: com.mysql.jdbc.Driver" is reported when you run the sqoop import or sqoop export command, the MySQL driver package is missing. Download the MySQL driver package from the MySQL official website, decompress it, upload it to the Client installation directory/Sqoop/sqoop/lib, and run the command again.

Figure 1 An error indicating that the MySQL driver package is missing