Talend tJavaFlex

Talend tJavaFlex 组件提供了一个代码编辑器,用于编写个性化或自定义的 Java 代码。我们可以使用此 tJavaFlex 组件将 Java 代码集成到 Job 工作流中。

在本节中,我们使用一个简单的 Java 程序在 tLogRow 中显示数组。首先,将 Talend tJavaFlex 组件拖放到 Job 设计中。

Add Talend tJavaFlex to the workflow

首先,我将点击 Edit Schema 按钮,并添加整数类型的 SNo 列和字符串类型的 Languages 列。

Choose or add the Column Names

从组件选项卡截图可以看出,它有三个部分。您可以使用 Start Code、Main Code 和 End Code 部分来划分 Java 代码或程序。为了 Talend 的演示目的,我们声明了一个包含不同编程语言的字符串数组。

在这里,我们还使用 println 语句打印了一个简单的起始消息。您可能会注意到我们使用了 tLogRow 来显示输出。接下来,我们使用 for 循环来迭代数组,并将值赋给我们在前面模式中创建的两个新列。

在 end code 部分,我们使用了另一个 println 语句来打印结束消息。

Go to Components Tab to write code

我们使用的 Java 代码是

// start part of your Java code
System.out.println("-----Java Code Start Point-----");
String [] arr = {"C", "C++", "C#", "Java", "Python", "JavaScript"}

// here is the main part of the component,
// a piece of code executed in the row
// loop
for(i = 0; i < arr.length; i++)
{
row1.SNo = i;
row1.Languages = arr[i];

// end of the component, outside/closing the loop
}
System.out.println("-----Java Code Start Point-----");

我将运行这个 Talend tJavaFlex Job。

run the Talend tJavaFlex job

让我向您展示 Talend tJavaFlex 组件的另一个示例。首先,我将更改模式,并添加整数类型的 Array_a、Array_b 和 Sum_of_aAndb 列。

接下来,我们声明了两个整数类型的二维数组。在 for 循环中,我们将这些值赋给了两个数组 Array_a 和 Array_b。接下来,我们还执行了这两个数组相加的算术运算,并将这些值赋给了 Sum 数组。

Write a Complex array code in Talend tJavaFlex

Java 代码

// start part of your Java code
System.out.println("-----Java Code Start Point-----");
int[][] a = { {15, 25, 35}, {45, 55, 65} };
int[][] b = {{12, 22, 32}, {55, 25, 85} };
int rows, columns;

// here is the main part of the component,
// a piece of code executed in the row
// loop
for(rows = 0; rows < a.length; rows++) {
	for(columns = 0; columns < a[0].length; columns++) { 
		row1.Array_a = a[rows][columns];
		row1.Array_b = b[rows][columns];
		row1.Sum_of_aAndb = a[rows][columns] + b[rows][columns];

// end of the component, outside/closing the loop
	}			
}
System.out.println("-----Java Code Start Point-----");

您可以看到 tJavaFlex 的输出。

Run the tJavaFlex Job