Wednesday, May 6, 2015

How to use tJavaRow component in Talend !!

The tJavaRow component allows Java logic to be performed for every record within a flow.

tJavaRow is called for every row processed, it is possible to create a global variable for a row that can be referenced by all components in a flow. This can be useful when pre and post field values are required for comparison purposes later in the flow.

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. Add FileInputDelimited , tJavaRow and tLogRow components from the palette.
2. Link the flows as shown in the following screenshot:--


3. Open the Edit schema button on tJavaRow component and you will see that there are no fields in the output.
 Highlight Employee_ID,Last_Name,First_Name,Birth_Date and click on the single arrow. 
4. Use the + button to add new columns Full_Name(String),First_Name_Length (Integer), Last_Name_Length(Integer) so that the schema looks like the following:
5. Close the schema by pressing OK button.

                                      

6. Then press the Generate code button in the main tJavaRow screen. The generated code will be as follows: 
//Code generated according to input schema and output schema is:--
output_row.Employee_ID = input_row.Employee_ID;
output_row.First_Name = input_row.First_Name;
output_row.Last_Name = input_row.Last_Name;
output_row.Full_Name = input_row.Birth_Date;
output_row.First_Name_Length = input_row.Birth_Date;
output_row.Last_Name_Length = input_row.Birth_Date;
output_row.Birth_Date = input_row.Birth_Date;


7. Replace the Generated code with this code:--
output_row.Employee_ID = input_row.Employee_ID;
output_row.First_Name = StringHandling.UPCASE(input_row.First_Name);
output_row.Last_Name =  StringHandling.UPCASE(input_row.Last_Name);
output_row.Full_Name =  StringHandling.UPCASE(input_row.First_Name +" "+input_row.Last_Name);
output_row.First_Name_Length=  StringHandling.LEN(input_row.First_Name);
output_row.Last_Name_Length=  StringHandling.LEN(input_row.Last_Name);
output_row.Birth_Date = TalendDate.parseDate("MM/dd/yyyy",input_row.Birth_Date) ;

8. In tLogRow component select Table option to display the result.

9. Run the job.

Starting job how_to_use_tjavarow_component at 15:02 06/05/2015.

[statistics] connecting to socket on port 3689
[statistics] connected
.-----------+----------+---------+---------------+-----------------+----------------+----------+--------------|.
|                                          tLogRow_1                                                                                          |
|=----------+----------+---------+---------------+-----------------+----------------+----------+-------------|
|Employee_ID|First_Name|Last_Name|Full_Name             |First_Name|Last_Name|Birth_        |
                                                                                              |_Length      |_Length     |Date           |
|=---------------+-------------+------------+-------------+-----------------------+-----------------------+---|
|1                     |SAMUEL   |LASSON  |SAMUEL LASSON|6                |6                |07/02/1969|
|2                     |LEE            |ORTEGA  |LEE ORTEGA       |3                 |6                |09/19/1928|
|3                     |THI             |ZANT       |THI ZANT             |3                 |4               |03/23/1938|
|4                     |JOHN         |COHEN    |JOHN COHEN       |4                 |5               |04/21/1927|
|5                     UMAR        |PARK       |UMAR PARK         |4                |4               |12/08/1968|
|6                     |TROY        |KNIPP      |TROY KNIPP          |4                |5               |02/19/1952|
|7                     |GREG        |LUNBERG|GREG LUNBERG  |4               |7               |08/30/1963|
|8                     |SAMI         |BROWN    |SAMI BROWN      |4                |5               |09/19/1958|
|9                     |PASCAL    |BARNHILL|PASCAL BARNHILL|6          |8               |07/02/1969|
|10                   |AARON     |ROSE         |AARON ROSE     |5                 |4               |02/13/1956|
'-----------+----------+---------+---------------+-----------------+----------------+-----------+-------------

[statistics] disconnected
Job how_to_use_tjavarow_component ended at 15:02 06/05/2015. [exit code=0]

Here you can see in the result that First_Name and Last_Name are in uppercase .
In Full_Name we have concatenated the First_Name and Last_Name with space in between.
 First_Name_Length and Last_Name_Length provide the length of  names.
In Birth_Date we have given it in date format.

How it works… The tJavaRow component is much like a 1 input to 1 output tMap, in that input columns can be ignored and new columns can be added to the output. Once the output fields have been defined the Generate code button will create a Java mapping for every output field. If the names are the same, then it will map correctly. If input fields are not found or are named differently, then it will automatically map the field in the same position in the input or the last known input field, so be careful when using this option if you have removed fields. In some cases, it is best to propagate all fields, generate the mappings and then remove unwanted fields and mappings.
Also, be aware that the Generated Code option will remove all code in the window. If you have code that you wish to keep, then ensure that you copy it into a text editor before regenerating the code.

No comments:

Post a Comment