flink旁路输出

旁路输出在Flink中叫作SideOutput,用途类似于DataStream#split,本质上是一个数据流的切分行为,按照条件将DataStream切分为多个子数据流,子数据流叫作旁路输出数据流,每个旁路输出数据流可以有自己的下游处理逻辑

旁路输出在Flink中叫作SideOutput,用途类似于DataStream#split,本质上是一个数据流的切分行为,按照条件将DataStream切分为多个子数据流,子数据流叫作旁路输出数据流,每个旁路输出数据流可以有自己的下游处理逻辑

package com.zxl.blink;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
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 OutPut {
    public static void main(String[] args) throws Exception {
        Configuration configuration=new Configuration();
        configuration.setInteger(RestOptions.PORT,8848);
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(configuration);
        environment.setParallelism(4);
        //split
        DataStream<Person> source = environment.addSource(new DataDB());
        // TODO: 2022/2/9 定义 旁路输出在Flink中叫作SideOutput,用途类似于DataStream#split,本质上是一个数据流的切分行为,按照条件将DataStream切分为多个子数据流,子数据流叫作旁路输出数据流,每个旁路输出数据流可以有自己的下游处理逻辑
        //当使用旁路输出的时候,首先需要定义OutputTag,OutputTag是每一个下游分支的标识
        OutputTag<Person>  hight=new OutputTag<Person>("hight"){};
        OutputTag<Person>  low=new OutputTag<Person>("low"){};
        SingleOutputStreamOperator<Person> process = source.process(new ProcessFunction<Person, Person>() {
            @Override
            public void processElement(Person person, Context ctx, Collector<Person> out) throws Exception {
                if (person.getPage() < 14) {
                    ctx.output(low, person);
                } else if (person.getPage() > 14 && person.getPage() < 18) {
                    ctx.output(hight, person);
                } else {
                    out.collect(person);
                }
            }
        });
        process.getSideOutput(low).print("小于14岁");
        process.getSideOutput(hight).print("大于14岁外成年");
        process.print("成年");
        environment.execute();
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值