Search in sources :

Example 6 with CoAggregateOperationBuilder

use of com.hazelcast.jet.aggregate.CoAggregateOperationBuilder in project hazelcast by hazelcast.

the class BatchAggregateTest method groupAggregateBuilder_withComplexAggrOp_withOutputFn.

@Test
@SuppressWarnings("ConstantConditions")
public void groupAggregateBuilder_withComplexAggrOp_withOutputFn() {
    // Given
    GroupAggregateFixture fx = new GroupAggregateFixture();
    BatchStageWithKey<Integer, Integer> stage0 = fx.srcStage0.groupingKey(fx.keyFn);
    BatchStageWithKey<Integer, Integer> stage1 = fx.srcStage1().groupingKey(fx.keyFn);
    BatchStageWithKey<Integer, Integer> stage2 = fx.srcStage2().groupingKey(fx.keyFn);
    // When
    GroupAggregateBuilder1<Integer, Integer> b = stage0.aggregateBuilder();
    Tag<Integer> tag0_in = b.tag0();
    Tag<Integer> tag1_in = b.add(stage1);
    Tag<Integer> tag2_in = b.add(stage2);
    CoAggregateOperationBuilder agb = coAggregateOperationBuilder();
    Tag<Long> tag0 = agb.add(tag0_in, SUMMING);
    Tag<Long> tag1 = agb.add(tag1_in, SUMMING);
    Tag<Long> tag2 = agb.add(tag2_in, SUMMING);
    AggregateOperation<Object[], ItemsByTag> aggrOp = agb.build();
    BatchStage<Entry<Integer, Long>> aggregated = b.build(aggrOp).map(e -> {
        ItemsByTag ibt = e.getValue();
        return entry(e.getKey(), ibt.get(tag0) + ibt.get(tag1) + ibt.get(tag2));
    });
    // Then
    aggregated.writeTo(sink);
    execute();
    Map<Integer, Long> expectedMap0 = input.stream().collect(groupingBy(fx.keyFn, fx.collectOp));
    Map<Integer, Long> expectedMap1 = input.stream().map(fx.mapFn1).collect(groupingBy(fx.keyFn, fx.collectOp));
    Map<Integer, Long> expectedMap2 = input.stream().map(fx.mapFn2).collect(groupingBy(fx.keyFn, fx.collectOp));
    assertEquals(streamToString(expectedMap0.entrySet().stream(), e -> FORMAT_FN.apply(entry(e.getKey(), e.getValue() + expectedMap1.get(e.getKey()) + expectedMap2.get(e.getKey())))), streamToString(sinkStreamOfEntry(), FORMAT_FN));
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) AggregateOperations.maxBy(com.hazelcast.jet.aggregate.AggregateOperations.maxBy) AggregateOperations.aggregateOperation2(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation2) QuickTest(com.hazelcast.test.annotation.QuickTest) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) AggregateOperations.coAggregateOperationBuilder(com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) BiFunctionEx(com.hazelcast.function.BiFunctionEx) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) Collectors.summingLong(java.util.stream.Collectors.summingLong) Util.entry(com.hazelcast.jet.Util.entry) Map(java.util.Map) ComparatorEx.comparingInt(com.hazelcast.function.ComparatorEx.comparingInt) Collector(java.util.stream.Collector) AggregateOperations.aggregateOperation3(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation3) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Tuple3(com.hazelcast.jet.datamodel.Tuple3) Before(org.junit.Before) FunctionEx(com.hazelcast.function.FunctionEx) Collections.emptyList(java.util.Collections.emptyList) Tag(com.hazelcast.jet.datamodel.Tag) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Category(org.junit.experimental.categories.Category) ItemsByTag.itemsByTag(com.hazelcast.jet.datamodel.ItemsByTag.itemsByTag) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Assert.assertEquals(org.junit.Assert.assertEquals) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Collectors.summingLong(java.util.stream.Collectors.summingLong) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with CoAggregateOperationBuilder

use of com.hazelcast.jet.aggregate.CoAggregateOperationBuilder in project hazelcast by hazelcast.

the class BatchAggregateTest method aggregateBuilder_withComplexAggrOp.

@Test
public void aggregateBuilder_withComplexAggrOp() {
    // Given
    AggregateBuilderFixture fx = new AggregateBuilderFixture();
    // When
    AggregateBuilder1<Integer> b = batchStageFromInput().aggregateBuilder();
    Tag<Integer> tag0_in = b.tag0();
    Tag<Integer> tag1_in = b.add(fx.stage1);
    Tag<Integer> tag2_in = b.add(fx.stage2);
    CoAggregateOperationBuilder agb = coAggregateOperationBuilder();
    Tag<Long> tag0 = agb.add(tag0_in, SUMMING);
    Tag<Long> tag1 = agb.add(tag1_in, SUMMING);
    Tag<Long> tag2 = agb.add(tag2_in, SUMMING);
    AggregateOperation<Object[], ItemsByTag> aggrOp = agb.build();
    BatchStage<ItemsByTag> aggregated = b.build(aggrOp);
    // Then
    aggregated.writeTo(sink);
    execute();
    long sum0 = input.stream().mapToLong(i -> i).sum();
    assertEquals(singletonList(itemsByTag(tag0, sum0, tag1, FACTOR_1 * sum0, tag2, FACTOR_2 * sum0)), new ArrayList<>(sinkList));
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) AggregateOperations.maxBy(com.hazelcast.jet.aggregate.AggregateOperations.maxBy) AggregateOperations.aggregateOperation2(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation2) QuickTest(com.hazelcast.test.annotation.QuickTest) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) AggregateOperations.coAggregateOperationBuilder(com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) BiFunctionEx(com.hazelcast.function.BiFunctionEx) AggregateOperation(com.hazelcast.jet.aggregate.AggregateOperation) Collectors.summingLong(java.util.stream.Collectors.summingLong) Util.entry(com.hazelcast.jet.Util.entry) Map(java.util.Map) ComparatorEx.comparingInt(com.hazelcast.function.ComparatorEx.comparingInt) Collector(java.util.stream.Collector) AggregateOperations.aggregateOperation3(com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation3) Tuple2(com.hazelcast.jet.datamodel.Tuple2) Tuple3(com.hazelcast.jet.datamodel.Tuple3) Before(org.junit.Before) FunctionEx(com.hazelcast.function.FunctionEx) Collections.emptyList(java.util.Collections.emptyList) Tag(com.hazelcast.jet.datamodel.Tag) AggregateOperations(com.hazelcast.jet.aggregate.AggregateOperations) Test(org.junit.Test) AggregateOperation1(com.hazelcast.jet.aggregate.AggregateOperation1) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Category(org.junit.experimental.categories.Category) ItemsByTag.itemsByTag(com.hazelcast.jet.datamodel.ItemsByTag.itemsByTag) Tuple3.tuple3(com.hazelcast.jet.datamodel.Tuple3.tuple3) List(java.util.List) Tuple2.tuple2(com.hazelcast.jet.datamodel.Tuple2.tuple2) LongAccumulator(com.hazelcast.jet.accumulator.LongAccumulator) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) Entry(java.util.Map.Entry) Assert.assertEquals(org.junit.Assert.assertEquals) ItemsByTag(com.hazelcast.jet.datamodel.ItemsByTag) CoAggregateOperationBuilder(com.hazelcast.jet.aggregate.CoAggregateOperationBuilder) Collectors.summingLong(java.util.stream.Collectors.summingLong) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CoAggregateOperationBuilder (com.hazelcast.jet.aggregate.CoAggregateOperationBuilder)7 Test (org.junit.Test)7 LongAccumulator (com.hazelcast.jet.accumulator.LongAccumulator)6 AggregateOperation1 (com.hazelcast.jet.aggregate.AggregateOperation1)6 AggregateOperations.coAggregateOperationBuilder (com.hazelcast.jet.aggregate.AggregateOperations.coAggregateOperationBuilder)6 ItemsByTag (com.hazelcast.jet.datamodel.ItemsByTag)6 Tag (com.hazelcast.jet.datamodel.Tag)6 Tuple2 (com.hazelcast.jet.datamodel.Tuple2)6 Tuple2.tuple2 (com.hazelcast.jet.datamodel.Tuple2.tuple2)6 Tuple3 (com.hazelcast.jet.datamodel.Tuple3)6 Collections.singletonList (java.util.Collections.singletonList)6 List (java.util.List)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Util.entry (com.hazelcast.jet.Util.entry)5 AggregateOperations (com.hazelcast.jet.aggregate.AggregateOperations)5 AggregateOperations.aggregateOperation2 (com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation2)5 AggregateOperations.aggregateOperation3 (com.hazelcast.jet.aggregate.AggregateOperations.aggregateOperation3)5 Tuple3.tuple3 (com.hazelcast.jet.datamodel.Tuple3.tuple3)5 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5