Skip to main content Skip to complementary content

Using beans and routines in a DSQL map

Availability-noteBeta
Use the java:call function to call global and custom routines in a DSQL map. This example only uses routines, but you can use the same method to call beans.

Before you begin

  • You have created an input and an output structure. You can use the JSON samples below to create your structures.

About this task

In this example, you have an array of objects that each contain two strings. The untrimmed string has extra spaces that need to be removed with a global routine. For the ordered string, you will create a custom routine to reverse the order of the letters.

The input data looks like this:
[
    {
        "untrimmed": " extra spaces before and after  ",
        "ordered": "abcdefgh"
    },
    {
        "untrimmed": " extra spaces before",
        "ordered": "ijklmno"
    },
    {
        "untrimmed": "extra spaces after     ",
        "ordered": "pqrstuvwxyz"
    }
]
The output structure looks like this:
[
    {
        "trimmed": "",
        "reversed": ""
    }
]

Procedure

  1. Create a custom routine to reverse a string:
    1. Open the Integration perspective and expand the Code node in the Repository tab.
    2. Right-click Custom Routine Jars, click Create Routine Jar, and name it dsqlmap.
    3. Right-click dsqlmap, click Create routine, and name the routine StringUtils.
    4. Replace the sample helloExample method in the routine with the following code and save it.
      public static String reverse(String s) {
          return new StringBuilder(s).reverse().toString();
          }
  2. Open Window > Preferences > Mapping and select the Enable java function calls in DSQL Maps check box.
    This is disabled by default for security reasons.
  3. Create a new DSQL Map and add the input and output structures.
  4. Click the output trimmed element and enter the following expression to call the TRIM method in the StringHandling global routine.
    java:call('routines.StringHandling', 'TRIM', untrimmed)
    This function requires at least two parameters:
    • The full class name of the routine
    • The method to call
    After those, you can add any parameters expected by the method. In this case, the method expects a string, so you can add a reference to the input untrimmed element.
  5. Click the output reversed element and add the following expression to call your custom routine:
    java:call('org.example.<yourproject>.routinesjar.dsqlmap.StringUtils', 'reverse', ordered)

    In this call, <yourproject> should be replaced with the name of your Talend Studio project. You can also check the line at the beginning of your routine to see its full class name.

Results

The map is configured to call the two Java functions, you can use the Test Run feature to test it. In this case, the following result is returned:
[
   {
      "trimmed": "extra spaces before and after",
      "reversed": "hgfedcba"
   },
   {
      "trimmed": "extra spaces before",
      "reversed": "onmlkji"
   },
   {
      "trimmed": "extra spaces after",
      "reversed": "zyxwvutsrqp"
   }
]

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!