Sqoop 简明教程

Sqoop - Eval

本章描述了如何使用 Sqoop 的“eval”工具。它允许用户对各自的数据库服务器执行用户定义的查询,并在控制台上预览结果。因此,用户可以预计导入结果表数据。使用 eval,我们可以评估任何类型的 SQL 查询,无论是 DDL 还是 DML 语句。

This chapter describes how to use the Sqoop ‘eval’ tool. It allows users to execute user-defined queries against respective database servers and preview the result in the console. So, the user can expect the resultant table data to import. Using eval, we can evaluate any type of SQL query that can be either DDL or DML statement.

Syntax

Sqoop eval 命令使用了以下语法。

The following syntax is used for Sqoop eval command.

$ sqoop eval (generic-args) (eval-args)
$ sqoop-eval (generic-args) (eval-args)

Select Query Evaluation

使用 eval 工具,我们可以评估任何类型的 SQL 查询。让我们举一个在 employee 数据库的 db 表中选择有限行的一个例子。以下命令用于使用 SQL 查询来评估给定的示例。

Using eval tool, we can evaluate any type of SQL query. Let us take an example of selecting limited rows in the employee table of db database. The following command is used to evaluate the given example using SQL query.

$ sqoop eval \
--connect jdbc:mysql://localhost/db \
--username root \
--query “SELECT * FROM employee LIMIT 3”

如果命令执行成功,它将在终端上产生以下输出。

If the command executes successfully, then it will produce the following output on the terminal.

+------+--------------+-------------+-------------------+--------+
| Id   | Name         | Designation | Salary            | Dept   |
+------+--------------+-------------+-------------------+--------+
| 1201 | gopal        | manager     | 50000             | TP     |
| 1202 | manisha      | preader     | 50000             | TP     |
| 1203 | khalil       | php dev     | 30000             | AC     |
+------+--------------+-------------+-------------------+--------+

Insert Query Evaluation

Sqoop eval 工具可以适用于建模和定义 SQL 语句。这意味着,我们也可以将 eval 用于插入语句。以下命令用于在 employee 数据库的 db 表中插入新行。

Sqoop eval tool can be applicable for both modeling and defining the SQL statements. That means, we can use eval for insert statements too. The following command is used to insert a new row in the employee table of db database.

$ sqoop eval \
--connect jdbc:mysql://localhost/db \
--username root \
-e “INSERT INTO employee VALUES(1207,‘Raju’,‘UI dev’,15000,‘TP’)”

如果命令执行成功,它将在控制台上显示更新行状态。

If the command executes successfully, then it will display the status of the updated rows on the console.

否则,您可以在 MySQL 控制台上验证员工表。以下命令用于使用 select’ 查询验证 employee 数据库 db 表的行。

Or else, you can verify the employee table on MySQL console. The following command is used to verify the rows of employee table of db database using select’ query.

mysql>
mysql> use db;
mysql> SELECT * FROM employee;
+------+--------------+-------------+-------------------+--------+
| Id   | Name         | Designation | Salary            | Dept   |
+------+--------------+-------------+-------------------+--------+
| 1201 | gopal        | manager     | 50000             | TP     |
| 1202 | manisha      | preader     | 50000             | TP     |
| 1203 | khalil       | php dev     | 30000             | AC     |
| 1204 | prasanth     | php dev     | 30000             | AC     |
| 1205 | kranthi      | admin       | 20000             | TP     |
| 1206 | satish p     | grp des     | 20000             | GR     |
| 1207 | Raju         | UI dev      | 15000             | TP     |
+------+--------------+-------------+-------------------+--------+