Scenario: Transforming data line by line using tJavaRow - 6.1

Talend Components Reference Guide

EnrichVersion
6.1
EnrichProdName
Talend Big Data
Talend Big Data Platform
Talend Data Fabric
Talend Data Integration
Talend Data Management Platform
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for Big Data
Talend Open Studio for Data Integration
Talend Open Studio for Data Quality
Talend Open Studio for ESB
Talend Open Studio for MDM
Talend Real-Time Big Data Platform
task
Data Governance
Data Quality and Preparation
Design and Development
EnrichPlatform
Talend Studio

In this scenario, the information of a few cities read from an input delimited file is transformed using Java code through the tJavaRow component and printed on the console.

Setting up the Job

  1. Drop a tFileInputDelimited component and a tJavaRow component from the Palette onto the design workspace, and label them to better identify their roles in the Job.

  2. Connect the two components using a Row > Main connection.

Configuring the components

  1. Double-click the tFileInputDelimited component to display its Basic settings view in the Component tab.

  2. In the File name/Stream field, type in the path to the input file in double quotation marks, or browse to the path by clicking the [...] button, and define the first line of the file as the header.

    In this example, the input file has the following content:

    City;Population;LandArea;PopDensity
    Beijing;10233000;1418;7620
    Moscow;10452000;1081;9644
    Seoul;10422000;605;17215
    Tokyo;8731000;617;14151
    New York;8310000;789;10452
  3. Click the [...] button next to Edit schema to open the [Schema] dialog box, and define the data structure of the input file. Then, click OK to validate the schema setting and close the dialog box.

  4. Double-click the tJavaRow component to display its Basic settings view in the Component tab.

  5. Click Sync columns to make sure that the schema is correctly retrieved from the preceding component.

  6. In the Code field, enter the code to be applied on each line of data based on the defined schema columns.

    In this example, we want to transform the city names to upper case, group digits of numbers larger than 1000 using the thousands separator for ease of reading, and print the data on the console:

    System.out.print("\n" + input_row.City.toUpperCase() + ":");
    System.out.print("\n - Population: " 
    + FormatterUtils.format_Number(String.valueOf(input_row.Population), ',', '.') + " people");
    System.out.print("\n - Land area: " 
    + FormatterUtils.format_Number(String.valueOf(input_row.LandArea), ',', '.') 
    + " km2");
    System.out.print("\n - Population density: " 
    + FormatterUtils.format_Number(String.valueOf(input_row.PopDensity), ',', '.') + " people/km2\n");

    Note

    In the Code field, input_row refers to the link that connects to tJavaRow.

Saving and executing the Job

  1. Press Ctrl+S to save your Job.

  2. Press F6 or click Run on the Run tab to execute the Job.

    The city information is transformed by the Java code set through tJavaRow and displayed on the console.