[Talend Open Studio Data Integration] How to use global variable in Talend (Global Map)

[Talend Open Studio Data Integration] How to use global variable in Talend (Global Map)

How to use global variable in Talend

If we need to use a variable in a job globally, we can use Global Variable in Talend.

By utilizing global variable, we don't need to put same value on each component. Once we changes a global variable, all component's variable is changed at once.

Talend provides two ways to define global variable. We are going to look at the two ways.



Define global variable by using tSetGlobalVar component

By using tSetGlobalVar component, we can easily define global variable. Global variable is combination of "variable name" and "value".

Let's create a sample job by using tSetGlobalVar component. As the following image, we need to place tSetGlobalVar component and tJava component, then  each other need to be connected by "On Subjob OK".

In tSetGlobalVar component setting, press green plus button (+) to add a new global variable. We need to enter "foo" for Key and TalendDate.getDate("YYYY-MM-dd HH:mm:ss") for Value.

TalendDate.getDate("YYYY-MM-dd HH:mm:ss") returns current time stamp. We can write Java code like this in Value field.

Then, open tJava component setting and type System.out.println(globalMap.get("foo")); in Code field as the following image. globalMap.get("Variable Name") means to get defined value from global variable. Since we defined "foo" as global variable in the tSetGlobalVar component,  we can get value by using globalMap.get("foo")

Also, System.out.println means to print out data or message. This is Java programming language.

Let's run the job now. As the following image, we could get current time stamp from global variable.
As we could see, we can get global variable which is defined in tSetGlobalVar component. Also, other components in the same job is able to get global variable.


Define global variable by using tJava component

We used tSetGlobalVar component to define global variable, but we can do the same thing by using tJava component.

Let's place two tJava components as follows as each other need to be connected by "On Subjob OK".

As the following image, open tJava_1 component setting and put the following code into "Code" field. globalMap.put takes two parameters. One is to define global variable name ("current_timestamp" in the sample job). The second parameter is a value or Java function. As a result of that, we can define global variable in tJava component.
globalMap.put("current_timestamp", TalendDate.getDate("YYYY-MM-dd HH:mm:ss"));

Then, open tJava_2 component setting and type the following code in the Code field as the following image. This is to print out global variable.
System.out.println(globalMap.get("current_timestamp"));

Let's run the job. As the following image, we could define current_timestamp as global variable in tJava_1 and could get the value in tJava_2 component through global variable.

Conclusion

We learned about how to use global variable by using tSetGlobalVar and tJava components. tSetGlobalVar is to define simple global variable. We need to use tJava to define global variable with complex calculation or if we need to use complex Java features.