'Apache Ignite - get all from cache
I'm trying to get all items from a Apache Ignite cache.
Currently I can get an individual item using
ClientCache<Integer, BinaryObject> cache = igniteClient.cache("myCache").withKeepBinary();
BinaryObject temp = cache.get(1);
To get all keys, Ive tried the following:
try(QueryCursor<Entry<Integer,BinaryObject>> cursor = cache.query(new ScanQuery<Integer, BinaryObject>(null))) {
for (Object p : cursor)
System.out.println(p.toString());
}
This returns a list of org.apache.ignite.internal.client.thin.ClientCacheEntry
which is internal, and I cannot call getValue
.
How can I get all items for this cache?
Solution 1:[1]
By using Iterator you can get all values and key from cache. below are the sample code to retrieve all values from cache.
Iterator<Entry<Integer, BinaryObject>> itr = cache.iterator();
while(itr.hasNext()) {
BinaryObject object = itr.next().getValue();
System.out.println(object);
}
Solution 2:[2]
The following may help you to iterate over all the records in the cache.
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);
}
}
}
Solution 3:[3]
Using Ignite Rest API we can fetch certain Number of records[Size]. I strive a lot and finally found API.
http://<Server_IP>:8080/ignite?cmd=qryscanexe&pageSize=10&cacheName=
Add Auth Header As per Ignite Cluster User.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marius Jaraminas |
Solution 2 | |
Solution 3 | Rajeev Rathor |