Using Scan Queries
Overview
IgniteCache
has several query methods, all of which receive a subclass of the Query
class and return a QueryCursor
.
A Query
represents an abstract paginated query to be executed on a cache.
The page size is configurable via the Query.setPageSize(…)
method (default is 1024).
QueryCursor
represents the query result set and allows for transparent page-by-page iteration.
When a user starts iterating over the last page, QueryCursor
automatically requests the next page in the background.
For cases when pagination is not needed, you can use the QueryCursor.getAll()
method, which fetches the entries and stores them in a collection.
Executing Scan Queries
A scan query is a simple search query used to retrieve data from a cache in a distributed manner. When executed without parameters, a scan query returns all entries from the cache.
IgniteCache<Integer, Person> cache = ignite.getOrCreateCache("myCache");
QueryCursor<Cache.Entry<Integer, Person>> cursor = cache.query(new ScanQuery<>());
var cursor = cache.Query(new ScanQuery<int, Person>());
Cache<int64_t, Person> cache = ignite.GetOrCreateCache<int64_t, ignite::Person>("personCache");
QueryCursor<int64_t, Person> cursor = cache.Query(ScanQuery());
Scan queries return entries that match a predicate, if specified. The predicate is applied on the remote nodes.
IgniteCache<Integer, Person> cache = ignite.getOrCreateCache("myCache");
// Find the persons who earn more than 1,000.
IgniteBiPredicate<Integer, Person> filter = (key, p) -> p.getSalary() > 1000;
try (QueryCursor<Cache.Entry<Integer, Person>> qryCursor = cache.query(new ScanQuery<>(filter))) {
qryCursor.forEach(
entry -> System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()));
}
class SalaryFilter : ICacheEntryFilter<int, Person>
{
public bool Invoke(ICacheEntry<int, Person> entry)
{
return entry.Value.Salary > 1000;
}
}
public static void ScanQueryFilterDemo()
{
var ignite = Ignition.Start();
var cache = ignite.GetOrCreateCache<int, Person>("person_cache");
cache.Put(1, new Person {Name = "person1", Salary = 1001});
cache.Put(2, new Person {Name = "person2", Salary = 999});
using (var cursor = cache.Query(new ScanQuery<int, Person>(new SalaryFilter())))
{
foreach (var entry in cursor)
{
Console.WriteLine("Key = " + entry.Key + ", Value = " + entry.Value);
}
}
}
This API is not presently available for C++.
Scan queries also support an optional transformer closure which lets you convert the entry on the server node before sending it back. This is useful, for example, when you want to fetch only several fields of a large object and want to minimize the network traffic. The example below shows how to fetch only the keys without sending the values.
IgniteCache<Integer, Person> cache = ignite.getOrCreateCache("myCache");
// Get only keys for persons earning more than 1,000.
List<Integer> keys = cache.query(new ScanQuery<>(
// Remote filter
(IgniteBiPredicate<Integer, Person>) (k, p) -> p.getSalary() > 1000),
// Transformer
(IgniteClosure<Cache.Entry<Integer, Person>, Integer>) Cache.Entry::getKey).getAll();
This API is not presently available for C#/.NET.
This API is not presently available for C++.
Local Scan Query
By default, a scan query is distributed to all nodes. However, you can execute the query locally, in which case the query runs against the data stored on the local node (i.e. the node where the query is executed).
QueryCursor<Cache.Entry<Integer, Person>> cursor = cache
.query(new ScanQuery<Integer, Person>().setLocal(true));
var query = new ScanQuery<int, Person> {Local = true};
var cursor = cache.Query(query);
ScanQuery sq;
sq.SetLocal(true);
QueryCursor<int64_t, Person> cursor = cache.Query(sq);
Related Topics
© 2024 GridGain Systems, Inc. All Rights Reserved. Privacy Policy | Legal Notices. GridGain® is a registered trademark of GridGain Systems, Inc.
Apache, Apache Ignite, the Apache feather and the Apache Ignite logo are either registered trademarks or trademarks of The Apache Software Foundation.