
# 使用sqoop-shell工具导入数据到HDFS
#### 操作场景
本文将以从SFTP服务器导入数据到HDFS的作业为例，介绍如何分别在交互模式和批量模式下使用sqoop-shell工具进行创建和启动Loader作业。
#### 前提条件
已安装并配置Loader客户端，具体操作请参见[使用客户端运行Loader作业](https://support.huaweicloud.com/cmpntguide-lts-mrs/mrs_01_1156.html)。
#### 交互模式示例
1. 使用安装客户端的用户登录Loader客户端所在节点。
2. 执行以下命令，进入sqoop-shell工具的"conf"目录。例如，Loader客户端安装目录为"/opt/client/Loader/"。 
   **cd /opt/client/Loader/loader-tools-1.99.3/sqoop-shell/conf**
   
   
3. 执行以下命令，配置认证信息。 
   **vi client.properties**
   ```
   server.url=10.0.0.1:21351
   # simple or kerberos 
   authentication.type=simple 
   # true or false 
   use.keytab=true 
   authentication.user= 
   authentication.password= 
   client.principal=hdfs/hadoop@<系统域名> 
   # keytab file 
   client.keytab.file=./conf/login/hdfs.keytab
   ```
   ![](https://support.huaweicloud.com/cmpntguide-lts-mrs/public_sys-resources/note_3.0-zh-cn.png)
   登录FusionInsight Manager，选择"系统 \> 权限 \> 域和互信"，"本端域"参数即为当前系统域名。
   表1配置参数说明 
   | 配置参数                    | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | 示例                             |
   |:---|:---|:---|
   | server.url              | Loader服务的浮动IP地址和端口（21351）。 为了兼容性，此处支持配置多个IP地址和端口，并以","进行分隔。其中第一个必须是Loader服务的浮动IP地址和端口（21351），其余的可根据业务需求配置。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 | 10.0.0.1:21351                 |
   | authentication.type     | 登录认证的方式。 - "kerberos"，表示使用安全模式，进行Kerberos认证。Kerberos认证提供两种认证方式：密码和keytab文件。  - "simple"，表示使用普通模式，不进行Kerberos认证。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | kerberos                       |
   | authentication.user     | 普通模式或者使用密码认证方式时，登录使用的用户。 keytab登录方式，则不需要设置该参数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             | bar                            |
   | authentication.password | 使用密码认证方式时，登录使用的用户密码。 普通模式或者keytab登录方式，则不需要设置该参数。 用户需要对密码加密，加密方法： 1. 进入"encrypt_tool"所在目录。例如，Loader客户端安装目录为"/opt/hadoopclient/Loader"，则执行如下命令。 **cd /opt/hadoopclient/Loader/loader-tools-1.99.3**   2. 执行以下命令，对非加密密码进行加密。命令中如果携带认证密码信息可能存在安全风险，在执行命令前建议关闭系统的history命令记录功能，避免信息泄露。 **./encrypt_tool** *未加密的密码* 得到加密后的密文，作为"authentication.password"的取值。 说明： 非加密密码中含有特殊字符时需要转义。例如，$符号属于特殊字符，可使用单引号进行转义；非加密密码中含有单引号时可用双引号进行转义，非加密密码中含有双引号应使用反斜杠\\进行转义。可参考Shell的转义字符规则。    | -                              |
   | use.keytab              | 是否使用keytab方式登录。 - true，表示使用keytab文件登录  - false，表示使用密码登录。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | true                           |
   | client.principal        | 使用keytab认证方式时，访问Loader服务的用户规则。 普通模式或者密码登录方式，则不需要设置该参数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | loader/hadoop                  |
   | client.keytab.file      | 使用keytab认证方式登录时，使用的keytab文件所在目录。 普通模式或者密码登录方式，则不需要设置该参数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   | /opt/client/conf/loader.keytab |
      
   
   
4. 执行以下命令，进入交互模式。 
   **source /opt/client/bigdata_env**
   **cd /opt/client/Loader/loader-tools-1.99.3/sqoop-shell**
   **./sqoop2-shell**
   上述命令通过读取配置文件获取认证信息。
   也可以直接通过密码或者Kerberos认证。
   使用密码进行认证：
   **./sqoop2-shell -uk false -u** *username* **-p** *encryptedPassword*
   命令中如果携带认证密码信息可能存在安全风险，在执行命令前建议关闭系统的history命令记录功能，避免信息泄露。
   使用Kerberos认证：
   **./sqoop2-shell -uk true -k** *user.keytab* **-s** *userPrincipal*
   ```
   Welcome to sqoop client
   Use the username and password authentication mode
   Authentication success.
   Sqoop Shell: Type 'help' or '\h' for help.
   sqoop:000>
   ```
   
   
5. 执行以下命令，查看当前连接器对应的ID。 
   **show connector**
   显示如下信息：
   ```
   +----+----------------------------+----------------+----------------------------------------------------------------------+
   | Id |            Name            |    Version     |                                Class                                 |
   +----+----------------------------+----------------+----------------------------------------------------------------------+
   | 1  | generic-jdbc-connector     | 2.0.6-SNAPSHOT | org.apache.sqoop.connector.jdbc.GenericJdbcConnector                 |
   | 2  | ftp-connector              | 2.0.5-SNAPSHOT | org.apache.sqoop.connector.ftp.FtpConnector                          |
   | 3  | hdfs-connector             | 2.0.5-SNAPSHOT | org.apache.sqoop.connector.hdfs.HdfsConnector                        |
   | 4  | oracle-connector           | 2.0.1-SNAPSHOT | org.apache.sqoop.connector.oracle.OracleConnector                    |
   | 5  | mysql-fastpath-connector   | 2.0.1-SNAPSHOT | org.apache.sqoop.connector.mysql.MySqlConnector                      |
   | 6  | sftp-connector             | 2.0.5-SNAPSHOT | org.apache.sqoop.connector.sftp.SftpConnector                        |
   | 7  | oracle-partition-connector | 2.0.6-SNAPSHOT | org.apache.sqoop.connector.oracle.partition.OraclePartitionConnector |
   +----+----------------------------+----------------+----------------------------------------------------------------------+
   ```
   根据如上信息，可知SFTP连接器类型ID为6。
   
   
6. 执行如下命令，创建连接器，根据提示输入具体的连接器信息。 
   **create connection -c** *连接器类型ID*
   例如，连接器类型的ID为6，则执行如下命令：
   **create connection -c 6**
   ```
   sqoop:000> create connection -c 6
   Creating connection for connector with id 6
   Please fill following values to create new connection object
   Name: sftp14
   Connection configuration
   Sftp server IP: 10.0.0.1
   Sftp server port: 22
   Sftp user name: root
   Sftp password: ************
   Sftp public key:
   New connection was successfully created with validation status FINE and persistent id 20
   sqoop:000>
   ```
   根据如上信息，可知连接器ID为20。
   
   
7. 根据连接器ID，执行如下命令，创建作业。 
   **create job -x** *连接器ID* **-t impor**t
   例如，连接器ID为20，则执行如下命令：
   **create job -x 20 -t import**
   显示如下信息：
   ```
   Creating job for connection with id 20
   Please fill following values to create new job object
   Name: sftp-hdfs-test
   File configuration
   Input path: /opt/tempfile
   File split type: 
     0 : FILE
     1 : SIZE
   Choose: 0
   Filter type: 
     0 : WILDCARD
     1 : REGEX
   Choose: 0
   Path filter: *
   File filter: *
   Encode type: 
   Suffix name: 
   Compression: 
   Output configuration
   Storage type: 
     0 : HDFS
     1 : HBASE_BULKLOAD
     2 : HBASE_PUTLIST
     3 : HIVE
   Choose: 0
   File type: 
     0 : TEXT_FILE
     1 : SEQUENCE_FILE
     2 : BINARY_FILE
   Choose: 0
   Compression format: 
     0 : NONE
     1 : DEFAULT
     2 : DEFLATE
     3 : GZIP
     4 : BZIP2
     5 : LZ4
     6 : SNAPPY
   Choose: 
   Output directory: /user/loader/test
   File operate type: 
     0 : OVERRIDE
     1 : RENAME
     2 : APPEND
     3 : IGNORE
     4 : ERROR
   Choose: 0
   Throttling resources
   Extractors: 2
   Extractor size: 
   New job was successfully created with validation status FINE  and persistent id 85
   sqoop:000>
   ```
   根据如上信息，可知作业ID为85。
   
   
8. 执行以下命令，启动作业。 
   **start job -j** *作业ID* **-s**
   例如，作业ID为85，则执行如下命令：
   **start job -j 85 -s**
   显示"SUCCEEDED"信息，则说明作业启动成功。
   ```
   Submission details
   Job ID: 85
   Server URL: https://10.0.0.0:21351/loader/
   Created by: admin
   Creation date: 2016-07-20 16:25:38 GMT+08:00
   Lastly updated by: admin
   2016-07-20 16:25:38 GMT+08:00: BOOTING  - Progress is not available
   2016-07-20 16:25:46 GMT+08:00: BOOTING  - 0.00 %
   2016-07-20 16:25:53 GMT+08:00: BOOTING  - 0.00 %
   2016-07-20 16:26:08 GMT+08:00: RUNNING  - 90.00 %
   2016-07-20 16:26:08 GMT+08:00: RUNNING  - 90.00 %
   2016-07-20 16:26:27 GMT+08:00: SUCCEEDED
   ```
   
   
 
#### 批量模式示例
1. 使用安装客户端的用户登录Loader客户端所在节点。
2. 执行以下命令，进入sqoop-shell工具的"conf"目录。例如，Loader客户端安装目录为"/opt/client/Loader/"。 
   **cd /opt/client/Loader/loader-tools-1.99.3/sqoop-shell/conf**
   
   
3. 执行以下命令，配置认证信息。 
   **vi client.properties**
   ```
   server.url=10.0.0.1:21351
   # simple or kerberos 
   authentication.type=simple 
   # true or false 
   use.keytab=true 
   authentication.user= 
   authentication.password= 
   client.principal=hdfs/hadoop@<系统域名> 
   # keytab file 
   client.keytab.file=./conf/login/hdfs.keytab
   ```
   表2配置参数说明 
   | 配置参数                    | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     | 示例                             |
   |:---|:---|:---|
   | server.url              | Loader服务的浮动IP地址和端口（21351）。 为了兼容性，此处支持配置多个IP地址和端口，并以","进行分隔。其中第一个必须是Loader服务的浮动IP地址和端口（21351），其余的可根据业务需求配置。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            | 10.0.0.1:21351                 |
   | authentication.type     | 登录认证的方式。 - "kerberos"，表示使用安全模式，进行Kerberos认证。Kerberos认证提供两种认证方式：密码和keytab文件。  - "simple"，表示使用普通模式，不进行Kerberos认证。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  | kerberos                       |
   | authentication.user     | 普通模式或者使用密码认证方式时，登录使用的用户。 keytab登录方式，则不需要设置该参数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        | bar                            |
   | authentication.password | 使用密码认证方式时，登录使用的用户密码。 普通模式或者keytab登录方式，则不需要设置该参数。 用户需要对密码加密，加密方法： 1. 进入"encrypt_tool"所在目录。例如，Loader客户端安装目录为"/opt/hadoopclient/Loader"，则执行如下命令。 **cd /opt/hadoopclient/Loader/loader-tools-1.99.3**   2. 执行以下命令，对非加密密码进行加密。 **./encrypt_tool** *未加密的密码* 得到加密后的密文，作为"authentication.password"的取值。 说明： 非加密密码中含有特殊字符时需要转义。例如，$符号属于特殊字符，可使用单引号进行转义；非加密密码中含有单引号时可用双引号进行转义，非加密密码中含有双引号应使用反斜杠\\进行转义。可参考Shell的转义字符规则。    | -                              |
   | use.keytab              | 是否使用keytab方式登录。 - true，表示使用keytab文件登录  - false，表示使用密码登录。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         | true                           |
   | client.principal        | 使用keytab认证方式时，访问Loader服务的用户规则。 普通模式或者密码登录方式，则不需要设置该参数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                | loader/hadoop                  |
   | client.keytab.file      | 使用keytab认证方式登录时，使用的keytab文件所在目录。 普通模式或者密码登录方式，则不需要设置该参数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              | /opt/client/conf/loader.keytab |
      
   
   
4. 执行以下命令，进入"sqoop2-shell"脚本所在目录，并在该目录下创建一个文本文件，例如"batchCommand.sh"。 
   **cd /opt/client/Loader/loader-tools-1.99.3/sqoop-shell**
   **vi batchCommand.sh**
   "batchCommand.sh"样例如下：
   ```
   //查看参数
   create connection -c 6 --help
   //创建连接器
   create connection -c 6 -name sftp-connection --connector-connection-sftpServerIp 10.0.0.1 --connector-connection-sftpServerPort 22 --connector-connection-sftpUser root --connector-connection-sftpPassword xxxxx
   //创建作业
   create job -t import -x 20 --connector-file-inputPath /opt/tempfile --connector-file-fileFilter * --framework-output-outputDirectory /user/loader/1 --framework-output-storageType HDFS --framework-throttling-extractorSize 120 --framework-output-fileType TEXT_FILE --connector-file-splitType FILE -name test
   //启动作业
   start job -j 85 -s
   ```
   其中xxxxx为连接器密码。
   
   
5. 执行如下命令，sqoop-shell工具将依次执行上述命令。 
   **./sqoop2-shell batchCommand.sh**
   也可以直接在命令里附带认证信息。
   使用密码认证：
   **./sqoop2-shell -uk false -u** *username* **-p** *encryptedPassword* **batchCommand.sh**
   使用Kerberos认证：
   **./sqoop2-shell -uk true -k** *user.keytab* **-s** *userPrincipal* **batchCommand.sh**
   显示"SUCCEEDED"信息，则说明作业启动成功。
   ```
   Welcome to sqoop client
   Use the username and password authentication mode
   Authentication success.
   sqoop:000> create connection -c 6 --help
   usage: Show connection parameters:
       --connector-connection-sftpPassword <arg>
       --connector-connection-sftpServerIp <arg>
       --connector-connection-sftpServerPort <arg>
       --connector-connection-sftpUser <arg>
       --framework-security-maxConnections <arg>
       --name <arg>
   ===> FINE
   sqoop:000> create connection -c 6 -name sftp-connection --connector-connection-sftpServerIp 10.0.0.1 --connector-connection-sftpServerPort 22 --connector-connection-sftpUser root --connector-connection-sftpPassword xxxxx
   Creating connection for connector with id 6
   New connection was successfully created with validation status FINE and persistent id 20
   ===> FINE
   sqoop:000> create job -t import -x 20 --connector-file-inputPath /opt/tempfile --connector-file-fileFilter * --framework-output-outputDirectory /user/loader/1 --framework-output-storageType HDFS --framework-throttling-extractorSize 120 --framework-output-fileType TEXT_FILE --connector-file-splitType FILE -name test
   Creating job for connection with id 20
   New job was successfully created with validation status FINE  and persistent id 85
   ===> FINE
   Submission details
   Job ID: 85
   Server URL: https://10.0.0.0:21351/loader/
   Created by: admin
   Creation date: 2016-07-20 16:25:38 GMT+08:00
   Lastly updated by: admin
   2016-07-20 16:25:38 GMT+08:00: BOOTING  - Progress is not available
   2016-07-20 16:25:46 GMT+08:00: BOOTING  - 0.00 %
   2016-07-20 16:25:53 GMT+08:00: BOOTING  - 0.00 %
   2016-07-20 16:26:08 GMT+08:00: RUNNING  - 90.00 %
   2016-07-20 16:26:08 GMT+08:00: RUNNING  - 90.00 %
   2016-07-20 16:26:27 GMT+08:00: SUCCEEDED
   ```
   
   
6. 批处理模式下，使用-c参数附带一条命令，sqoop-shell可以一次只执行附带的这一条命令。 
   执行如下命令将创建连接器。
   **./sqoop2-shell -c "create connection -c 6 -name sftp-connection --connector-connection-sftpServerIp 10.0.0.1 --connector-connection-sftpServerPort 22 --connector-connection-sftpUser root --connector-connection-sftpPassword xxxxx** **"**
   可以在命令里直接附带认证信息。
   使用密码认证：
   **./sqoop2-shell -uk false -u** *username* **-p** *encryptedPassword* **-c "create connection -c 6 -name sftp-connection --connector-connection-sftpServerIp 10.0.0.1 --connector-connection-sftpServerPort 22 --connector-connection-sftpUser root --connector-connection-sftpPassword xxxxx** **"**
   使用Kerberos认证：
   **./sqoop2-shell -uk true -k** *user.keytab* **-s** *userPrincipal* **-c "create connection -c 6 -name sftp-connection --connector-connection-sftpServerIp 10.0.0.1 --connector-connection-sftpServerPort 22 --connector-connection-sftpUser root --connector-connection-sftpPassword xxxxx** **"**
   显示"FINE"信息，则说明连接创建成功。
   ```
   Welcome to sqoop client
   Use the username and password authentication mode
   Authentication success.
   sqoop:000> create connection -c 6 -name sftp-connection --connector-connection-sftpServerIp 10.0.0.1 --connector-connection-sftpServerPort 22 --connector-connection-sftpUser root --connector-connection-sftpPassword xxxxx
   Creating connection for connector with id 6
   New connection was successfully created with validation status FINE and persistent id 20
   ===> FINE
   ```
   
   
 
