Search in sources :

Example 6 with MultipleInputTransformation

use of org.apache.flink.streaming.api.transformations.MultipleInputTransformation in project flink by apache.

the class SourceNAryInputChainingITCase method nAryInputStreamOperation.

private static DataStream<Long> nAryInputStreamOperation(final DataStream<?>... inputs) {
    final StreamExecutionEnvironment env = inputs[0].getExecutionEnvironment();
    // this is still clumsy due to the raw API
    final MultipleInputTransformation<Long> transform = new MultipleInputTransformation<>("MultipleInputOperator", new NAryUnionOpFactory(inputs.length), Types.LONG, env.getParallelism());
    for (DataStream<?> input : inputs) {
        transform.addInput(input.getTransformation());
    }
    transform.setChainingStrategy(ChainingStrategy.HEAD_WITH_SOURCES);
    env.addOperator(transform);
    return new MultipleConnectedStreams(env).transform(transform);
}
Also used : MultipleConnectedStreams(org.apache.flink.streaming.api.datastream.MultipleConnectedStreams) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) MultipleInputTransformation(org.apache.flink.streaming.api.transformations.MultipleInputTransformation)

Example 7 with MultipleInputTransformation

use of org.apache.flink.streaming.api.transformations.MultipleInputTransformation in project flink by apache.

the class MultipleInputITCase method testNonKeyed.

public void testNonKeyed(boolean withUnion) throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(1);
    TestListResultSink<Long> resultSink = new TestListResultSink<>();
    DataStream<Long> source1 = env.fromElements(1L, 10L);
    DataStream<Long> source2 = env.fromElements(2L, 11L);
    DataStream<String> source3 = env.fromElements("42", "44");
    MultipleInputTransformation<Long> multipleInput = new MultipleInputTransformation<>("My Operator", new SumAllInputOperatorFactory(withUnion ? 2 : 3), BasicTypeInfo.LONG_TYPE_INFO, 1);
    MultipleInputTransformation<Long> multipleInputTransformation;
    if (withUnion) {
        UnionTransformation<Long> union = new UnionTransformation<>(Arrays.asList(source1.getTransformation(), source2.getTransformation()));
        multipleInputTransformation = multipleInput.addInput(union);
    } else {
        multipleInputTransformation = multipleInput.addInput(source1.getTransformation()).addInput(source2.getTransformation());
    }
    env.addOperator(multipleInputTransformation.addInput(source3.getTransformation()));
    new MultipleConnectedStreams(env).transform(multipleInput).addSink(resultSink);
    env.execute();
    List<Long> result = resultSink.getResult();
    Collections.sort(result);
    long actualSum = result.get(result.size() - 1);
    assertEquals(1 + 10 + 2 + 11 + 42 + 44, actualSum);
}
Also used : MultipleConnectedStreams(org.apache.flink.streaming.api.datastream.MultipleConnectedStreams) TestListResultSink(org.apache.flink.test.streaming.runtime.util.TestListResultSink) UnionTransformation(org.apache.flink.streaming.api.transformations.UnionTransformation) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) MultipleInputTransformation(org.apache.flink.streaming.api.transformations.MultipleInputTransformation) KeyedMultipleInputTransformation(org.apache.flink.streaming.api.transformations.KeyedMultipleInputTransformation)

Aggregations

MultipleInputTransformation (org.apache.flink.streaming.api.transformations.MultipleInputTransformation)7 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)5 Transformation (org.apache.flink.api.dag.Transformation)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 Test (org.junit.Test)3 Method (java.lang.reflect.Method)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 RuntimeExecutionMode (org.apache.flink.api.common.RuntimeExecutionMode)2 WatermarkStrategy (org.apache.flink.api.common.eventtime.WatermarkStrategy)2 FilterFunction (org.apache.flink.api.common.functions.FilterFunction)2 FlatMapFunction (org.apache.flink.api.common.functions.FlatMapFunction)2 MapFunction (org.apache.flink.api.common.functions.MapFunction)2