Tuesday, June 18, 2013

Shorthand Globals for Talend Open Studio


In Talend Open Studio, even with code complete, it's sometimes difficult to work with long strings for reading global variables.  Use a single function 'glstr' to reduce the clutter in your expressions.

Reading global variables in Java usually involves a cast, a variable, a method, and a key.  Using the technique in this blog post, this can be reduced to a method and a key.

The typical way of accessing a global,

(String)globalMap.get("SYSTEM_NAME")

Can be reduced to

glstr("SYSTEM_NAME")


Futhermore, this technique can serve as the basis for a useful library that provides a shorthand for other global access.  This can include other casting or data conversion.  Another function could be something like 'glint' which will either cast the global to an Integer, parse a String global into an Integer, or truncate a Double global.

Take the following Talend Open Studio Job.

Talend Open Studio Job with glstr Function
This job uses 5 components.
  • Init glstr. Import the GlobalUtils class and glstr function.  Initialize the GlobalUtils class with the Job's globalMap.
  • tSetGlobalVar. Set a global variable.
  • tForEach, tIterateToFlow, tLogRow.  Iterate over one row, writing out the global variable using glstr.
The following is the GlobalUtils routine.  It's created using the "Create routine" function.  See "User Defined Functions with Talend Open Studio" for instructions on how to do this.


package routines;

import java.util.HashMap;
import java.util.Map;

public class GlobalUtils {

   private static Map globalMap = new HashMap();

   public static void setGlobalMap(Map _globalMap) {
     globalMap = _globalMap;
   }

   public static String glstr(String _key) {
     return (String)globalMap.get(_key);
   }
}


Init glstr is a tJava that has entries in the "Basic settings" and "Advanced settings" tabs.  The following appears in Basic.

GlobalUtils.setGlobalMap( globalMap );


And the following appears in Advanced.  Notice the static import which lets you refer to glstr rather than GlobalUtils.glstr.


import routines.GlobalUtils;
import static routines.GlobalUtils.glstr;


tSetGlobalVar sets a single variable with Key="myglobal" and Value="someglobalvalue123".

The tIterateToFlow component actually invokes the glstr. Here is a screenshot showing the call. tIterateToFlow uses a single-field schema with a string column 'Field1'.

Talend Open Studio Calling 'glstr'
With a well-placed static import, commonly used functions can be reduced to a minimum of keystrokes.  A strategy like this can be very effective in larger tMaps where long expressions will scroll out-of-view.

No comments: