Side Output(侧输出)

Side Output(侧输出)

除了从DataStream操作的结果中获取主数据流之外,你还可以产生任意数量额外的侧输出结果流。侧输出结果流的数据类型不需要与主数据流的类型一致,不同侧输出流的类型也可以不同。当您想要拆分数据流时(通常必须复制流),然后从每个流过滤出您不想拥有的数据,此操作将非常有用。

package side_Outputs;

import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class Test {

    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream input = env.fromElements(1);

        final OutputTag outputTag = new OutputTag("side-output"){};

        SingleOutputStreamOperator mainDataStream = input
                .process(new ProcessFunction() {

                    @Override
                    public void processElement(
                            Integer input,
                            Context ctx,
                            Collector out) throws Exception {
                        // emit data to regular output
                        out.collect(input);

                        // emit data to side output
                        ctx.output(outputTag, "sideout-" + String.valueOf(input));
                    }
                });


        final OutputTag output = new OutputTag("side-output"){};

        SingleOutputStreamOperator reciveStream = null;

        DataStream sideOutputStream = mainDataStream.getSideOutput(output);

    }
}

你可能感兴趣的:(Side Output(侧输出))