public class OutPutTagStream {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(3);
FlinkKafkaConsumer010<String> dwdConsumer = new FlinkKafkaConsumer010<>("dwd", new SimpleStringSchema(), KafkaUtils.comsumerProps());
DataStreamSource<String> source = env.addSource(dwdConsumer);
// 创建一个侧输出流
OutputTag<String> outBak = new OutputTag<String>("bak") {
};
// 只有process级别的函数能够写入侧输出流
SingleOutputStreamOperator<String> out = source.process(new ProcessFunction<String, String>() {
@Override
public void processElement(String s, Context context, Collector<String> collector) throws Exception {
JSONObject jn = JSON.parseObject(s);
if (jn.get("vodid").equals("vod_0000000001")) {
// 写入侧输出流
context.output(outBak, "BAK:" + s);
} else {
collector.collect("MAIN:" + s);
}
}
});
// 获取侧输出流
DataStream<String> bak = out.getSideOutput(outBak);
out.print();
bak.print();
env.execute();
}
}
Flink笔记-侧输出流
最新推荐文章于 2024-10-05 14:56:50 发布