Cache Queries
This is a legacy Apache Ignite documentation
The new documentation is hosted here: https://ignite.apache.org/docs/latest/
Overview
Ignite supports a very elegant query API with support for Predicate-based Scan Queries, SQL Queries (ANSI-99 compliant), and Text Queries. For SQL queries, Ignite supports in-memory indexing, so data lookups are extremely fast.
Ignite also provides support for custom indexing via the IndexingSpi
and SpiQuery
classes.
Main Abstractions
IgniteCache
has several query methods, all of which receive some subclass of the Query
class and return QueryCursor
.
##Query
Query
abstract class represents an abstract paginated query to be executed on the distributed cache. You can set the page size for the returned cursor via the Query.setPageSize(...)
method (default is 1024
).
QueryCursor
QueryCursor
represents the query result set and allows for transparent page-by-page iteration. Whenever a user starts iterating over the last page, it will automatically request the next page in the background. For cases when pagination is not needed, you can use the QueryCursor.getAll()
method which will fetch the whole query result and store it in a collection.
Closing Cursors
Cursors will close automatically if you call the
QueryCursor.getAll()
method. If you are iterating over the cursor in a for loop or explicitly gettingIterator
, you mustclose()
the cursor explicitly or useAutoCloseable
syntax.
Scan Queries
Scan queries allow for querying cache in distributed form based on some user defined predicate.
IgniteCache<Long, Person> cache = ignite.cache("mycache");
// Find only persons earning more than 1,000.
try (QueryCursor<Cache.Entry<Long, Person>> cursor =
cache.query(new ScanQuery<Long, Person>((k, p) -> p.getSalary() > 1000))) {
for (Cache.Entry<Long, Person> entry : cursor)
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
IgniteCache<Long, Person> cache = ignite.cache("mycache");
// Find only persons earning more than 1,000.
IgniteBiPredicate<Long, Person> filter = new IgniteBiPredicate<Long, Person>() {
@Override public boolean apply(Long key, Person p) {
return p.getSalary() > 1000;
}
};
try (QueryCursor<Cache.Entry<Long, Person>> cursor = cache.query(new ScanQuery<>(filter))) {
for (Cache.Entry<Long, Person> entry : cursor)
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Scan queries also support optional transformer closure which lets you convert the entry on the server node before sending it to the client. This is useful, for example, when you want to fetch only several fields out of large object and want to minimize the network traffic. The example below shows how to fetch only keys and to not send value objects at all.
IgniteCache<Long, Person> cache = ignite.cache("mycache");
// Get only keys for persons earning more than 1,000.
List<Long> keys = cache.query(new ScanQuery<Long, Person>(
(k, p) -> p.getSalary() > 1000), // Remote filter.
Cache.Entry::getKey // Transformer.
).getAll();
IgniteCache<Long, Person> cache = ignite.cache("mycache");
// Get only keys for persons earning more than 1,000.
List<Long> keys = cache.query(new ScanQuery<>(
// Remote filter.
new IgniteBiPredicate<Long, Person>() {
@Override public boolean apply(Long k, Person p) {
return p.getSalary() > 1000;
}
}),
// Transformer.
new IgniteClosure<Cache.Entry<Long, Person>, Long>() {
@Override public Long apply(Cache.Entry<Long, Person> e) {
return e.getKey();
}
}
).getAll();
SQL Queries
Ignite SQL queries are covered in the SQL documentation section.
Text Queries
Ignite also supports text-based queries based on Lucene indexing.
IgniteCache<Long, Person> cache = ignite.cache("mycache");
// Query for all people with "Master Degree" in their resumes.
TextQuery txt = new TextQuery(Person.class, "Master Degree");
try (QueryCursor<Entry<Long, Person>> masters = cache.query(txt)) {
for (Entry<Long, Person> e : cursor)
System.out.println(e.getValue().toString());
}
Query Configuration by Annotations
Indexes can be configured from code by using @QuerySqlField
annotations. To tell Ignite which types should be indexed, key-value pairs can be passed into the CacheConfiguration.setIndexedTypes(MyKey.class, MyValue.class)
method. Note that this method accepts only pairs of types, one for key class and another for value class.
public class Person implements Serializable {
/** Person ID (indexed). */
@QuerySqlField(index = true)
private long id;
/** Organization ID (indexed). */
@QuerySqlField(index = true)
private long orgId;
/** First name (not-indexed). */
@QuerySqlField
private String firstName;
/** Last name (not indexed). */
@QuerySqlField
private String lastName;
/** Resume text (create LUCENE-based TEXT index for this field). */
@QueryTextField
private String resume;
/** Salary (indexed). */
@QuerySqlField(index = true)
private double salary;
...
}
Query Configuration using QueryEntity
Indexes and fields can also be configured with org.apache.ignite.cache.QueryEntity
which is convenient for XML configuration with Spring. Please refer to javadoc for details. It is equivalent to using @QuerySqlField
annotation because class annotations are converted to query entities internally.
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="mycache"/>
<!-- Configure query entities -->
<property name="queryEntities">
<list>
<bean class="org.apache.ignite.cache.QueryEntity">
<property name="keyType" value="java.lang.Long"/>
<property name="valueType" value="org.apache.ignite.examples.Person"/>
<property name="fields">
<map>
<entry key="id" value="java.lang.Long"/>
<entry key="orgId" value="java.lang.Long"/>
<entry key="firstName" value="java.lang.String"/>
<entry key="lastName" value="java.lang.String"/>
<entry key="resume" value="java.lang.String"/>
<entry key="salary" value="java.lang.Double"/>
</map>
</property>
<property name="indexes">
<list>
<bean class="org.apache.ignite.cache.QueryIndex">
<constructor-arg value="id"/>
</bean>
<bean class="org.apache.ignite.cache.QueryIndex">
<constructor-arg value="orgId"/>
</bean>
<bean class="org.apache.ignite.cache.QueryIndex">
<constructor-arg value="salary"/>
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>();
...
cacheCfg.setName("mycache");
// Setting up query entity.
QueryEntity queryEntity = new QueryEntity();
queryEntity.setKeyType(Long.class.getName());
queryEntity.setValueType(Person.class.getName());
// Listing query fields.
LinkedHashMap<String, String> fields = new LinkedHashMap();
fields.put("id", Long.class.getName());
fields.put("orgId", Long.class.getName());
fields.put("firstName", String.class.getName());
fields.put("lastName", String.class.getName());
fields.put("resume", String.class.getName());
fields.put("salary", Double.class.getName());
queryEntity.setFields(fields);
// Listing indexes.
Collection<QueryIndex> indexes = new ArrayList<>(3);
indexes.add(new QueryIndex("id"));
indexes.add(new QueryIndex("orgId"));
indexes.add(new QueryIndex("salary"));
queryEntity.setIndexes(indexes);
...
cacheCfg.setQueryEntities(Arrays.asList(queryEntity));
...
Updated over 3 years ago