Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
There are three Java components in the Custom Code family: tJava, tJavaRow, and tJavaFlex. These Java components allow you to integrate custom Java code in a Talend program. This article explains the difference between these three components, and explains how to use them in a Job.
This article was written with:
This article applies to all versions of Talend Studio.
You can use a tJava component to integrate your custom Java code into a Talend program. It applies exclusively to the start of the generated code of the subjob: it will be executed first, but only once in the subjob. Normally, tJava has no input or output data flow and is used as a separate subjob.
The following example shows how you can use a tJava component:
The source data file read by tFileInputDelimited_1 includes the following data:
1;Shong 2;Elisa 3;Sabrina
Some Java code should then be inserted in the tJava_1 component in order to obtain the number of records being processed:
int nb_line=(Integer)globalMap.get("tFileInputDelimited_1_NB_LINE"); System.out.println("The total number of records are read from the text file is: " +nb_line);
Execute the Job by pressing F6. You can see the following results in the console:
Starting job aaa at 15:42 22/09/2013. [statistics] connecting to socket on port 3589 [statistics] connected 1|Shong 2|Elisa 3|Sabrina The total number of records are read from the text file is: 3 [statistics] disconnected Job aaa ended at 15:42 22/09/2013. [exit code=0]
The actual records from the input file are printed into the console natively. In addition, due to the use of the tJava component, the number of records also displays on the console.
The tJavaRow code applies exclusively to the main part of the generated code of the subjob. The Java code inserted through the tJavaRow will be executed for each row. Generally, the tJavaRow component is used as an intermediate component and you are able to access the input flow and transform the data.
The following use case shows a typical Job using a tJavaRow:
The tFileInputDelimited_1 reads the same text file as in the tJava example.
The following Java code needs to be inserted in the tJavaRow_1 component to transform the data. In this use case, it converts the column names to upper case.
output_row.id = input_row.id; output_row.name = (input_row.name).toUpperCase();
Execute the Job by pressing F6. You can see the following results in the console:
Starting job aaa at 16:27 22/09/2013. [statistics] connecting to socket on port 3393 [statistics] connected 1|SHONG 2|ELISA 3|SABRINA [statistics] disconnected Job aaa ended at 16:27 22/09/2013. [exit code=0]
This example shows that it is possible to access the input flow using a dedicated variable and following a specific syntax, such as: input_row.name. The source data is processed at runtime by the tJavaRow component.
The tJavaFlex component has three Java code parts (start, main, end) that enable you to enter personalized code for different purposes. The start part will be executed first, but only once in the subjob. The main part will be executed for each row. You are able to access the input flow and modify the data. The source data is processed at runtime by tJavaFlex. The end part will be executed at the end of the subjob, but only once.
The following is a simple use case using tJavaFlex:
The tFileInputDelimited_1 still reads the same text file as in the tJava and tJavaRowexamples.
The following Java code needs to be inserted in the three Java-code parts of tJavaFlex:
System.out.println("******The subjob begins to work!******"); int nb_line=0;
row2.name=(row1.name).toUpperCase(); nb_line++;
End code:
System.out.println("The total number of processed data is: "+nb_line); System.out.println("******The subjob finishes!******");
Execute the Job by pressing F6. You can see the following results in the console:
Starting job aaa at 16:47 22/09/2013. [statistics] connecting to socket on port 3641 [statistics] connected ******The subjob begins to work!****** 1|SHONG 2|ELISA 3|SABRINA The total number of processed data is: 3 ******The subjob finishes!****** [statistics] disconnected Job aaa ended at 16:47 22/09/2013. [exit code=0]
Basically tJavaFlex is a combination of tJava and tJavaRow, mixing injection of code on a one-shot basis at the start/end of a Job with real data transformations for each row.
Through the examples above, you notice that tJava is used to execute a piece of Java code as a separate subjob in most cases. The Java code will be executed only once, whereas the Java code of the tJavaRow will be executed for each row. tJavaRow is normally used as an intermediary component: you are able to access and transform the input data flow. If you want to do some initialization operations at the beginning of subjob or other processing operations at the end of subjob, the tJavaFlex is the best component to use.