Talend tJavaRow component accepts input rows and generates the output rows. We can use this tJavaRow to write a custom Java code to perform any transformation. In general, people coming from Java backgrounds use this tJavaRow to accomplish anything or everything.
In this example, we use this Talend tJavaRow component to write a Java code that performs string conversations and mathematical operations.
Talend tJavaRow example
First, we used tDBConnection, tDBCommit, tDBInput to establish SQL connection, and select Customer table.
Next, drag the Talend tJavaRow component from palette to job design. From the tooltip, tJavaRow allows us to manually enter the main java code part of a component.
Within the Component tab it has the java code section with some code samples and schema options. Please check the schema using the Edit Schema button.
Clicking the Generate Code button will generate the following code. It assigns the output row (tJavaRow output) values to input_row (values coming from tDBInput). It means the following code will read all the records and assign them to tJavaRow.
Let me assign a tLogRow to tJavaRow to check whether the records are coming. From the below screenshot, you can see the result.
Let me add a few more columns to the tJavaRow output by clicking the Edit Schema button in the Component tab.
As you can see from the below code:
- FullName: Concatenation of First Name and Last Name and a space between them.
- Upper_Edu = We used Talend toUpperCase() function to convert the Education column to Uppercase.
- Lower_Occ = We used Talend toLowerCase() function to convert the Occupation column to Lowercase.
- NewIncome: Here, we were adding a 15% bonus to existing income.
And the Java code that we used the above is
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;
Please run the tJavaRow job to see the below result.
Let me replace the tLogRow with tDBOutput to save the result in a Talend_JavaRow.
Let us run the Talend tJavaRow job.