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.
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.
Connect the two components using a Row > Main connection.
Double-click the tFileInputDelimited component to display its Basic settings view in the Component tab.
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
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.
Double-click the tJavaRow component to display its Basic settings view in the Component tab.
Click Sync columns to make sure that the schema is correctly retrieved from the preceding component.
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");
In the Code field, input_row refers to the link that connects to tJavaRow.