Create a user routine and call it in a Job

Overview

Routines are reusable pieces of Java code. Routines enable you to write custom code in Java to optimize data processing, improve Job capacity, and extend Talend Studio features.

There are two types of routines in Talend Studio:

  • System routines: a number of system routines are provided. They are classified according to the type of data they process, such as: numerical, string, date. These types of routines are read-only, and you can call them directly in a Talend Job.
  • User routines: You can create your own new user routines or adapt existing routines.

This article explains how to create a user routine and call it in a Job.

 

Environment

This procedure was written with:

  • Talend Open Studio for Data Integration 5.5.0-r117820
  • JDK version: Sun JDK build 1.6.0_26-b03
  • Operating system: Windows 7 Professional, 64-bit

Talend verified this procedure to be compatible with all versions of Talend Studio.

 

Procedure

Create user routine

  1. In the Repository, click the Code node, then right-click Routines, then select the Create routine option to create a user routine.

    create_routine.png

     

  2. In the pop-up window, enter a routine name in the Name field. (This example uses MyRoutineDemo.) Then click Finish.

    Input_routine_name.png

     

  3. By default an example function called helloExample is provided whenever a new user routine is created

    example_function.png

     

    The function annotates are optional, but if you want the function to be displayed in a category so that the function can be chosen from the category when editing the code expression, as the below figure showed, you have to specify the {Category} property.

    routine_category.png

     

  4. Create a new function called reverseString that is used to reverse a string, the details as below:

    /**
     * reverseString: reverse the input string and return the result.
     *
     *
     * {talendTypes} String
     *
     * {Category} User defined
     *
     * {param} string type: this function requires an input parameter
     *
     * {example} reverseString("123456") # returns "654321"
     */
    public static String reverseString(String message) {
        StringBuffer sb = new StringBuffer();
        int len = message.length();
        for (int i = len - 1; i >= 0; i--) {
        sb.append(message.charAt(i));
        }
        return sb.toString();
        }

     

    Note: If you need to use external JARs in the function, right-click the routine name in the Repository and select the Edit Routine Libraries option to add the external JARs.

    edit_routine_lib.png

 

Call the routine in a Job

  1. Create a Job called CallRoutineDemo. The job design is shown below; the job is also available in the attached Zip file.

    demo_job.png

     

    tFixedFlowInput: generate input data for testing.

    tFixedFlowInput_1.png

     

    tMap: add a new column in the output table, and call the routine to transform the input data.

    tMap.png

     

    tLogRow: print the result on the console with table model.

     

  2. Execute the Job. The following results will be shown in the console:

    Starting job CallRoutineDemo at 12:18 24/07/2014.
    [statistics] connecting to socket on port 3987
    [statistics] connected
    .-----------+--------.
    |     tLogRow_1      |
    |=----------+-------=|
    |source_data|new_data|
    |=----------+-------=|
    |123456     |654321  |
    |abcd       |dcba    |
    '-----------+--------'
    [statistics] disconnected
    Job CallRoutineDemo ended at 12:18 24/07/2014. [exit code=0]
Version history
Revision #:
2 of 2
Last update:
‎06-23-2017 05:09 PM
Updated by:
 
Labels (1)
Tags (1)