Sqoop 简明教程
Sqoop - Codegen
此章节介绍了“codegen”工具的重要性。从面向对象的应用程序的角度来看,每个数据库表都包含一个 DAO 类,该类包含“getter”和“setter”方法,以初始化对象。此工具 (-codegen) 自动生成 DAO 类。
This chapter describes the importance of ‘codegen’ tool. From the viewpoint of object-oriented application, every database table has one DAO class that contains ‘getter’ and ‘setter’ methods to initialize objects. This tool (-codegen) generates the DAO class automatically.
它基于表架构结构以 Java 形式生成 DAO 类。在导入流程中会对 Java 定义进行实例化。此工具的主要用途是检查 Java 是否丢失了 Java 代码。如果是,它将使用字段之间的默认分隔符创建新版本的 Java。
It generates DAO class in Java, based on the Table Schema structure. The Java definition is instantiated as a part of the import process. The main usage of this tool is to check if Java lost the Java code. If so, it will create a new version of Java with the default delimiter between fields.
Syntax
以下是 Sqoop codegen 命令的语法。
The following is the syntax for Sqoop codegen command.
$ sqoop codegen (generic-args) (codegen-args)
$ sqoop-codegen (generic-args) (codegen-args)
Example
我们来看一个在 userdb 数据库中为 emp 表生成 Java 代码的示例。
Let us take an example that generates Java code for the emp table in the userdb database.
以下命令用于执行给定示例。
The following command is used to execute the given example.
$ sqoop codegen \
--connect jdbc:mysql://localhost/userdb \
--username root \
--table emp
如果命令执行成功,它将在终端上产生以下输出。
If the command executes successfully, then it will produce the following output on the terminal.
14/12/23 02:34:40 INFO sqoop.Sqoop: Running Sqoop version: 1.4.5
14/12/23 02:34:41 INFO tool.CodeGenTool: Beginning code generation
……………….
14/12/23 02:34:42 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /usr/local/hadoop
Note: /tmp/sqoop-hadoop/compile/9a300a1f94899df4a9b10f9935ed9f91/emp.java uses or
overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
14/12/23 02:34:47 INFO orm.CompilationManager: Writing jar file:
/tmp/sqoop-hadoop/compile/9a300a1f94899df4a9b10f9935ed9f91/emp.jar
Verification
我们来看一下输出。以粗体显示的路径是 emp 表的 Java 代码生成和存储的位置。让我们使用以下命令验证该位置的文件。
Let us take a look at the output. The path, which is in bold, is the location that the Java code of the emp table generates and stores. Let us verify the files in that location using the following commands.
$ cd /tmp/sqoop-hadoop/compile/9a300a1f94899df4a9b10f9935ed9f91/
$ ls
emp.class
emp.jar
emp.java
如果你希望进行深入的验证,请将 userdb 数据库中的 emp 表与以下目录中的 emp.java 进行比较
If you want to verify in depth, compare the emp table in the userdb database and emp.java in the following directory
/tmp/sqoop-hadoop/compile/9a300a1f94899df4a9b10f9935ed9f91/.