@Override public <SuppliedValue, Result> Result aggregate( Supplier<K, V, SuppliedValue> supplier, Aggregation<K, SuppliedValue, Result> aggregation, JobTracker jobTracker) { try { Preconditions.isNotNull(jobTracker, "jobTracker"); KeyValueSource<K, V> keyValueSource = KeyValueSource.fromMultiMap(this); Job<K, V> job = jobTracker.newJob(keyValueSource); Mapper mapper = aggregation.getMapper(supplier); CombinerFactory combinerFactory = aggregation.getCombinerFactory(); ReducerFactory reducerFactory = aggregation.getReducerFactory(); Collator collator = aggregation.getCollator(); MappingJob mappingJob = job.mapper(mapper); ReducingSubmittableJob reducingJob; if (combinerFactory != null) { reducingJob = mappingJob.combiner(combinerFactory).reducer(reducerFactory); } else { reducingJob = mappingJob.reducer(reducerFactory); } ICompletableFuture<Result> future = reducingJob.submit(collator); return future.get(); } catch (Exception e) { throw new HazelcastException(e); } }
@Test(timeout = 60000) public void testMapReduceWithList() throws Exception { Config config = buildConfig(); HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance(config); HazelcastInstance h2 = hazelcastFactory.newHazelcastInstance(config); HazelcastInstance h3 = hazelcastFactory.newHazelcastInstance(config); assertClusterSizeEventually(3, h1); assertClusterSizeEventually(3, h2); assertClusterSizeEventually(3, h3); HazelcastInstance client = hazelcastFactory.newHazelcastClient(); int expectedResult = 0; IList<Integer> list = h1.getList("default"); for (int o = 0; o < 100; o++) { list.add(o); expectedResult += o; } JobTracker jobTracker = client.getJobTracker("default"); Job<String, Integer> job = jobTracker.newJob(KeyValueSource.fromList(list)); ICompletableFuture<Map<String, Integer>> ICompletableFuture = job.chunkSize(10) .mapper(new ListSetMapReduceTest.ListSetMapper()) .combiner(new ListSetMapReduceTest.ListSetCombinerFactory()) .reducer(new ListSetMapReduceTest.ListSetReducerFactory()) .submit(); Map<String, Integer> result = ICompletableFuture.get(); assertEquals(1, result.size()); int count = 0; for (Map.Entry<String, Integer> entry : result.entrySet()) { assertEquals(list.getName(), entry.getKey()); assertEquals(expectedResult, (int) entry.getValue()); } }