Querying Nested Objects

574 views
Skip to first unread message

Wallace

unread,
Jul 15, 2015, 4:32:25 AM7/15/15
to haze...@googlegroups.com
Hi,

I'm trying to figure out a more performant way of querying objects using Predicates in Hazelcast as I'm sure how I'm doing it isn't the most efficient.

Say, I have a Person object that has, FirstName (string), LastName (string), Address (Object) (stored in the Person Map).  Address is another object (stored in the Address Map) and this has elements City (string) and PostCode (string).

I want to retruen all Person objects with Address.City = "New York".

How my querying currently works is I first of all get all Person objects.  As I have no predicate for the Person object I get all Persons stored in the Map returned.  then I iterate of all Person objects, do an "AddressMap".keySet(predicate) where Predicate is "City = 'New York" and then check that returned value equals to my Person.Address.City.  If true, add that Person object to return list, if not continue to the next Person.

So in Pseudo...

Collection<Person> persons = getMap("Person").values();

for each Person {
   Set<String> filteredKeys = getMap("Address").keySet(predicate);  // predicate is City="New York"

   if filteredKey contains  Person.City
      add Person to return set.
}


So my question, is there any way of "nesting" the queries.  This is very slow, especially if I have a large number of Persons (say 2000) as it gets and iterates over them all (no predicate on Person object) and then does the check for City.Address.

I feel like I'm missing something so any help would be appreicated.

Cheers

M. Sancar Koyunlu

unread,
Jul 15, 2015, 5:17:54 AM7/15/15
to haze...@googlegroups.com
Hi Wallace,
Nested queries are possible with Portable objects.

You can  do a query as follows :

Collection<Object> values = map.values(new SqlPredicate("address.city = 'New York' "));

You can take a look at following test class for more examples and usage.

https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/test/java/com/hazelcast/map/query/QueryBasicTest.java#L852

Regards.
Sancar

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at http://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/1f105bb9-84c0-4061-b783-3c544cb2bb69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wallace

unread,
Jul 15, 2015, 6:35:46 AM7/15/15
to haze...@googlegroups.com
Thanks.  So another case for Portable over Java Serialization other than pure performance.

Also, I notice you use Sql Query as opposed to Criteria API.  Is that just a personal preference or do you find Sql query better?

Thanks again.

Alex Rogachevsky

unread,
Aug 26, 2015, 10:23:28 PM8/26/15
to Hazelcast
Portable is the best option for query-heavy Hazelcast usage, but it is not mandatory.
What you need is custom top-level getters e.g. String getDefaultAddressCity(). It can be as complex, as you want. E.g you can return comma-separated unique cities and then use ILikePredicate on that. Those getters can perform any calculations accessing any nested collections and sub-objects.
Reply all
Reply to author
Forward
0 new messages