One Star

Global Variables using the tSetGlobalVar component

I have lookup file about 400 records and wanted to use the all rows as a global variables wherever needed to use them in transformation(in tMap component).
Is there any way to store the all lookup values in tSetGlobalVar component without typing each and every row(key & value) ? please help me in this case.
Thanks in advance !
Kyle
9 REPLIES
Community Manager

Re: Global Variables using the tSetGlobalVar component

Hello
You might need tFlowToIterate component, this component is used to iterate each row and put each column to global variable, so you can access the value of one column like 
(String)globalMap.get("row1.columnName")
Please describe your requirement with more details, so we can give you more accurate answers.
Regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Global Variables using the tSetGlobalVar component

Thank you Shong !!
I have input data as in column level and lookup as in row level, need to join both and get the respective Average to populate output.

Input:
ID      C1  C2 C3 C4 C5 C6 
X123    0   1   0   1   1   0
B456    1   0   1   0   0   0
lookup:
CODES  AVERAGE
C1         1.50
C2         0.50
C3         2.50
C4         4.50
C5         3.50
C6         1.50
so trying to use the tSetGlobalVar component and keep the all lookup values in it and fetching through globalMap.get as below

tSetGlobalVar----onsubjobok----> inputfile-----> tmap----->outputfile.
in tmap vatiable section placing below statements and getting respective AVERAGE
(String)globalMap.get("C1")
(String)globalMap.get("C2")
(String)globalMap.get("C3") ...etc


my question is... in future i will have more data in lookup file about 400 rows, so is there any way to load the all values in tSetGlobalVar compoent (directly read the values from the lookup file and keep them in tSetGlobalVar component to use in downstream compoents) ?
so far, rhall helping me on this case and here is link- https://www.talendforge.org/forum/viewtopic.php?id=53032
Thanks,
Kyle
Community Manager

Re: Global Variables using the tSetGlobalVar component

Hello Kyle 
You can set all the lookup rows to global variable by configuring the tsetGlobalVar as:
(tfilelist--iterate--for multiple lookup file)lookup_file---main(row1)---tsetGlobalVar
   |
onsubjobok
   |
inputfile-----> tmap----->outputfile.

on tsetGlobalVar
set the key column as: row1.CODES, set value column as: row1.AVERAGE

Regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Global Variables using the tSetGlobalVar component

Hi Shong-
I followed the same using the Double.parseDouble((String)globalMap.get("C1")) in tMap to get Average of C1 but getting NullPointerException error
lookup_file---main(row1)---tsetGlobalVar (set the key column as: row1.CODES, set value column as: row1.AVERAGE)
   |
onsubjobok
   |
inputfile-----> tmap----->outputfile.

Please help me if i'm wrong in using the function

Exception in component tMap_1
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
 
Regards
Kyle
Fifteen Stars

Re: Global Variables using the tSetGlobalVar component

That is because the globalMap value is null. You need to do a check for null value and not try to convert it to a Double if it is null
Rilhia Solutions
One Star

Re: Global Variables using the tSetGlobalVar component

Hi rhall..
don't have any NULLs in input(lookup) file, i tried with just one record which is not null and still getting the same error
Regards,
Kyle
Fifteen Stars

Re: Global Variables using the tSetGlobalVar component

OK. Are you sure that your globalMap variable key is "C1"? Because the globalMap is passing back null for that key. Java is case sensitive.
Rilhia Solutions
One Star

Re: Global Variables using the tSetGlobalVar component

Here is the flow i have.
lookup_file---main(row1)---tsetGlobalVar (key column as: row1.CODES, value column as: row1.AVERAGE) ... tlogrow
   |
onsubjobok
   |
inputfile-----> tmap----->outputfile.

I'm able to see correct records as below from the tlogrow output
row1.CODES  row1.AVERAGE
C1                  1.50
C2                  0.50
C3                  2.50
C4                  4.50
C5                  3.50
C6                  1.50
but when i use the Double.parseDouble((String)globalMap.get("C1")) getting error
just making sure, when i use Double.parseDouble((String)globalMap.get("row1.CODES")) it's giving me result but it's not useful
Thanks,
Kyle
Fifteen Stars

Re: Global Variables using the tSetGlobalVar component

Where is globalMap.get("C1") being set? the globalMap is just a HashMap which will only hold what is put in there. You have to put the values in there using Java, a tSetGlobalMap or a tFlowToIterate (other Talend components use it as well). Where is "C1" being add to the globalMap and what is being put in there? For some reason, when you are trying to use the value corresponding to the "C1" key, it is not there.
Rilhia Solutions