Carlos Bribiescas
Carlos Bribiescas

Reputation: 4407

Operate on Cached data per node

I'm able to do affinity computer-data collocation with apache ignite. In the following two examples, it works as expected.

    // Works on all nodes
    IgniteUtil.getIgnite().compute().broadcast(() -> {
        System.out.println("Should happen on all nodes");
        cache.get(key).forEach(x -> {
            System.out.println(x);
        });
    });

    // Works on just the one node
    IgniteUtil.getIgnite().compute().affinityRun(IgniteUtil.CACHE_NAME, key , () -> {
        System.out.println("Should only happen on one node");
        cache.get(key).forEach(x -> System.out.println(x));
    });

However, I want to run a lambda against all the nodes data. So for example, say I had cached For every person, all of their orders from Amazon. I want to know what the total dollar amount is for everyone's orders.

I'm probably just missing an example, but according to the docs I don't see how to do this. In the examples I've seen I have to specify the keys I want to compute with. In this example, I just want to be able to do some lambda on all of the nodes, with each node only operating on its own share of the data.

I've tried doing this

    IgniteUtil.getIgnite().compute().affinityRun(IgniteUtil.CACHE_NAME, key , () -> {
        System.out.println("Should only happen once per node");
        List<Integer> count = new ArrayList<Integer>();
        System.out.println("Size: " + Sets.newHashSet(cache.iterator()).size());
        cache.iterator().forEachRemaining(x -> {count.add(count.size());});
        System.out.println("Calculated Size: " + count.size());
        System.out.println("Values: " );
        cache.get(key).forEach(x -> System.out.print(x));
        System.out.println();
    });

and it does only execute on the node that has the key, however, the cache size is the full cache size, not just the values that are local.

Any suggestions?

Upvotes: 0

Views: 168

Answers (1)

Valentin Kulichenko
Valentin Kulichenko

Reputation: 8390

You can broadcast a closure as in your first example and use IgniteCache.localEntries() method to iterate through the local data.

Upvotes: 1

Related Questions