Anju
Anju

Reputation: 641

Gridgain: Query an Object which has a List of Items

In my Java code I have an object Person with the following properties:

class Person{
    int id;
    List<Integer> values;
}

I wrote the following query to get the sum and average of each value of a person.

GridCacheQuery<Map.Entry<GridCacheAffinityKey<Integer>, Person>> qry=cache.queries().createSqlQuery(Person.class,"values.get(1) > ? and values.get(2) <= ?");
Iterable<Entry<GridCacheAffinityKey<Long>, Subscriber>> cols = qry.execute(1,10).get();

I know this is not the correct way to query, but I'm not able to identify the correct way to do it.

I'm getting the following errors:

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT ""partitioned"".PERSON._KEY,""partitioned"".PERSON._VAL FROM ""partitioned"".PERSON WHERE VALUES.GET([*]1) > ? AND VALUES.GET(2) <= ? ";  
SQL statement: SELECT "partitioned".Person._key, "partitioned".Person._val FROM "partitioned".Person WHERE values.get(1) > ? and values.get(2) <= ? [42000-175]

Upvotes: 1

Views: 165

Answers (2)

Aaron L
Aaron L

Reputation: 195

I think predicates can serve your purpopse as well, but not sure if it is faster or slower than the way suggested by Alexey.

Upvotes: 1

Alexey
Alexey

Reputation: 406

GridGain does not support code snippets embedded into SQL queries. If you want to access these particular elements of your list in query, you can create accessor methods (aliases) for these elements like this:

@GridCacheQuerySqlField
public int first() {
    return values.get(1);
}

@GridCacheQuerySqlField
public int second() {
    return values.get(2);
}

and use them directly in your query: first > ? and second <= ?

Upvotes: 1

Related Questions