Wednesday, May 6, 2015

How to tJava component in Talend !!

The tJava component allows one-off logic to be added to a job. Common uses of tJava include setting global or context variables prior to the main data processing stages and printing logging messages.

Here I have used Employee Table as FileInputDelimited.

Employee_ID,Last_Name,First_Name,Birth_Date
1,Lasson,Samuel,7/2/1969
2,Ortega,lee,9/19/1928
3,Zant,Thi,3/23/1938
4,Cohen,John,4/21/1927
5,Park,Umar,12/8/1968
6,Knipp,Troy,2/19/1952
7,Lunberg,Greg,8/30/1963
8,Brown,Sami,9/19/1958
9,Barnhill,Pascal,7/2/1969
10,Rose,Aaron,2/13/1956

1. Drag the following components from the palette and drop it to the design work space. And connect each of them as shown in the screenshot.

As per the below screen shot you can search for the required components used in the job and use same connection between the components to get the desired output as per mentioned below in output section. There can be many ways to do the same things and this is one of them.


2. Open the tJava_1 component Basic settings properties.
3. Type the following code:
System.out.println("Executing job "+jobName+" at "+TalendDate. getDate("YYYY-MM-dd HH:mm:ss"));
4. Open the tJava_2 component.
5. Type the following code:
context.NumberOfRows= ((Integer)globalMap.get("tFileInputDelimited_1_NB_LINE"));

System.out.println("Total number of rows in input file - " + context.NumberOfRows);

Here we have used the context variable which I have created in my previous post how to create a context .Context variable used is NumberOfRows.


7. In tLogRow component select Table option to display the result in table format.
6. Run the job. You will see that message is printed showing the job name and the  date and time of execution then Employee data and at last total number of rows in input file.

Starting job how_to_use_tjava_component at 14:37 06/05/2015.

[statistics] connecting to socket on port 3568
[statistics] connected
Executing job how_to_use_tjava_component at 2015-05-06 14:37:10
.-----------+---------+----------+----------+-------------|.
|                 tLogRow_1                                            |
|=----------+---------+----------+---------+------------- |
|Employee_ID|Last_Name|First_Name|Birth_Date|
|=---------------+-----------+-------------+--------------|
|1                     |Lasson       |Samuel       |7/2/1969   |
|2                     |Ortega        |lee              |9/19/1928 |
|3                     |Zant           |Thi              |3/23/1938 |
|4                     |Cohen        |John            |4/21/1927 |
|5                     |Park           |Umar           |12/8/1968 |
|6                     |Knipp        |Troy            |2/19/1952 |
|7                     |Lunberg     |Greg           |8/30/1963 |
|8                     |Brown        |Sami           |9/19/1958 |
|9                     |Barnhill      |Pascal         |7/2/1969  |
|10                   |Rose           |Aaron         |2/13/1956 |
'-----------------+-------------+--------------+-----------|

Total number of rows in input file - 10
[statistics] disconnected
Job how_to_use_tjava_component ended at 14:37 06/05/2015. [exit code=0]

No comments:

Post a Comment