我正在尝试从Apache Ignite缓存中获取所有项目。
目前,我可以使用来获得单个物品
ClientCache<Integer, BinaryObject> cache = igniteClient.cache("myCache").withKeepBinary();
BinaryObject temp = cache.get(1);
要获取所有密钥,我已经尝试了以下操作:
try ( QueryCursor<Entry<Integer,BinaryObject>> cursor = cache.query(new ScanQuery<Integer, BinaryObject>(null))) {
for (Object p : cursor)
System.out.println(p.toString());
}
这将返回内部的org.apache.ignite.internal.client.thin.ClientCacheEntry
列表,而我无法调用getValue
。
如何获取此缓存的所有项目?
答案 0 :(得分:1)
通过使用迭代器,您可以从缓存中获取所有值和键。下面是从缓存中检索所有值的示例代码。
Iterator<Entry<Integer, BinaryObject>> itr = cache.iterator();
while(itr.hasNext()) {
BinaryObject object = itr.next().getValue();
System.out.println(object);
}
答案 1 :(得分:0)
以下内容可以帮助您遍历缓存中的所有记录。
import javax.cache.Cache.Entry;
import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.binary.BinaryObject;
public class App5BinaryObject {
public static void main(String[] args) {
Ignition.setClientMode(true);
try (Ignite client = Ignition
.start("/Users/amritharajherle/git_new/ignite-learning-by-examples/complete/cfg/ignite-config.xml")) {
IgniteCache<BinaryObject, BinaryObject> cities = client.cache("City").withKeepBinary();
int count = 0;
for (Entry<BinaryObject, BinaryObject> entry : cities) {
count++;
BinaryObject key = entry.getKey();
BinaryObject value = entry.getValue();
System.out.println("CountyCode=" + key.field("COUNTRYCODE") + ", DISTRICT = " + value.field("DISTRICT")
+ ", POPULATION = " + value.field("POPULATION") + ", NAME = " + value.field("NAME"));
}
System.out.println("total cities count = " + count);
}
}
}