Exploring the apply API

The compute grid's apply method receives a job argument and passes it to the closure at the time of execution. In the call or run methods, you can't pass any job argument. The apply method accepts two arguments: IgniteClosure<R,T> and argument<T>. In this section, we are going to create a closure to calculate the sum of a list of numbers. We'll pass a closure and a list of integers to the apply method. The following are the steps:

  1. Create a new class, IgniteApply, and add the following lines:
      IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
try (Ignite ignite = Ignition.start(cfg)) {
IgniteCompute compute = ignite.compute();
List<Integer> numbersToAdd = Arrays.asList(2, 3, 5, 6, 7);
Integer result = compute.apply(new IgniteClosure<List<Integer>,
Integer>() {
@Override
public Integer apply(List<Integer> numbers) {
int sum = numbers.stream().mapToInt(i -> i.intValue()).sum();
return sum;
}
}, numbersToAdd);

System.out.println(String.format("The sum of %s is %s",
numbersToAdd, result));
}
  1. We created an inline closure to loop through the list of integers and calculate the sum. The compute.apply method passes the numbersToAdd to the closure's apply method during the execution. Here is the result of the distributed processing:

The difference between the apply and run/call method is  in the call/run APIs, you cannot pass any job argument at execution time, but apply receives a job argument.

Next, we'll examine the execute and affinity APIs in the MapReduce/ForkJoin section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.233