Search code examples
regexhazelcast

regex on key hazelcast


I am storing data using map data structure in Hazelcast. This will get stored like below:

key1 valueforKey1
key2 valueforkey2
key11 valueforKey11
key22 valueforKey22
key123 valueforKey123
key33 valueforKey33

Now I want to fetch data on the basis of key from hazelcast, e.g I want all records whose key starts with key1*.

How can I do this in hazelcast?

Note: At value I am storing some string.


Solution

  • Just by using the Hazelcast Criteria API http://docs.hazelcast.org/docs/3.4/manual/html-single/hazelcast-documentation.html#criteria-api

    Either with like or regex directives.

    edit:

    Here's a complete example:

    public class Test {
      public static void main(String[] args) {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        IMap<String, String> map = hazelcastInstance.getMap("map-name");
    
        map.put("key1", "value-1");
        map.put("key21", "value-21");
        map.put("key11", "value-key11");
    
        SqlPredicate predicate = new SqlPredicate("__key REGEX '^key1.*$'");
        System.out.println(map.values(predicate));
      }
    }