Flink个人学习整理-Process侧输出和定时器篇(七)

Flink个人学习整理-Process侧输出和定时器篇(七)

一、侧输出流demo

public class Flink_Process_SideOutPut {
    public static void main(String[] args) throws Exception {
        //  获取运行时环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<Sensor> localhostDS = env.socketTextStream("localhost", 9999)
                .map(new MapFunction<String, Sensor>() {
                    @Override
                    public Sensor map(String value) throws Exception {
                        String[] strings = value.split(",");
                        return new Sensor(
                                strings[0],
                                Long.parseLong(strings[1]),
                                Integer.parseInt(strings[2])
                        );
                    }
                });

        //  使用Process将数据分流
        SingleOutputStreamOperator<Sensor> process = localhostDS.process(new SplitProcessFunction());

        //  打印数据
        process.print("主流");

        process.getSideOutput(new OutputTag<Tuple2<String,Integer>>("side"){}).print("process侧输出流");


        env.execute();
    }

    public static class SplitProcessFunction extends ProcessFunction<Sensor,Sensor>{

        @Override
        public void processElement(Sensor value, Context ctx, Collector<Sensor> out) throws Exception {

            //  取出进行判断的字段数据
            Integer vc = value.getVc();

            //  判断数据,进行分流
            if (vc >= 30){
                out.collect(value);
            }else {
                ctx.output(new OutputTag<Tuple2<String,Integer>>("side"){},Tuple2.of(value.getId(),vc));
            }
        }
    }
}

二、定时器

//  定时间只能用在KeyedStream
public class Flink_Process_OnTimer {
    public static void main(String[] args) throws Exception {

        //  获取运行时环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        KeyedStream<Sensor, String> localhostDS = env.socketTextStream("localhost", 9999)
                .map(new MapFunction<String, Sensor>() {
                    @Override
                    public Sensor map(String value) throws Exception {
                        String[] strings = value.split(",");
                        return new Sensor(
                                strings[0],
                                Long.parseLong(strings[1]),
                                Integer.parseInt(strings[2])
                        );
                    }
                })
                .keyBy(new KeySelector<Sensor, String>() {
                    @Override
                    public String getKey(Sensor value) throws Exception {
                        return value.getId();
                    }
                });


        //  使用ProcessFunction定时器功能
        localhostDS.process(new KeyedProcessFunction<String, Sensor, Sensor>() {

            @Override
            public void processElement(Sensor value, Context ctx, Collector<Sensor> out) throws Exception {

                //  获取定时器服务,获取当前处理时间
                long ts = ctx.timerService().currentProcessingTime();
                System.out.println("当前处理时间:"+ts);
                //  注册定时器 +5s
                ctx.timerService().registerProcessingTimeTimer(ts + 5000L);
                out.collect(value);
            }

            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<Sensor> out) throws Exception {

                //  timestamp:定时器触发时间   ctx:侧输出流    out:主流
                System.out.println("定时器触发时间:"+timestamp);

                //  循环制定定时器
                long ts = ctx.timerService().currentProcessingTime();
                ctx.timerService().registerProcessingTimeTimer(ts + 5000L);

            }
        }).print();

        env.execute();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值