Six Stars

How To Get Values In HashMap Using globalMap.get()

Hi, my scenario is like:

 

Map<String, HashSet<String>> B = new HashMap<String, HashSet<String>>();

if(!B.containsKey(row5.issue)){
B.put(row5.issue,new HashSet<String>());
}
B.get(row5.issue).add(row5.name);

globalMap.put("BMap", B);

 

 

Now, I have the following scripts in another Subjob:

 

select * from Table

where name in ('"+BMap.values()+"');

 

 

The bold portion has problems, anyone can advise? Thanks very much.

  • Big Data
  • Data Integration
1 ACCEPTED SOLUTION

Accepted Solutions
Ten Stars

Re: How To Get Values In HashMap Using globalMap.get()

You need to "get" BMap before you can act on it.
e.g.
((HashMap)globalMap.get("BMap")).values()

As @TRF noted, the values method does not return a String that you can drop in to your SQL statement. With your current implementation, you'll need to iterate over the Collection it returns and build a String to include in your where clause.

If this is the only use of the HashMap in your project, you may want to consider a different approach.
5 REPLIES
Ten Stars

Re: How To Get Values In HashMap Using globalMap.get()

You can't reference globalMap variables directly. You have to retrieve them, casting them to the appropriate data type. Ex:
(String)globalMap.get("exampleVariable")
Six Stars

Re: How To Get Values In HashMap Using globalMap.get()

Thanks for the reply!

 

This is what my scripts look like:

where name in ('"+((String)globalMap.get("BMap.values()"))+"');

 

But that can't go through. Thanks again for your reply!

Six Stars

Re: How To Get Values In HashMap Using globalMap.get()

Can anyone please advise? Appreciate your help!!

 

Regards

Nine Stars TRF
Nine Stars

Re: How To Get Values In HashMap Using globalMap.get()

You should build the "inString" in the 1st part instead of store indivual values of row5.name in a hashmap or you need to extract values one by one and concatenate them with ' and , to build a valid SQL string before to use it.

TRF
Ten Stars

Re: How To Get Values In HashMap Using globalMap.get()

You need to "get" BMap before you can act on it.
e.g.
((HashMap)globalMap.get("BMap")).values()

As @TRF noted, the values method does not return a String that you can drop in to your SQL statement. With your current implementation, you'll need to iterate over the Collection it returns and build a String to include in your where clause.

If this is the only use of the HashMap in your project, you may want to consider a different approach.