Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Tuesday, February 15, 2011

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<String, Object> globalMap = new HashMap<String, Object>();

   public static void setGlobalMap(Map<String, Object> _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.

8 comments:

  1. Thanks for your trick.

    I suggest used String.valueOf in place of cast with (string), like:

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

    ReplyDelete
  2. Thanks for the suggestion AntOineC. Using String.valueOf() is an improvement because it will handle anything put on the globalMap without a ClassCastException.

    globalMap.put("myname", "carl");
    globalMap.put("mynum", new Integer(0)); // can't cast to String
    globalMap.put("mydate", new java.util.Date()); // can't cast to String

    ReplyDelete
  3. Hey Carl, unless this is you, it appears someone has ripped off your blog mate:
    http://talendhunter.blogspot.com.au/2013/06/shorthand-globals-for-talend-open-studio.html

    ReplyDelete
    Replies
    1. That isn't me. You're better off using this blog because I update the posts and respond to comments.

      Delete
  4. hi
    I need to know how to declare global variable in talend using the code without using component.can u help me out with thi...

    ReplyDelete
    Replies
    1. You don't need a component to access the global. Put globalMap.get("myvar") in any expression such as a line in a tMap.

      You don't need to declare globalMap; it already exists. To add values to globalMap, use a tJava or other component to call globalMap.put("myvar").

      Alternatively, you can use a Context Variable which works well for configuration values that will stay constant throughout the project but are set when the job starts.

      Good luck

      Delete
    2. Hi carl
      Thank you for helping me out. I need to know more about this can I get in touch with you..
      my facebook name is sivakumar or use my mail id sivaredarrow@gmail.com. Because I need to know a lot about talend...can you help me out

      Delete
  5. jobs worldwide

    We are leading top modeling agency for music producers, talents and models in Singapore. We have model talents agency for you in Singapore.

    to get more - https://blog.mustdotravels.com/

    ReplyDelete