public interface CacheQuery<T>
SQL query allows to execute distributed cache
queries using standard SQL syntax. All values participating in where clauses
or joins must be annotated with QuerySqlField annotation.
select clause is ignored as query result contains full objects.
This type of query replaces full objects with individual fields. Note that selected fields
must be annotated with QuerySqlField annotation.
Note that cache name is case sensitive and has to always be specified in double quotes. Here is an example of cross cache query (note that 'replicated' and 'partitioned' are cache names for replicated and partitioned caches accordingly):
CacheQuery<Map.Entry<Integer, FactPurchase>> storePurchases = cache.queries().createSqlQuery(
Purchase.class,
"from \"replicated\".Store, \"partitioned\".Purchase where Store.id=Purchase.storeId and Store.id=?");
QuerySqlFunction. Classes containing these methods must be registered in
CacheConfiguration.setSqlFunctionClasses(Class[]).
QueryTextField
annotation.
Group by and sort by statements are applied separately
on each node, so result set will likely be incorrectly grouped or sorted
after results from multiple remote nodes are grouped together.
sum, max, avg, etc.
are also applied on each node. Therefore you will get several results
containing aggregated values, one for each node.
CacheMode.REPLICATED cache. Refer to
AffinityKey javadoc for more information about colocation.
'Employee' and 'Organization'
classes defined as follows:
public class Organization {
// Indexed field.
@QuerySqlField(index = true)
private long id;
// Indexed field.
@QuerySqlField(index = true)
private String name;
...
}
public class Person {
// Indexed field.
@QuerySqlField(index = true)
private long id;
// Indexed field (Organization ID, used as a foreign key).
@QuerySqlField(index = true)
private long orgId;
// Without SQL field annotation, this field cannot be used in queries.
private String name;
// Not indexed field.
@QuerySqlField
private double salary;
// Index for text search.
@QueryTextField
private String resume;
...
}
Then you can create and execute queries that check various salary ranges like so:
Cache<Long, Person> cache = G.grid().cache();
...
// Create query which selects salaries based on range for all employees
// that work for a certain company.
CacheQuery<Map.Entry<Long, Person>> qry = cache.queries().createSqlQuery(Person.class,
"from Person, Organization where Person.orgId = Organization.id " +
"and Organization.name = ? and Person.salary > ? and Person.salary <= ?");
// Query all nodes to find all cached Ignite employees
// with salaries less than 1000.
qry.execute("Ignition", 0, 1000);
// Query only remote nodes to find all remotely cached Ignite employees
// with salaries greater than 1000 and less than 2000.
qry.projection(grid.remoteProjection()).execute("Ignition", 1000, 2000);
Here is a possible query that will use Lucene text search to scan all resumes to
check if employees have Master degree:
CacheQuery<Map.Entry<Long, Person>> mastersQry =
cache.queries().createFullTextQuery(Person.class, "Master");
// Query all cache nodes.
mastersQry.execute();
private class MapPoint implements Serializable {
// Geospatial index.
@QuerySqlField(index = true)
private org.locationtech.jts.geom.Point location;
// Not indexed field.
@QuerySqlField
private String name;
public MapPoint(org.locationtech.jts.geom.Point location, String name) {
this.location = location;
this.name = name;
}
}
Example of spatial query on the geo-indexed field from above:
org.locationtech.jts.geom.GeometryFactory factory = new org.locationtech.jts.geom.GeometryFactory();
org.locationtech.jts.geom.Polygon square = factory.createPolygon(new Coordinate[] {
new org.locationtech.jts.geom.Coordinate(0, 0),
new org.locationtech.jts.geom.Coordinate(0, 100),
new org.locationtech.jts.geom.Coordinate(100, 100),
new org.locationtech.jts.geom.Coordinate(100, 0),
new org.locationtech.jts.geom.Coordinate(0, 0)
});
Map.Entry records = cache.queries().createSqlQuery(MapPoint.class, "select * from MapPoint where location && ?")
.queryArguments(square)
.execute()
.get();
| Modifier and Type | Method and Description |
|---|---|
CacheQuery<T> |
enableDedup(boolean dedup)
Sets whether or not to deduplicate query result set.
|
<R> CacheQueryFuture<R> |
execute(IgniteReducer<T,R> rmtReducer,
Object... args)
Executes the query the same way as
execute(Object...) method but reduces result remotely. |
CacheQueryFuture<T> |
execute(Object... args)
Executes the query and returns the query future.
|
GridCloseableIterator |
executeScanQuery() |
CacheQuery<T> |
includeBackups(boolean incBackups)
Sets whether or not to include backup entries into query result.
|
QueryMetrics |
metrics()
Gets metrics for this query.
|
CacheQuery<T> |
pageSize(int pageSize)
Sets result page size.
|
CacheQuery<T> |
projection(ClusterGroup prj)
Sets optional grid projection to execute this query on.
|
void |
resetMetrics()
Resets metrics for this query.
|
CacheQuery<T> |
timeout(long timeout)
Sets query timeout.
|
CacheQuery<T> pageSize(int pageSize)
Query.DFLT_PAGE_SIZE will be used.
Results are returned from queried nodes one page at a tme.pageSize - Page size.this query instance for chaining.CacheQuery<T> timeout(long timeout)
0 means there is no timeout (this
is a default value).timeout - Query timeout.this query instance for chaining.CacheQuery<T> includeBackups(boolean incBackups)
false by default.incBackups - Query includeBackups flag.this query instance for chaining.CacheQuery<T> enableDedup(boolean dedup)
true
then query result will not contain some key more than once even if several nodes
returned entries with the same keys. Default value is false.dedup - Query enableDedup flag.this query instance for chaining.CacheQuery<T> projection(ClusterGroup prj)
prj - Projection.this query instance for chaining.CacheQueryFuture<T> execute(@Nullable Object... args)
'get(..)' methods on the returned future.
Note that if the passed in grid projection is a local node, then query will be executed locally without distribution to other nodes.
Also note that query state cannot be changed (clause, timeout etc.), except arguments, if this method was called at least once.
args - Optional arguments.<R> CacheQueryFuture<R> execute(IgniteReducer<T,R> rmtReducer, @Nullable Object... args)
execute(Object...) method but reduces result remotely.rmtReducer - Remote reducer.args - Optional arguments.QueryMetrics metrics()
void resetMetrics()
GridCloseableIterator executeScanQuery() throws IgniteCheckedException
IgniteCheckedException
Follow @ApacheIgnite
Ignite Database and Caching Platform : ver. 2.7.5 Release Date : June 4 2019