July 30, 2023

tJavaFlex – Docs for ESB 7.x

tJavaFlex

Provides a Java code editor that lets you enter personalized code in order to
integrate it in Talend
program.

tJavaFlex enables you to add Java
code to the Start/Main/End code sections of this component itself. 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.

tJavaFlex Standard properties

These properties are used to configure tJavaFlex running in the Standard Job framework.

The Standard
tJavaFlex component belongs to the Custom Code family.

The component in this framework is available in all Talend
products
.

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. When you create a Spark
Job, avoid the reserved word line when naming the
fields.

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.

 

Repository: You have already created the schema and stored it in the
Repository. You can reuse it in various projects and Job designs.

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.

You can find more details about how to
verify default values in retrieved schema in 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

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

Usage rule

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.

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.

tJavaFlex_1.png

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.

    tJavaFlex_2.png

  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.

    tJavaFlex_3.png

  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:
  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:
    tJavaFlex_4.png

    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:
    tJavaFlex_5.png

  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.

    tJavaFlex_6.png

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

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.

tJavaFlex_7.png

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.

    tJavaFlex_8.png

  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.

    tJavaFlex_9.png

  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:
  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:
    tJavaFlex_10.png

    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:
    tJavaFlex_11.png

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.

    tJavaFlex_12.png

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

Using tJavaFlex to display file content based on a dynamic schema

This scenario applies only to subscription-based Talend products.

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.

    tJavaFlex_13.png

  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.

    tJavaFlex_14.png

    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.

    tJavaFlex_15.png

  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.

    tJavaFlex_16.png

  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.

    tJavaFlex_17.png

  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:

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

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.

    tJavaFlex_18.png

    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.

Document get from Talend https://help.talend.com
Thank you for watching.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x