Search in sources :

Example 1 with OutputSelector

use of org.apache.flink.streaming.api.collector.selector.OutputSelector in project flink by apache.

the class TwoInputStreamTaskTestHarness method initializeInputs.

@Override
protected void initializeInputs() throws IOException, InterruptedException {
    inputGates = new StreamTestSingleInputGate[numInputGates];
    List<StreamEdge> inPhysicalEdges = new LinkedList<StreamEdge>();
    StreamOperator<IN1> dummyOperator = new AbstractStreamOperator<IN1>() {

        private static final long serialVersionUID = 1L;
    };
    StreamNode sourceVertexDummy = new StreamNode(null, 0, "default group", dummyOperator, "source dummy", new LinkedList<OutputSelector<?>>(), SourceStreamTask.class);
    StreamNode targetVertexDummy = new StreamNode(null, 1, "default group", dummyOperator, "target dummy", new LinkedList<OutputSelector<?>>(), SourceStreamTask.class);
    for (int i = 0; i < numInputGates; i++) {
        switch(inputGateAssignment[i]) {
            case 1:
                {
                    inputGates[i] = new StreamTestSingleInputGate<IN1>(numInputChannelsPerGate, bufferSize, inputSerializer1);
                    StreamEdge streamEdge = new StreamEdge(sourceVertexDummy, targetVertexDummy, 1, new LinkedList<String>(), new BroadcastPartitioner<Object>(), null);
                    inPhysicalEdges.add(streamEdge);
                    break;
                }
            case 2:
                {
                    inputGates[i] = new StreamTestSingleInputGate<IN2>(numInputChannelsPerGate, bufferSize, inputSerializer2);
                    StreamEdge streamEdge = new StreamEdge(sourceVertexDummy, targetVertexDummy, 2, new LinkedList<String>(), new BroadcastPartitioner<Object>(), null);
                    inPhysicalEdges.add(streamEdge);
                    break;
                }
            default:
                throw new IllegalStateException("Wrong input gate assignment.");
        }
        this.mockEnv.addInputGate(inputGates[i].getInputGate());
    }
    streamConfig.setInPhysicalEdges(inPhysicalEdges);
    streamConfig.setNumberOfInputs(numInputGates);
    streamConfig.setTypeSerializerIn1(inputSerializer1);
    streamConfig.setTypeSerializerIn2(inputSerializer2);
}
Also used : StreamEdge(org.apache.flink.streaming.api.graph.StreamEdge) StreamTestSingleInputGate(org.apache.flink.runtime.io.network.partition.consumer.StreamTestSingleInputGate) AbstractStreamOperator(org.apache.flink.streaming.api.operators.AbstractStreamOperator) LinkedList(java.util.LinkedList) BroadcastPartitioner(org.apache.flink.streaming.runtime.partitioner.BroadcastPartitioner) OutputSelector(org.apache.flink.streaming.api.collector.selector.OutputSelector) StreamNode(org.apache.flink.streaming.api.graph.StreamNode)

Example 2 with OutputSelector

use of org.apache.flink.streaming.api.collector.selector.OutputSelector in project flink by apache.

the class DataStreamTest method operatorTest.

@Test
public void operatorTest() {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    DataStreamSource<Long> src = env.generateSequence(0, 0);
    MapFunction<Long, Integer> mapFunction = new MapFunction<Long, Integer>() {

        @Override
        public Integer map(Long value) throws Exception {
            return null;
        }
    };
    DataStream<Integer> map = src.map(mapFunction);
    map.addSink(new DiscardingSink<Integer>());
    assertEquals(mapFunction, getFunctionForDataStream(map));
    FlatMapFunction<Long, Integer> flatMapFunction = new FlatMapFunction<Long, Integer>() {

        private static final long serialVersionUID = 1L;

        @Override
        public void flatMap(Long value, Collector<Integer> out) throws Exception {
        }
    };
    DataStream<Integer> flatMap = src.flatMap(flatMapFunction);
    flatMap.addSink(new DiscardingSink<Integer>());
    assertEquals(flatMapFunction, getFunctionForDataStream(flatMap));
    FilterFunction<Integer> filterFunction = new FilterFunction<Integer>() {

        @Override
        public boolean filter(Integer value) throws Exception {
            return false;
        }
    };
    DataStream<Integer> unionFilter = map.union(flatMap).filter(filterFunction);
    unionFilter.addSink(new DiscardingSink<Integer>());
    assertEquals(filterFunction, getFunctionForDataStream(unionFilter));
    try {
        env.getStreamGraph().getStreamEdges(map.getId(), unionFilter.getId());
    } catch (RuntimeException e) {
        fail(e.getMessage());
    }
    try {
        env.getStreamGraph().getStreamEdges(flatMap.getId(), unionFilter.getId());
    } catch (RuntimeException e) {
        fail(e.getMessage());
    }
    OutputSelector<Integer> outputSelector = new OutputSelector<Integer>() {

        @Override
        public Iterable<String> select(Integer value) {
            return null;
        }
    };
    SplitStream<Integer> split = unionFilter.split(outputSelector);
    split.select("dummy").addSink(new DiscardingSink<Integer>());
    List<OutputSelector<?>> outputSelectors = env.getStreamGraph().getStreamNode(unionFilter.getId()).getOutputSelectors();
    assertEquals(1, outputSelectors.size());
    assertEquals(outputSelector, outputSelectors.get(0));
    DataStream<Integer> select = split.select("a");
    DataStreamSink<Integer> sink = select.print();
    StreamEdge splitEdge = env.getStreamGraph().getStreamEdges(unionFilter.getId(), sink.getTransformation().getId()).get(0);
    assertEquals("a", splitEdge.getSelectedNames().get(0));
    ConnectedStreams<Integer, Integer> connect = map.connect(flatMap);
    CoMapFunction<Integer, Integer, String> coMapper = new CoMapFunction<Integer, Integer, String>() {

        private static final long serialVersionUID = 1L;

        @Override
        public String map1(Integer value) {
            return null;
        }

        @Override
        public String map2(Integer value) {
            return null;
        }
    };
    DataStream<String> coMap = connect.map(coMapper);
    coMap.addSink(new DiscardingSink<String>());
    assertEquals(coMapper, getFunctionForDataStream(coMap));
    try {
        env.getStreamGraph().getStreamEdges(map.getId(), coMap.getId());
    } catch (RuntimeException e) {
        fail(e.getMessage());
    }
    try {
        env.getStreamGraph().getStreamEdges(flatMap.getId(), coMap.getId());
    } catch (RuntimeException e) {
        fail(e.getMessage());
    }
}
Also used : FilterFunction(org.apache.flink.api.common.functions.FilterFunction) CoFlatMapFunction(org.apache.flink.streaming.api.functions.co.CoFlatMapFunction) MapFunction(org.apache.flink.api.common.functions.MapFunction) CoMapFunction(org.apache.flink.streaming.api.functions.co.CoMapFunction) FlatMapFunction(org.apache.flink.api.common.functions.FlatMapFunction) CoFlatMapFunction(org.apache.flink.streaming.api.functions.co.CoFlatMapFunction) FlatMapFunction(org.apache.flink.api.common.functions.FlatMapFunction) Collector(org.apache.flink.util.Collector) CoMapFunction(org.apache.flink.streaming.api.functions.co.CoMapFunction) StreamEdge(org.apache.flink.streaming.api.graph.StreamEdge) OutputSelector(org.apache.flink.streaming.api.collector.selector.OutputSelector) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) Test(org.junit.Test)

Example 3 with OutputSelector

use of org.apache.flink.streaming.api.collector.selector.OutputSelector in project flink by apache.

the class OperatorChain method createOutputCollector.

// ------------------------------------------------------------------------
//  initialization utilities
// ------------------------------------------------------------------------
private <T> Output<StreamRecord<T>> createOutputCollector(StreamTask<?, ?> containingTask, StreamConfig operatorConfig, Map<Integer, StreamConfig> chainedConfigs, ClassLoader userCodeClassloader, Map<StreamEdge, RecordWriterOutput<?>> streamOutputs, List<StreamOperator<?>> allOperators) {
    List<Tuple2<Output<StreamRecord<T>>, StreamEdge>> allOutputs = new ArrayList<>(4);
    // create collectors for the network outputs
    for (StreamEdge outputEdge : operatorConfig.getNonChainedOutputs(userCodeClassloader)) {
        @SuppressWarnings("unchecked") RecordWriterOutput<T> output = (RecordWriterOutput<T>) streamOutputs.get(outputEdge);
        allOutputs.add(new Tuple2<Output<StreamRecord<T>>, StreamEdge>(output, outputEdge));
    }
    // Create collectors for the chained outputs
    for (StreamEdge outputEdge : operatorConfig.getChainedOutputs(userCodeClassloader)) {
        int outputId = outputEdge.getTargetId();
        StreamConfig chainedOpConfig = chainedConfigs.get(outputId);
        Output<StreamRecord<T>> output = createChainedOperator(containingTask, chainedOpConfig, chainedConfigs, userCodeClassloader, streamOutputs, allOperators, outputEdge.getOutputTag());
        allOutputs.add(new Tuple2<>(output, outputEdge));
    }
    // if there are multiple outputs, or the outputs are directed, we need to
    // wrap them as one output
    List<OutputSelector<T>> selectors = operatorConfig.getOutputSelectors(userCodeClassloader);
    if (selectors == null || selectors.isEmpty()) {
        // simple path, no selector necessary
        if (allOutputs.size() == 1) {
            return allOutputs.get(0).f0;
        } else {
            // send to N outputs. Note that this includes teh special case
            // of sending to zero outputs
            @SuppressWarnings({ "unchecked", "rawtypes" }) Output<StreamRecord<T>>[] asArray = new Output[allOutputs.size()];
            for (int i = 0; i < allOutputs.size(); i++) {
                asArray[i] = allOutputs.get(i).f0;
            }
            // otherwise multi-chaining would not work correctly.
            if (containingTask.getExecutionConfig().isObjectReuseEnabled()) {
                return new CopyingBroadcastingOutputCollector<>(asArray, this);
            } else {
                return new BroadcastingOutputCollector<>(asArray, this);
            }
        }
    } else {
        // otherwise multi-chaining would not work correctly.
        if (containingTask.getExecutionConfig().isObjectReuseEnabled()) {
            return new CopyingDirectedOutput<>(selectors, allOutputs);
        } else {
            return new DirectedOutput<>(selectors, allOutputs);
        }
    }
}
Also used : CopyingDirectedOutput(org.apache.flink.streaming.api.collector.selector.CopyingDirectedOutput) StreamRecord(org.apache.flink.streaming.runtime.streamrecord.StreamRecord) ArrayList(java.util.ArrayList) StreamEdge(org.apache.flink.streaming.api.graph.StreamEdge) StreamConfig(org.apache.flink.streaming.api.graph.StreamConfig) CopyingDirectedOutput(org.apache.flink.streaming.api.collector.selector.CopyingDirectedOutput) DirectedOutput(org.apache.flink.streaming.api.collector.selector.DirectedOutput) RecordWriterOutput(org.apache.flink.streaming.runtime.io.RecordWriterOutput) OutputSelector(org.apache.flink.streaming.api.collector.selector.OutputSelector) Tuple2(org.apache.flink.api.java.tuple.Tuple2) Output(org.apache.flink.streaming.api.operators.Output) CopyingDirectedOutput(org.apache.flink.streaming.api.collector.selector.CopyingDirectedOutput) DirectedOutput(org.apache.flink.streaming.api.collector.selector.DirectedOutput) RecordWriterOutput(org.apache.flink.streaming.runtime.io.RecordWriterOutput)

Example 4 with OutputSelector

use of org.apache.flink.streaming.api.collector.selector.OutputSelector in project flink by apache.

the class StreamTaskTestHarness method setupOutputForSingletonOperatorChain.

/**
	 * Users of the test harness can call this utility method to setup the stream config
	 * if there will only be a single operator to be tested. The method will setup the
	 * outgoing network connection for the operator.
	 *
	 * For more advanced test cases such as testing chains of multiple operators with the harness,
	 * please manually configure the stream config.
	 */
public void setupOutputForSingletonOperatorChain() {
    streamConfig.setChainStart();
    streamConfig.setBufferTimeout(0);
    streamConfig.setTimeCharacteristic(TimeCharacteristic.EventTime);
    streamConfig.setOutputSelectors(Collections.<OutputSelector<?>>emptyList());
    streamConfig.setNumberOfOutputs(1);
    streamConfig.setTypeSerializerOut(outputSerializer);
    streamConfig.setVertexID(0);
    StreamOperator<OUT> dummyOperator = new AbstractStreamOperator<OUT>() {

        private static final long serialVersionUID = 1L;
    };
    List<StreamEdge> outEdgesInOrder = new LinkedList<StreamEdge>();
    StreamNode sourceVertexDummy = new StreamNode(null, 0, "group", dummyOperator, "source dummy", new LinkedList<OutputSelector<?>>(), SourceStreamTask.class);
    StreamNode targetVertexDummy = new StreamNode(null, 1, "group", dummyOperator, "target dummy", new LinkedList<OutputSelector<?>>(), SourceStreamTask.class);
    outEdgesInOrder.add(new StreamEdge(sourceVertexDummy, targetVertexDummy, 0, new LinkedList<String>(), new BroadcastPartitioner<Object>(), null));
    streamConfig.setOutEdgesInOrder(outEdgesInOrder);
    streamConfig.setNonChainedOutputs(outEdgesInOrder);
}
Also used : OutputSelector(org.apache.flink.streaming.api.collector.selector.OutputSelector) StreamEdge(org.apache.flink.streaming.api.graph.StreamEdge) StreamNode(org.apache.flink.streaming.api.graph.StreamNode) AbstractStreamOperator(org.apache.flink.streaming.api.operators.AbstractStreamOperator) LinkedList(java.util.LinkedList) BroadcastPartitioner(org.apache.flink.streaming.runtime.partitioner.BroadcastPartitioner)

Aggregations

OutputSelector (org.apache.flink.streaming.api.collector.selector.OutputSelector)4 StreamEdge (org.apache.flink.streaming.api.graph.StreamEdge)4 LinkedList (java.util.LinkedList)2 StreamNode (org.apache.flink.streaming.api.graph.StreamNode)2 AbstractStreamOperator (org.apache.flink.streaming.api.operators.AbstractStreamOperator)2 BroadcastPartitioner (org.apache.flink.streaming.runtime.partitioner.BroadcastPartitioner)2 ArrayList (java.util.ArrayList)1 FilterFunction (org.apache.flink.api.common.functions.FilterFunction)1 FlatMapFunction (org.apache.flink.api.common.functions.FlatMapFunction)1 MapFunction (org.apache.flink.api.common.functions.MapFunction)1 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)1 StreamTestSingleInputGate (org.apache.flink.runtime.io.network.partition.consumer.StreamTestSingleInputGate)1 CopyingDirectedOutput (org.apache.flink.streaming.api.collector.selector.CopyingDirectedOutput)1 DirectedOutput (org.apache.flink.streaming.api.collector.selector.DirectedOutput)1 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)1 CoFlatMapFunction (org.apache.flink.streaming.api.functions.co.CoFlatMapFunction)1 CoMapFunction (org.apache.flink.streaming.api.functions.co.CoMapFunction)1 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)1 Output (org.apache.flink.streaming.api.operators.Output)1 RecordWriterOutput (org.apache.flink.streaming.runtime.io.RecordWriterOutput)1