Skip to main content
Announcements
A fresh, new look for the Data Integration & Quality forums and navigation! Read more about what's changed.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

How to make multiple values available in the Global Map for a given key ...

Hello, i am setting a global variable in a Talend job by using the tSetGlobalVar component. I read a file that contains two fields, cust_id and cust_info and am setting a global variable the name of which is the cust_id and its value is the cust_info. But sometimes, the cust_id repeats but with a different value in its cust_info field. But downstream when i go to retrieve the information for that particular cust_id, only one value is available since each time that cust_id is run thru the tSetGlobalVar component, the global variable just gets the latest value. Is there a way to make both values available in the environment for a given cust_id ? I am wanting something that will in effect allow me to do the following :

 

int i = globalMap.get(row1.cust_id).count(); /* NOTE: this "count" function is theoretical at this point */

temp_info1 = globalMap.get(row2.cust_id);

if (i>1)

     { temp_info2 = globalMap.get(row2.cust_id).next(); }  /* NOTE: this "next" function is theoretical at this point */ 

 

p.s. i am wanting to avoid using a tJoin or a join in a tMap component; i know that is a possible solution, but doing the way described is important for what i am wanting to do.

Labels (2)
3 Replies
Anonymous
Not applicable
Author

If you want to hold multiple values per key, you can use a Java collection for this. An ArrayList would work and would be pretty easy to implement. But the next thing to think about is how do you know which value to use? For example, if you have the following data...

 

Key - Value
A     - 1

A     - 2

A     - 2

A     - 3

 

Assuming these values came in in a row, how would you link your value to the row? It might be that you just need to adjust your key (a composite key to make it unique?) rather than implementing a more complicated solution. 

Jesperrekuh
Specialist

Why not use tAggregate?
Anonymous
Not applicable
Author

Thanks all for the suggestions.   I was able to come up with what I need by doing the following. I run the file thru
a tSetGlobalVar.   Each customer_id is used as a global map variable.   For the value of that key, i use a ternary
operator which checks to see if that key is already defined in the environment by using a globalMap.get.  If it is
not, then I set the value to the customer_info.  Otherwise, i set the value to the customer_info field concatenated
with a "#" concatenated with the results of the globalMap.get for that customer_id.   Although a given repeating
customer_id is overwritten with each pass thru the tSetGlobalVar, its prior value(s) plus the new one is captured
and reassigned to that given key, so no values are lost.  Downstream, i can then split the resultant string for a
given globally mapped customer_id into a string array using the embedded #(s).  Once i have the string array, i can
then perform the count and next functions as needed.    Here is what the value definition looks like  :

(String)globalMap.get((String) row1.cust_id)) == null   ?   row1.customer_info + "#"    
row1.customer_info + "#" ((String)globalMap.get((String) row1.customer_id))