tJavaFlex - 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

tJavaFlex properties

Component family

Custom Code

 

Function

tJavaFlex enables you to enter personalized code in order to integrate it in Talend program. With tJavaFlex, you can enter the three java-code parts (start, main and end) that constitute a kind of component dedicated to do a desired operation.

Purpose

tJavaFlex lets you add Java code to the Start/Main/End code sections of this component itself.

Basic settings

Schema and Edit Schema

A schema is a row description. It defines the number of fields (columns) to be processed and passed on to the next component. The schema is either Built-In or stored remotely in the Repository.

Since version 5.6, both the Built-In mode and the Repository mode are available in any of the Talend solutions.

Click Sync columns to retrieve the schema from the previous component in the Job.

This component offers the advantage of the dynamic schema feature. This allows you to retrieve unknown columns from source files or to copy batches of columns from a source without mapping each column individually. For further information about dynamic schemas, see Talend Studio User Guide.

This dynamic schema feature is designed for the purpose of retrieving unknown columns of a table and is recommended to be used for this purpose only; it is not recommended for the use of creating tables.

 

 

Built-In: You create and store the schema locally for this component only. Related topic: see Talend Studio User Guide.

 

 

Repository: You have already created the schema and stored it in the Repository. You can reuse it in various projects and Job designs. Related topic: see Talend Studio User Guide.

When the schema to be reused has default values that are integers or functions, ensure that these default values are not enclosed within quotation marks. If they are, you must remove the quotation marks manually.

For more details, see https://help.talend.com/display/KB/Verifying+default+values+in+a+retrieved+schema.

  

Click Edit schema to make changes to the schema. If the current schema is of the Repository type, three options are available:

  • View schema: choose this option to view the schema only.

  • Change to built-in property: choose this option to change the schema to Built-in for local changes.

  • Update repository connection: choose this option to change the schema stored in the repository and decide whether to propagate the changes to all the Jobs upon completion. If you just want to propagate the changes to the current Job, you can select No upon completion and choose this schema metadata again in the [Repository Content] window.

 

Data Auto Propagate

Select this check box to automatically propagate the data to the component that follows.

 

Start code

Enter the Java code that will be called during the initialization phase.

 

Main code

Enter the Java code to be applied for each line in the data flow.

 

End code

Enter the Java code that will be called during the closing phase.

Advanced settings

Import

Enter the Java code that helps to import, if necessary, external libraries used in the Main code box of the Basic settings view.

 

tStatCatcher Statistics

Select this check box to gather the Job processing metadata at a job level as well as at each component level.

Global Variables

ERROR_MESSAGE: the error message generated by the component when an error occurs. This is an After variable and it returns a string. This variable functions only if the Die on error check box is cleared, if the component has this check box.

A Flow variable functions during the execution of a component while an After variable functions after the execution of the component.

To fill up a field or expression with a variable, press Ctrl + Space to access the variable list and choose the variable to use from it.

For further information about variables, see Talend Studio User Guide.

Usage

You can use this component as a start, intermediate or output component. You can as well use it as a one-component subjob.

Limitation

You should know the Java language.

Scenario 1: Generating data flow

This scenario describes a two-components Job that generates a three-line data flow describing different personal titles (Miss, Mrs, and Mr) and displaying them on the console.

Setting up the Job

  1. Drop tJavaFlex and tLogRow from the Palette onto the design workspace.

  2. Connect the components together using a Row > Main link.

Configuring the tJavaFlex component

  1. Double-click tJavaFlex to display its Basic settings view and define its properties.

  2. Click the three-dot button next to Edit schema to open the corresponding dialog box where you can define the data structure to pass to the component that follows.

  3. Click the [+] button to add two columns: key and value and then set their types to Integer and String respectively.

  4. Click OK to validate your changes and close the dialog box.

  5. In the Basic settings view of tJavaFlex, select the Data Auto Propagate check box to automatically propagate data to the component that follows.

    In this example, we do not want to do any transformation on the retrieved data.

  6. In the Start code field, enter the code to be executed in the initialization phase.

    In this example, the code indicates the initialization of tJavaFlex by displaying the START message and sets up the loop and the variables to be used afterwards in the Java code:

    System.out.println("## START\n#");
    String [] valueArray = {"Miss", "Mrs", "Mr"};
    
    for (int i=0;i<valueArray.length;i++) {

  7. In the Main code field, enter the code you want to apply on each of the data rows.

    In this example, we want to display each key with its value:

    row1.key = i;
    row1.value = valueArray[i];

    Warning

    In the Main code field, "row1" corresponds to the name of the link that comes out of tJavaFlex. If you rename this link, you have to modify the code of this field accordingly.

  8. In the End code field, enter the code that will be executed in the closing phase.

    In this example, the brace (curly bracket) closes the loop and the code indicates the end of the execution of tJavaFlex by displaying the END message:

    }
    System.out.println("#\n## END");

  9. If needed, double-click tLogRow and in its Basic settings view, click the [...] button next to Edit schema to make sure that the schema has been correctly propagated.

Saving and executing the Job

  1. Save your Job by pressing Ctrl+S.

  2. Execute the Job by pressing F6 or clicking Run on the Run tab.

    The three personal titles are displayed on the console along with their corresponding keys.

Scenario 2: Processing rows of data with tJavaFlex

This scenario describes a two-component Job that generates random data and then collects that data and does some transformation on it line by line using Java code through the tJavaFlex component.

Setting up the Job

  1. Drop tRowGenerator and tJavaFlex from the Palette onto the design workspace.

  2. Connect the components together using a Row Main link.

Configuring the input component

  1. Double-click tRowGenerator to display its Basic settings view and the [RowGenerator Editor] dialog box where you can define the component properties.

  2. Click the plus button to add four columns: number, txt, date and flag.

  3. Define the schema and set the parameters to the four columns according to the above capture.

  4. In the Functions column, select the three-dot function [...] for each of the defined columns.

  5. In the Parameters column, enter 10 different parameters for each of the defined columns. These 10 parameters corresponds to the data that will be randomly generated when executing tRowGenerator.

  6. Click OK to validate your changes and close the editor.

Configuring the tJavaFlex component

  1. Double-click tJavaFlex to display its Basic settings view and define the components properties.

  2. Click Sync columns to retrieve the schema from the preceding component.

  3. In the Start code field, enter the code to be executed in the initialization phase.

    In this example, the code indicates the initialization of the tJavaFlex component by displaying the START message and defining the variable to be used afterwards in the Java code:

    System.out.println("## START\n#");
    int i = 0;

  4. In the Main code field, enter the code to be applied on each line of data.

    In this example, we want to show the number of each line starting from 0 and then the number and the random text transformed to upper case and finally the random date set in the editor of tRowGenerator. Then, we create a condition to show if the status is true or false and we increment the number of the line:

    System.out.print(" row" + i + ":");
    System.out.print("# number:" + row1.number);
    System.out.print (" | txt:" + row1.txt.toUpperCase());
    System.out.print(" | date:" + row1.date);
    if(row1.flag) System.out.println(" | flag: true");
    else  System.out.println(" | flag: false");
    
    i++;

    Warning

    In the Main code field, "row1" corresponds to the name of the link that connects to tJavaFlex. If you rename this link, you have to modify the code.

  5. In the End code field, enter the code that will be executed in the closing phase.

    In this example, the code indicates the end of the execution of tJavaFlex by displaying the END message:

    System.out.println("#\n## END");

Saving and executing the Job

  1. Save your Job by pressing Ctrl+S.

  2. Execute the Job by pressing F6 or clicking Run on the Run tab.

    The console displays the randomly generated data that was modified by the java command set through tJavaFlex.

Scenario 3: Using tJavaFlex to display file content based on a dynamic schema

This scenario describes a three-component Job that uses Java code through a tJavaFlex component to display the content of a file being transferred from the source to the target. As all the components in this Job support the dynamic schema feature, we can leverage this feature to save the time of configuring each column of the schema.

Setting up the Job

  1. Drop tFileInputDelimited, tJavaFlex, and tFileOutputDelimited from the Palette onto the design workspace.

  2. Connect the components in a series using Row > Main links.

  3. Label the components according to their roles in the Job.

Configuring the input and output components

  1. Double-click the tFileInputDelimited component, which is labeled Source, to display its Basic settings view where you can define the component properties.

    Warning

    The dynamic schema feature is only supported in Built-In mode and requires the input file to have a header row.

  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.

  3. In the Header field, type in 1 to use the first line of the file as the header.

  4. Click the [...] button next to Edit schema to open the [Schema] dialog box.

  5. Click the [+] button to add a column, give a name to the column, dyna in this example, and select Dynamic from the Type list. This dynamic column will retrieve the three columns, FirstName, LastName and Address, of the input file.

  6. Click OK to validate the setting and close the dialog box.

  7. Double-click the tFileOutputDelimited component, which is labeled Target, to display its Basic settings view where you can define the component properties.

  8. Define the output file path in the File Name field.

  9. Select the Include Header check box to include the header in the output file. Leave all the other settings are they are.

Configuring the tJavaFlex component

  1. Double-click tJavaFlex to display its Basic settings view and define the components properties.

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

  3. In the Start code field, enter the following code:

    System.out.println("Content of file being transferred:\n");
  4. In the Main code field, enter the code to be applied on each line of data.

    In this example, we want to display the content of the source file based on the defined dynamic schema column:

    System.out.println(row1.dyna);

    Warning

    In the Main code field, "row1" refers to the name of the link that connects to tJavaFlex. If you rename this link, you have to modify the code.

  5. In the End code field, enter the code that will be executed in the closing phase.

    In this example, the code indicates the end of the execution of tJavaFlex by displaying the "End of file" message:

    System.out.println("\nEnd of file");

Saving and executing the Job

  1. Save your Job by pressing Ctrl+S.

  2. Execute the Job by pressing F6 or clicking Run on the Run tab.

    The console displays the content of the input file. As the Data Auto Propagate check box is selected by default, the content of the file is intactly transferred to the output component.