2017-10-14 273 views
2

'setInitialQuery'的用途是什麼?Apache Ignite - 連續查詢

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)); 
} 

上面的代碼沒有設置初始查詢。 試圖瞭解何時會使用'setInitialQuery'。

回答

1

在開始偵聽更新之前,初始查詢允許將光標放在已存在緩存中的數據上。這確實是可選的。