2
votes

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?

2
Does this answer your question? Apache Ignite: How to get all items from named cachedlu_ko

2 Answers

3
votes

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

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

    }

  }

}