Talend tJavaRow

Talend tJavaRow 组件接受输入行并生成输出行。我们可以使用此 tJavaRow 编写自定义 Java 代码以执行任何转换。通常,来自 Java 背景的人们使用此 tJavaRow 来完成任何或所有事情。 

在此示例中,我们使用此 tJavaRow 组件编写执行字符串转换和数学运算的 Java 代码。

Talend tJavaRow 示例

首先,我们使用 tDBConnection、tDBCommit、tDBInput 来建立 SQL 连接并选择 Customer 表。

Configure the connection and source table

接下来,将 Talend tJavaRow 组件从调色板拖到作业设计中。从工具提示中,tJavaRow 允许我们手动输入组件的主要 Java 代码部分。

Add Talend tJavaRow to the Workflow

组件选项卡有一个 Java 代码部分,其中包含一些代码示例和模式选项。请使用“编辑模式”按钮检查模式。

单击“生成代码”按钮将生成以下代码。它将输出行(tJavaRow 输出)的值分配给 input_row(来自 tDBInput 的值)。这意味着以下代码将读取所有记录并将它们分配给 tJavaRow。

Click the Generate Code button

让我将 tLogRow 分配给 Talend tJavaRow,以检查记录是否正在进入。从下面的屏幕截图中,您可以看到结果。

Run the Talend tJavaRow Job

通过单击组件选项卡中的“编辑模式”按钮,让我为 tJavaRow 输出添加更多列。

Add Few More columns in the Edit Schema window

正如您从下面的 Talend tJavaRow 代码中看到的

  • FullName:名字和姓氏的连接,中间有一个空格。
  • Upper_Edu = 我们使用 Talend 的 toUpperCase() 函数将 Education 列转换为大写。
  • Lower_Occ = 我们使用 Talend 的 toLowerCase() 函数将 Occupation 列转换为小写。
  • NewIncome:这里,我们将现有收入增加了 15% 的奖金。
Write the Java Code

而我们使用的 Java 代码如上

output_row.EmpID = input_row.EmpID;
output_row.FirstName = input_row.FirstName;
output_row.LastName = input_row.LastName;
output_row.Education = input_row.Education;
output_row.Occupation = input_row.Occupation;
output_row.YearlyIncome = input_row.YearlyIncome;
output_row.Sales = input_row.Sales;
output_row.HireDate = input_row.HireDate;
output_row.FullName = input_row.FirstName + " " +input_row.LastName;
output_row.Upper_Edu = input_row.Education.toUpperCase();
output_row.Lower_Occ = input_row.Occupation.toLowerCase();
output_row.NewIncome = input_row.YearlyIncome + input_row.YearlyIncome / 15;

请运行 tJavaRow 作业以查看以下结果。

Run the tJavaRow tool Job

让我用 tDBOutput 替换 tLogRow,将结果保存在 Talend_JavaRow 中。

Configure tDBOutput output table

让我们运行 tJavaRow 作业。

Run Talend tJavaRow job to save in sql table

请打开 Management Studio 以在 SQL 表中查看结果。

Destination Table