manu
manu

Reputation: 169

How to select multiple keys in Hazelcast's IMap?

I'm trying to fetch values by IMap keys.

My test code:

1) Model Class

public class ModelTest implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String id1;
    private String id2;
    private String id3;

    public ModelTest() {
        super();
    }


2) Main method

        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        IMap<ModelTest, String> map = hazelcastInstance.getMap("map-name");
        ModelTest t1 = new ModelTest();
        t1.setId1("a1");
        t1.setId2("a2");
        t1.setId3("a3");

        ModelTest t2 = new ModelTest();
        t2.setId1("b1");
        t2.setId2("b2");
        t2.setId3("b3");
       
       //Loading into cache
        map.put(t1, "rakesh");
        map.put(t2, "ramkumar");

I'm able to fetch values by passing a single key at a time

Example:

SqlPredicate predicate = new SqlPredicate("__key.id1='a1'");

Is it possible to select more than one key at a time?

SqlPredicate predicate = new SqlPredicate("__key.id1='a1' and __key.id2='b2'");

Something like the above code?

I don't want to use Predicate because it's not dynamic.

Predicate predicate = e.is( "active" ).and( e.get( "age" ).lessThan( 30 ) );

Upvotes: 0

Views: 888

Answers (1)

Rafał Leszko
Rafał Leszko

Reputation: 5531

You can select more than one key with one query, just use or keyword.

In your case, the following predicate should return both entries.

SqlPredicate predicate = new SqlPredicate("__key.id1='a1' or __key.id2='b2'");

Upvotes: 1

Related Questions