Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to query by key instead of value in Hazelcast (using Predicates)?

Tags:

java

hazelcast

In Hazelcast, is it possible to query an IMap based on attributes of a key instead of the values? All the Hazelcast examples show querying by value. E.g., for a map of employees with keys that are strings:

IMap<String, Employee> employees;

The typical search predicates then search based on employee attributes (name, salary, etc). But my case uses more complex keys, such as:

IMap<DataAttributes, DataValue> myData;

So if DataAttributes has fields such as:

 class DataAttributes {
     String theDescription;
     Date   theStartTime;
     public String getDescription() { return theDescription; }
     // etc....
 }

I want to write a predicate that can query by the keys, to return an appropriate DataValue object. This does not work:

Predicate pred = Predicates.equal("description", "myDescription");
myData.keySet(pred);  // Throws IllegalArgumentException: "There is no suitable accessor for..."

I could roll-my-own as suggested in this answer, but I'd rather use an out-of-the-box solution if I can.

It doesn't matter if I wind up using the Criteria API, or the Distributed SQL Query API. Any working query would be great. Bonus points for a solution that works on nested attributes (i.e.: DataAttributes theStartTime.getYear()).

like image 573
Ogre Psalm33 Avatar asked May 27 '15 15:05

Ogre Psalm33


1 Answers

It is possible using PredicateBuilder (com.hazelcast.query.PredicateBuilder). The PredicateBuilder paradigm allows you to query based on keys, like so:

EntryObject eo = new PredicateBuilder().getEntryObject();
Predicate fredWithStartTimeThisYear = eo.key().get("Description").equal("Fred")
  .and(eo.key().get("theStartTime.Year").equal(2015));

Note that you can refer to class members by accessor method ("getter") or field name, as you can see in the above example code. I found this information in the "Mastering Hazelcast" online book, available at hazelcast.org (but you have to fill out a registration form to gain access to it).

like image 159
Ogre Psalm33 Avatar answered Nov 14 '22 22:11

Ogre Psalm33