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 |
- 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.
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' |
Thanks for your trick.
ReplyDeleteI suggest used String.valueOf in place of cast with (string), like:
public static String glstr(String _key) {
return String.valueOf(globalMap.get(_key));
}
Thanks for the suggestion AntOineC. Using String.valueOf() is an improvement because it will handle anything put on the globalMap without a ClassCastException.
ReplyDeleteglobalMap.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
Hey Carl, unless this is you, it appears someone has ripped off your blog mate:
ReplyDeletehttp://talendhunter.blogspot.com.au/2013/06/shorthand-globals-for-talend-open-studio.html
That isn't me. You're better off using this blog because I update the posts and respond to comments.
Deletehi
ReplyDeleteI need to know how to declare global variable in talend using the code without using component.can u help me out with thi...
You don't need a component to access the global. Put globalMap.get("myvar") in any expression such as a line in a tMap.
DeleteYou 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
Hi carl
DeleteThank 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
jobs worldwide
ReplyDeleteWe 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/