Flink-水印-主流与侧流输出

实际工作中,数据到来不会总是有序的,所以window需要结合水印来使用,以满足实际场景。但是既便用了水印,也有可能存在漏网之鱼,这时就要用到侧流来将漏网数据收住,提高数据精准度。

运行方法

1、将下述代码贴到工程里,注意需要依赖flink的相关包 2、调试步骤1:linux 打开 nc工具,启动控制台,准备数据输入 3、调试步骤2:启动工程,观察侧流与主流的数据

例子数据

yaml 复制代码
        /**
         * 1000,a,1
         * 2000,a,1
         * 4998,a,1
         * 4999,a,1
         * 6999,a,1
         * 12000,a,1
         * 当到了12000时,执行的时,第二个窗口内的数据
         * 第1个窗口:[0000,5000)
         * 第2个窗口:[5000,10000)
         * 注:flink窗口是左闭右开的
         */

代码如下

swift 复制代码
package com.flink.watermarks;


import cn.hutool.core.date.format.FastDateFormat;
import lombok.val;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class EventTimeWMApp {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);

        test01(env);
        env.execute("WindowApp");
    }

    /**
     * EventTime结合WM使用
     * 输入数据格式:时间字段、单词、次数
     * @param env
     */
    public static void test01(StreamExecutionEnvironment env){
        //用于接收延迟到来的数据(窗口已结束,还有相应的时间段的数据进来)
        OutputTag<Tuple2<String,Integer>> outputTag = new OutputTag<Tuple2<String,Integer>>("late-data"){};


        DataStreamSource<String> source = env.socketTextStream("localhost",9527);

        //开始建数据源时,直接建watermarker更好一些,而不是在过程中建。
        SingleOutputStreamOperator<String> lines = source.assignTimestampsAndWatermarks(
                new BoundedOutOfOrdernessTimestampExtractor<String>(Time.seconds(0)) {
                    //乱序的watermarker处理器
                    @Override
                    public long extractTimestamp(String element) {
                        return Long.parseLong(element.split(",")[0]);//获取数据中的第一列,被当成触发5秒一个窗口的时间序列(对比值)
                    }
                }
        );

        SingleOutputStreamOperator<Tuple2<String,Integer>>  mapStream = lines.map(new MapFunction<String, Tuple2<String,Integer>>(){
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] splits = value.split(",");
                try{
                    return Tuple2.of(splits[1],Integer.parseInt(splits[2].trim()));
                }catch (Exception e){
                    e.printStackTrace();
                    return new Tuple2("null",Integer.MIN_VALUE);
                }
            }
        });

        /**
         * 1000,a,1
         * 2000,a,1
         * 4998,a,1
         * 4999,a,1
         * 6999,a,1
         * 12000,a,1
         * 当到了12000时,执行的时,第二个窗口内的数据
         * 第1个窗口:[0000,5000)
         * 第2个窗口:[5000,10000)
         * 注:flink窗口是左闭右开的
         */
        SingleOutputStreamOperator window =  mapStream.keyBy(x -> x.f0)
            .window(TumblingEventTimeWindows.of(Time.seconds(5)))
            //sideOutputLateData(OutputTag<T> outputTag) ,用于解决窗口结束,还有数据进来的情况
                .sideOutputLateData(outputTag)//接收延迟数据
            .reduce(new ReduceFunction<Tuple2<String, Integer>>() {
                @Override
                public Tuple2<String, Integer> reduce(Tuple2<String, Integer> v1, Tuple2<String, Integer> v2) throws Exception {
                    System.out.println(" ---reduce invoked ---" + v1.f0 + "[(" + v1.f1 + "," + v2.f1 + ")]===>" + (v1.f1 + v2.f1));
                    return Tuple2.of(v1.f0, v1.f1 + v2.f1);
                }//以增量的方式聚合
            }, new ProcessWindowFunction<Tuple2<String, Integer>, Object, String, TimeWindow>() {
                FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
                //双窗口的方式
                @Override
                public void process(String s, Context context, Iterable<Tuple2<String, Integer>> elements, Collector<Object> out) throws Exception {
                    for (Tuple2<String, Integer> element : elements) {
                        out.collect("[" + format.format(context.window().getStart()) + "]===> " + format.format(context.window().getEnd()) + "]," + element.f0 + "===>" + element.f1);
                        //});
                    } //窗口的开始时间
                }
            });
            //.sum(1)
            //主流数据
            window.print();

            //侧流数据
            DataStream<Tuple2<String,Integer>> sideOutput = window.getSideOutput(outputTag); //得到延迟数据
            sideOutput.print();

    }


}
相关推荐
霍格沃兹软件测试开发7 小时前
Dify平台:Agent开发初学者指南
大数据·人工智能·深度学习
IT研究室9 小时前
大数据毕业设计选题推荐-基于大数据的痴呆症预测数据可视化分析系统-Spark-Hadoop-Bigdata
大数据·hadoop·信息可视化·spark·源码·bigdata
数据知道9 小时前
【系统分析师】高分论文:论大数据架构的应用
大数据·架构·软考高级·软考高项·系统分析师·高分论文
天翼云开发者社区10 小时前
分布式系统-秒杀
大数据·安全·中间件
项目題供诗12 小时前
Hadoop(五)
大数据·hadoop·分布式
阿里云大数据AI技术12 小时前
[VLDB 2025]阿里云大数据AI平台多篇论文被收录
数据库·flink
请提交用户昵称14 小时前
Flume
大数据·flume
Lx35216 小时前
MapReduce Combiner使用技巧:减少数据传输量
大数据·hadoop
smilejingwei17 小时前
数据分析编程第六步:大数据运算
java·大数据·开发语言·数据分析·编程·esprocspl
是乐谷19 小时前
Python图片转WebP常用库推荐:Pillow、Wand、cv2
大数据·运维·开发语言·人工智能·python·开源·pillow