Search in sources :

Example 1 with StreamGroupedReduce

use of org.apache.flink.streaming.api.operators.StreamGroupedReduce in project flink by apache.

the class AggregationFunctionTest method pojoMinMaxByTest.

@Test
public void pojoMinMaxByTest() throws Exception {
    // Pojos are grouped on field 0, aggregated on field 1
    // preparing expected outputs
    List<MyPojo3> maxByFirstExpected = ImmutableList.of(new MyPojo3(0, 0), new MyPojo3(1, 1), new MyPojo3(2, 2), new MyPojo3(2, 2), new MyPojo3(2, 2), new MyPojo3(2, 2), new MyPojo3(2, 2), new MyPojo3(2, 2), new MyPojo3(2, 2));
    List<MyPojo3> maxByLastExpected = ImmutableList.of(new MyPojo3(0, 0), new MyPojo3(1, 1), new MyPojo3(2, 2), new MyPojo3(2, 2), new MyPojo3(2, 2), new MyPojo3(2, 5), new MyPojo3(2, 5), new MyPojo3(2, 5), new MyPojo3(2, 8));
    List<MyPojo3> minByFirstExpected = ImmutableList.of(new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0));
    List<MyPojo3> minByLastExpected = ImmutableList.of(new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 0), new MyPojo3(0, 3), new MyPojo3(0, 3), new MyPojo3(0, 3), new MyPojo3(0, 6), new MyPojo3(0, 6), new MyPojo3(0, 6));
    // some necessary boiler plate
    TypeInformation<MyPojo3> typeInfo = TypeExtractor.getForObject(new MyPojo3(0, 0));
    ExecutionConfig config = new ExecutionConfig();
    KeySelector<MyPojo3, Tuple> keySelector = KeySelectorUtil.getSelectorForKeys(new Keys.ExpressionKeys<>(new String[] { "f0" }, typeInfo), typeInfo, config);
    TypeInformation<Tuple> keyType = TypeExtractor.getKeySelectorTypes(keySelector, typeInfo);
    // aggregations tested
    ReduceFunction<MyPojo3> maxByFunctionFirst = new ComparableAggregator<>("f1", typeInfo, AggregationType.MAXBY, true, config);
    ReduceFunction<MyPojo3> maxByFunctionLast = new ComparableAggregator<>("f1", typeInfo, AggregationType.MAXBY, false, config);
    ReduceFunction<MyPojo3> minByFunctionFirst = new ComparableAggregator<>("f1", typeInfo, AggregationType.MINBY, true, config);
    ReduceFunction<MyPojo3> minByFunctionLast = new ComparableAggregator<>("f1", typeInfo, AggregationType.MINBY, false, config);
    assertEquals(maxByFirstExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(maxByFunctionFirst, typeInfo.createSerializer(config)), getInputByPojoList(), keySelector, keyType));
    assertEquals(maxByLastExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(maxByFunctionLast, typeInfo.createSerializer(config)), getInputByPojoList(), keySelector, keyType));
    assertEquals(minByLastExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(minByFunctionLast, typeInfo.createSerializer(config)), getInputByPojoList(), keySelector, keyType));
    assertEquals(minByFirstExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(minByFunctionFirst, typeInfo.createSerializer(config)), getInputByPojoList(), keySelector, keyType));
}
Also used : ComparableAggregator(org.apache.flink.streaming.api.functions.aggregation.ComparableAggregator) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) StreamGroupedReduce(org.apache.flink.streaming.api.operators.StreamGroupedReduce) Keys(org.apache.flink.api.common.operators.Keys) Tuple(org.apache.flink.api.java.tuple.Tuple) Test(org.junit.Test)

Example 2 with StreamGroupedReduce

use of org.apache.flink.streaming.api.operators.StreamGroupedReduce in project flink by apache.

the class AggregationFunctionTest method minMaxByTest.

@Test
public void minMaxByTest() throws Exception {
    // Tuples are grouped on field 0, aggregated on field 1
    // preparing expected outputs
    List<Tuple3<Integer, Integer, Integer>> maxByFirstExpected = ImmutableList.of(Tuple3.of(0, 0, 0), Tuple3.of(0, 1, 1), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2));
    List<Tuple3<Integer, Integer, Integer>> maxByLastExpected = ImmutableList.of(Tuple3.of(0, 0, 0), Tuple3.of(0, 1, 1), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 2), Tuple3.of(0, 2, 5), Tuple3.of(0, 2, 5), Tuple3.of(0, 2, 5), Tuple3.of(0, 2, 8));
    List<Tuple3<Integer, Integer, Integer>> minByFirstExpected = ImmutableList.of(Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0));
    List<Tuple3<Integer, Integer, Integer>> minByLastExpected = ImmutableList.of(Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 0), Tuple3.of(0, 0, 3), Tuple3.of(0, 0, 3), Tuple3.of(0, 0, 3), Tuple3.of(0, 0, 6), Tuple3.of(0, 0, 6), Tuple3.of(0, 0, 6));
    // some necessary boiler plate
    TypeInformation<Tuple3<Integer, Integer, Integer>> typeInfo = TypeExtractor.getForObject(Tuple3.of(0, 0, 0));
    ExecutionConfig config = new ExecutionConfig();
    KeySelector<Tuple3<Integer, Integer, Integer>, Tuple> keySelector = KeySelectorUtil.getSelectorForKeys(new Keys.ExpressionKeys<>(new int[] { 0 }, typeInfo), typeInfo, config);
    TypeInformation<Tuple> keyType = TypeExtractor.getKeySelectorTypes(keySelector, typeInfo);
    // aggregations tested
    ReduceFunction<Tuple3<Integer, Integer, Integer>> maxByFunctionFirst = new ComparableAggregator<>(1, typeInfo, AggregationType.MAXBY, true, config);
    ReduceFunction<Tuple3<Integer, Integer, Integer>> maxByFunctionLast = new ComparableAggregator<>(1, typeInfo, AggregationType.MAXBY, false, config);
    ReduceFunction<Tuple3<Integer, Integer, Integer>> minByFunctionFirst = new ComparableAggregator<>(1, typeInfo, AggregationType.MINBY, true, config);
    ReduceFunction<Tuple3<Integer, Integer, Integer>> minByFunctionLast = new ComparableAggregator<>(1, typeInfo, AggregationType.MINBY, false, config);
    assertEquals(maxByFirstExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(maxByFunctionFirst, typeInfo.createSerializer(config)), getInputByList(), keySelector, keyType));
    assertEquals(maxByLastExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(maxByFunctionLast, typeInfo.createSerializer(config)), getInputByList(), keySelector, keyType));
    assertEquals(minByLastExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(minByFunctionLast, typeInfo.createSerializer(config)), getInputByList(), keySelector, keyType));
    assertEquals(minByFirstExpected, MockContext.createAndExecuteForKeyedStream(new StreamGroupedReduce<>(minByFunctionFirst, typeInfo.createSerializer(config)), getInputByList(), keySelector, keyType));
}
Also used : ComparableAggregator(org.apache.flink.streaming.api.functions.aggregation.ComparableAggregator) Tuple3(org.apache.flink.api.java.tuple.Tuple3) Keys(org.apache.flink.api.common.operators.Keys) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) Tuple(org.apache.flink.api.java.tuple.Tuple) StreamGroupedReduce(org.apache.flink.streaming.api.operators.StreamGroupedReduce) Test(org.junit.Test)

Aggregations

ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)2 Keys (org.apache.flink.api.common.operators.Keys)2 Tuple (org.apache.flink.api.java.tuple.Tuple)2 ComparableAggregator (org.apache.flink.streaming.api.functions.aggregation.ComparableAggregator)2 StreamGroupedReduce (org.apache.flink.streaming.api.operators.StreamGroupedReduce)2 Test (org.junit.Test)2 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)1