Creating 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 to 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 Repository, click the Code node, then Routines, then right-click on Routines and select the Create routine option to create a user routine.



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



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



    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.

  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();
        }
Icon

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

Call the routine in a Job

  1. Create a Job called CallRoutineDemo, the job design is as below:

    tFixedFlowInput: generate input data for testing.

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

    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]

Related Files

 

  File Modified
  ZIP Archive ExampleRoutineAndJobs.zip Jul 26, 2014 by
Version History
Revision #:
1 of 1
Last update:
‎04-17-2017 07:27 PM
Updated by:
 
Labels (1)
Contributors