Joe
Joe

Reputation: 13131

Apache Ignite - Continuous Query

What is the purpose of 'setInitialQuery'?

IgniteCache<Integer, String> cache = ignite.cache("mycache");

// Create new continuous query.
ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();

// Optional initial query to select all keys greater than 10.
qry.setInitialQuery(new ScanQuery<Integer, String>((k, v) -> k > 10)):

// Callback that is called locally when update notifications are received.
qry.setLocalListener((evts) -> 
    evts.forEach(e -> System.out.println("key=" + e.getKey() + ", val=" + e.getValue())));

// This filter will be evaluated remotely on all nodes.
// Entry that pass this filter will be sent to the caller.
qry.setRemoteFilter(e -> e.getKey() > 10);

// Execute query.
try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
    // Iterate through existing data stored in cache.
    for (Cache.Entry<Integer, String> e : cur)
        System.out.println("key=" + e.getKey() + ", val=" + e.getValue());

    // Add a few more keys and watch a few more query notifications.
    for (int i = 5; i < 15; i++)
        cache.put(i, Integer.toString(i));
}

Code above works without setting initial query. Trying to understand when would use 'setInitialQuery'.

Upvotes: 2

Views: 459

Answers (1)

Valentin Kulichenko
Valentin Kulichenko

Reputation: 8390

Initial query allows to get the cursor over data that is already in cache, before starting listening for updates. It's indeed optional.

Upvotes: 2

Related Questions