Changing the value of a variable set in tGlobalVar

One Star

Changing the value of a variable set in tGlobalVar

Hi guys,
If I have a variable set using tGlobalVar example name and it's value is an empty string. How can I modify it using tJava?
and if I ever declare a variable in tJava is it possible that can I call that variable in other components? If yes how?

Thanks,
drixs
Community Manager

Re: Changing the value of a variable set in tGlobalVar

Hi
In tJava, you can set a value to the global variable which you have defined on tGlobalVar, for example:
tSetGlobalVar
|
onsuojobok
|
tJava
on tSetGlobalVar, add a new variable let's call it "myKey" and set its value as "oldValue"
on tJava, set a new value to global variable 'myKey" like this:
globalMap.put("mykey", "newValue")
You can use this variable in other component later as long as you declare a global variable before using it, call the variables in other component later like:
(String)globalMap.get("myKey") //the data type of value is String
(Integer)globalMap.get("myKey") //the data type of value is Integer/int
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Changing the value of a variable set in tGlobalVar

Hi Shong,
Just want to have a clarification.
For example I've declared a variable in tJava:
int record = 0;
can i call this variable in other component?
Community Manager

Re: Changing the value of a variable set in tGlobalVar

Hi
No, only a global variable can be accessed on other component. As you done, define a global variable using tSetglobalVar component or use this expression:
globalMap.put("key","value")
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Changing the value of a variable set in tGlobalVar

Hi shong,
I have some problem here.
I declare a variable using a tSetGlobalVar : Key: PrevComb | Value:"novalue"
and here is my job outline
tSetGlobalVar
|
(Main)
|
tFlowToIterate
|
(Iterate)
|
tJava
In my tJava , I have a condition ((String)globalMap.get("CurrentComb")) != ((String)globalMap.get("PrevComb")) and if it's true it will be performing this:
globalMap.put("PrevComb" , ((String)globalMap.get("CurrentComb")))
this means the CurrentComb value will be stored in PrevComb, am i right?
with this try to simulate in the next run it will be false because they have the same value, right?
But in my case on the next the result is true, it seems that PrevComb value is still "novalue".
Help me out thanks!
One Star

Re: Changing the value of a variable set in tGlobalVar

Just done a test and there's no value change in the variable
Community Manager

Re: Changing the value of a variable set in tGlobalVar

Hi
In my tJava , I have a condition ((String)globalMap.get("CurrentComb")) != ((String)globalMap.get("PrevComb"))

To determine whether one string is equals to another one, you should the use the method String.equals("anotherString"), so the expression should be:
((String)globalMap.get("CurrentComb")).equals(((String)globalMap.get("PrevComb")))
BTW, for the numeric type, such as Integer/int, you can use ==.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Changing the value of a variable set in tGlobalVar

Oh I see , Noted
How about this issue:
I declare a variable using a tSetGlobalVar : Key: PrevComb | Value:"novalue"
and here is my job outline
tSetGlobalVar
|
(Main)
|
tFlowToIterate
|
(Iterate)
|
tJava

globalMap.put("PrevComb" , ((String)globalMap.get("CurrentComb")))
this means the CurrentComb value will be stored in PrevComb, am i right?
with this try to simulate in the next run it will be false because they have the same value, right?
But in my case on the next the result is true, it seems that PrevComb value is still "novalue".
Community Manager

Re: Changing the value of a variable set in tGlobalVar

Oh I see , Noted
How about this issue:

;(, before you post this message, I think you shall to change the method equlas() as I said and test the job again.
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Changing the value of a variable set in tGlobalVar

;(, before you post this message, I think you shall to change the method equlas() as I said and test the job again.

Omg! Sorry shong!! Wasnt able to reply it clearly because it's almost end of office hours.
Thanks for the .equals() shong!
one more question:
in this situation:
tGlobalSetVar:
key | value
count | 0

tGlobalSetVar
|
Main
|
tFlowToIterate
|
iterate
|
tJava

in tJava
globalMap.put("count" , ((Integer))globalMap.get("count") + 1 );

This is similar with count++;
right?
Community Manager

Re: Changing the value of a variable set in tGlobalVar

Hi
No, because it always set 'count' back to 0 on tGlobalSetVar, and set 'count' to 1 on tJava, so the value always is: 0,1,0,1...
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Changing the value of a variable set in tGlobalVar

ohh.. so that's how it works.
Do you have any job layout which may similar above.
Community Manager

Re: Changing the value of a variable set in tGlobalVar

Hi
Yes, I done many similar jobs before, but it depends on your specific needs. Normally, I use the tJavaFlex component, for example:
tFileInputDelimited---tJavaFlex--tLogRow
on the begin part of tJavaFlex:
int count=0;
on the main part of tJavaFlex:
count=count+1;
on the end part of tJavaFlex, set the count to a global variable, so that it can be used later on other component:
globalMap.put("count",count);
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business