tJavaFlex - 6.3

Talend Open Studio for Big Data Components Reference Guide

EnrichVersion
6.3
EnrichProdName
Talend Open Studio for Big Data
task
Data Governance
Data Quality and Preparation
Design and Development
EnrichPlatform
Talend Studio

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.

tJavaFlex properties

Component family

Custom Code

 

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.

 

 

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 the article Verifying default values in a retrieved schema on Talend Help Center (https://help.talend.com).

  

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.