• Flink多流转换 完整使用 (第八章)


    无论是基本的简单转换和聚合,还是基于窗口的计算,我们都是针对一条流上的数据进行处理的。而在实际应用中,可能需要将不同来源的数据连接合并在一起处理,也有可能需要将一条流拆分开,所以经常会有对多条流进行处理的场景

    简单划分的话,多流转换可以分为“分流”“合流”两大类。目前分流的操作一般是通过侧输出流(side output)来实现,而合流的算子比较丰富,根据不同的需求可以调用union、connect、join以及coGroup等接口进行连接合并操作

    一、分流

    所谓“分流”,就是将一条数据流拆分成完全独立的两条、甚至多条流。也就是基于一个DataStream,得到完全平等的多个子DataStream,如图8-1所示。一般来说,我们会定义一些筛选条件,将符合条件的数据拣选出来放到对应的流里。

    在这里插入图片描述

    1、简单实现

    其实根据条件筛选数据的需求,本身非常容易实现:只要针对同一条流多次独立调用.filter()方法进行筛选,就可以得到拆分之后的流了。

    例如,我们可以将电商网站收集到的用户行为数据进行一个拆分,根据类型(type)的不同,分为“Mary”的浏览数据、“Bob”的浏览数据等等。那么代码就可以这样实现:

    1)编码

    import org.apache.flink.api.common.functions.FilterFunction;
    import org.apache.flink.streaming.api.datastream.DataStream;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    
    public class SplitStreamByFilter {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
            SingleOutputStreamOperator<Event> stream = env
                    .addSource(new ClickSource());
            // 筛选Mary的浏览行为放入MaryStream流中
            DataStream<Event> MaryStream = stream.filter(new FilterFunction<Event>() {
                @Override
                public boolean filter(Event value) throws Exception {
                    return value.user.equals("Mary");
                }
            });
            // 筛选Bob的购买行为放入BobStream流中
            DataStream<Event> BobStream = stream.filter(new FilterFunction<Event>() {
                @Override
                public boolean filter(Event value) throws Exception {
                    return value.user.equals("Bob");
                }
            });
            // 筛选其他人的浏览行为放入elseStream流中
            DataStream<Event> elseStream = stream.filter(new FilterFunction<Event>() {
                @Override
                public boolean filter(Event value) throws Exception {
                    return !value.user.equals("Mary") && !value.user.equals("Bob") ;
                }
            });
    
            MaryStream.print("Mary pv");
            BobStream.print("Bob pv");
            elseStream.print("else pv");
    
            env.execute();
    
        }
    
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    2) 结果

    Bob pv> Event{user='Bob', url='./home', timestamp='2022-09-08 10:35:15.103'}
    Mary pv> Event{user='Mary', url='./home', timestamp='2022-09-08 10:35:16.118'}
    Bob pv> Event{user='Bob', url='./cart', timestamp='2022-09-08 10:35:17.131'}
    Mary pv> Event{user='Mary', url='./home', timestamp='2022-09-08 10:35:18.146'}
    Bob pv> Event{user='Bob', url='./fav', timestamp='2022-09-08 10:35:19.161'}
    else pv> Event{user='Cary', url='./home', timestamp='2022-09-08 10:35:20.165'}
    else pv> Event{user='Cary', url='./home', timestamp='2022-09-08 10:35:21.179'}
    Bob pv> Event{user='Bob', url='./prod?id=10', timestamp='2022-09-08 10:35:22.189'}
    Mary pv> Event{user='Mary', url='./prod?id =100', timestamp='2022-09-08 10:35:23.192'}
    Mary pv> Event{user='Mary', url='./cart', timestamp='2022-09-08 10:35:24.207'}
    else pv> Event{user='Alice', url='./prod?id =100', timestamp='2022-09-08 10:35:25.221'}
    Mary pv> Event{user='Mary', url='./fav', timestamp='2022-09-08 10:35:26.222'}
    Bob pv> Event{user='Bob', url='./prod?id =100', timestamp='2022-09-08 10:35:27.234'}
    Mary pv> Event{user='Mary', url='./prod?id=10', timestamp='2022-09-08 10:35:28.246'}
    Mary pv> Event{user='Mary', url='./prod?id=10', timestamp='2022-09-08 10:35:29.26'}
    else pv> Event{user='Alice', url='./prod?id =100', timestamp='2022-09-08 10:35:30.267'}
    else pv> Event{user='Alice', url='./cart', timestamp='2022-09-08 10:35:31.268'}
    Bob pv> Event{user='Bob', url='./cart', timestamp='2022-09-08 10:35:32.282'}
    else pv> Event{user='Cary', url='./home', timestamp='2022-09-08 10:35:33.283'}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3)问题

    这种实现非常简单,但代码显得有些冗余——我们的处理逻辑对拆分出的三条流其实是一样的,却重复写了三次。而且这段代码背后的含义,是将原始数据流stream复制三份,然后对每一份分别做筛选;这明显是不够高效的。我们自然想到,能不能不用复制流,直接用一个算子就把它们都拆分开呢

    在早期的版本中,DataStream API中提供了一个.split()方法,专门用来将一条流“切分”成多个。它的基本思路其实就是按照给定的筛选条件,给数据分类“盖戳”;然后基于这条盖戳之后的流,分别拣选想要的“戳”就可以得到拆分后的流。这样我们就不必再对流进行复制了。不过这种方法有一个缺陷:因为只是“盖戳”拣选,所以无法对数据进行转换,分流后的数据类型必须跟原始流保持一致。这就极大地限制了分流操作的应用场景现在split方法已经淘汰掉了,我们以后分流只使用下面要讲的侧输出流。

    理解:给数据盖戳、根据对应的戳、把想要的数据拣选出来

    2、使用侧输出流

    在Flink 1.13版本中,已经弃用了.split()方法,取而代之的是直接用处理函数(process function)的侧输出流(side output)。

    我们知道,处理函数本身可以认为是一个转换算子,它的输出类型是单一的,处理之后得到的仍然是一个DataStream;而侧输出流则不受限制,可以任意自定义输出数据,它们就像从“主流”上分叉出的“支流”。尽管看起来主流和支流有所区别,不过实际上它们都是某种类型的DataStream,所以本质上还是平等的。利用侧输出流就可以很方便地实现分流操作,而且得到的多条DataStream类型可以不同,这就给我们的应用带来了极大的便利。

    1) 编码

    package com.example.chapter08;
    
    import com.example.chapter05.ClickSource;
    import com.example.chapter05.Event;
    import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
    import org.apache.flink.api.common.eventtime.WatermarkStrategy;
    import org.apache.flink.api.java.tuple.Tuple3;
    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;
    
    
    /**
     * 分流
     * 测输出流
     */
    import java.time.Duration;
    
    public class SplitStreamTest {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
            SingleOutputStreamOperator<Event> stream = env.addSource(new ClickSource())
                    .assignTimestampsAndWatermarks(WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ZERO)
                            .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                                @Override
                                public long extractTimestamp(Event event, long l) {
                                    return event.timestamp;
                                }
                            })
                    );
    
            //定义测输出流
    
            OutputTag<Tuple3<String, String, Long>> maryTag = new OutputTag<Tuple3<String, String, Long>>("Mary") {
            };
    
            OutputTag<Tuple3<String, String, Long>> bobTag = new OutputTag<Tuple3<String, String, Long>>("Bob") {
            };
    
            SingleOutputStreamOperator<Event> processStream = stream.process(new ProcessFunction<Event, Event>() {
                @Override
                public void processElement(Event value, ProcessFunction<Event, Event>.Context ctx, Collector<Event> out) throws Exception {
    
                    if (value.user.equals("Mary"))
                        ctx.output(maryTag, Tuple3.of(value.user, value.url, value.timestamp));
                    else if (value.user.equals("Bob"))
                        ctx.output(bobTag, Tuple3.of(value.user, value.url, value.timestamp));
                    else
                        out.collect(value);
    
                }
            });
    
            processStream.print("else");
            processStream.getSideOutput(maryTag).print("Mary");
            processStream.getSideOutput(bobTag).print("Bob");
    
    
            env.execute();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    2) 结果

    Bob> (Bob,./cart,1662608050576)
    else> Event{user='Cary', url='./fav', timestamp='2022-09-08 11:34:11.577'}
    else> Event{user='Cary', url='./fav', timestamp='2022-09-08 11:34:12.588'}
    Mary> (Mary,./prod?id =100,1662608053602)
    else> Event{user='Alice', url='./home', timestamp='2022-09-08 11:34:14.611'}
    Bob> (Bob,./fav,1662608055625)
    else> Event{user='Cary', url='./prod?id =100', timestamp='2022-09-08 11:34:16.638'}
    else> Event{user='Cary', url='./home', timestamp='2022-09-08 11:34:17.645'}
    Mary> (Mary,./cart,1662608058657)
    Mary> (Mary,./home,1662608059666)
    Mary> (Mary,./fav,1662608060679)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    这里我们定义了两个侧输出流,分别拣选Mary的浏览事件和Bob的浏览事件;由于类型已经确定,我们可以只保留(用户id, url, 时间戳)这样一个三元组。而剩余的事件则直接输出到主流,类型依然保留Event,就相当于之前的elseStream。这样的实现方式显然更简洁,也更加灵活。

    二、基本合流操作

    既然一条流可以分开,自然多条流就可以合并。在实际应用中,我们经常会遇到来源不同的多条流,需要将它们的数据进行联合处理。所以Flink中合流的操作会更加普遍,对应的API也更加丰富。

    1、联合(Union)

    最简单的合流操作,就是直接将多条流合在一起,叫作流的“联合”(union),如图8-2所示。联合操作要求必须流中的数据类型必须相同合并之后的新流会包括所有流中的元素,数据类型不变。这种合流方式非常简单粗暴,就像公路上多个车道汇在一起一样。

    在这里插入图片描述

    在代码中,我们只要基于DataStream直接调用.union()方法,传入其他DataStream作为参数,就可以实现流的联合了;得到的依然是一个DataStream:

    stream1.union(stream2, stream3, ...)
    
    • 1

    注意:union()的参数可以是多个DataStream,所以联合操作可以实现多条流的合并。

    1)编码

    package com.example.chapter08;
    
    import com.example.chapter05.Event;
    import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
    import org.apache.flink.api.common.eventtime.WatermarkStrategy;
    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 java.time.Duration;
    
    /**
     * 合流
     * Union
     */
    public class UnionTest {
        public static void main(String[] args) {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
            SingleOutputStreamOperator<Event> stream1 = env.socketTextStream("localhost", 7777).map(data -> {
                String[] field = data.split(",");
                return new Event(field[0].trim(), field[1].trim(), Long.valueOf(field[2].trim()));
            }).assignTimestampsAndWatermarks(WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ofSeconds(2)) //延迟两秒
                    .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                        @Override
                        public long extractTimestamp(Event event, long l) {
                            return event.timestamp;
                        }
                    }));
    
            stream1.print("stream1");
    
    
            SingleOutputStreamOperator<Event> stream2 = env.socketTextStream("localhost", 7777).map(data -> {
                String[] field = data.split(",");
                return new Event(field[0].trim(), field[1].trim(), Long.valueOf(field[2].trim()));
            }).assignTimestampsAndWatermarks(WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ofSeconds(5))// 延迟5秒
                    .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                        @Override
                        public long extractTimestamp(Event event, long l) {
                            return event.timestamp;
                        }
                    }));
    
    
            stream1.print("stream2");
    
            //合并两条流
            stream1.union(stream2)
                    //比如想看水位线到哪里了 process 打印当前的信息
                    .process(new ProcessFunction<Event, String>() {
                        @Override
                        public void processElement(Event value, ProcessFunction<Event, String>.Context ctx, Collector<String> out) throws Exception {
                            out.collect("水位线: " + ctx.timerService().currentWatermark());
                        }
                    }).print();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    2、连接(Connect)

    流的联合虽然简单,不过受限于数据类型不能改变,灵活性大打折扣,所以实际应用较少出现。除了联合(union),Flink还提供了另外一种方便的合流操作——连接(connect)。顾名思义,这种操作就是直接把两条流像接线一样对接起来。

    除了联合(union),Flink还提供了另外一种方便的合流操作——连接(connect)。顾名思义,这种操作就是直接把两条流像接线一样对接起来。

    1. 连接流(ConnectedStreams)

    为了处理更加灵活,连接操作允许流的数据类型不同。但我们知道一个DataStream中的数据只能有唯一的类型,所以连接得到的并不是DataStream ,而是一个“连接流”(ConnectedStreams)。

    连接流可以看成是两条流形式上的“统一”,被放在了一个同一个流中;事实上内部仍保持各自的数据形式不变,彼此之间是相互独立的。要想得到新的DataStream,还需要进一步定义一个“同处理”(co-process)转换操作,用来说明对于不同来源、不同类型的数据,怎样分别进行处理转换、得到统一的输出类型。所以整体上来,两条流的连接就像是“一国两制”, 两条流可以保持各自的数据类型、处理方式也可以不同,不过最终还是会统一到同一个DataStream中

    在这里插入图片描述

    1. 编码(ConnectedStreams)

    package com.example.chapter08;
    
    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.co.CoMapFunction;
    
    /**
     * 多流转换
     * 合流
     * 链接Connect
     */
    public class ConnectTest {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
            DataStreamSource<Integer> stream1 = env.fromElements(1, 2, 3);
            DataStreamSource<Long> stream2 = env.fromElements(4L, 5L, 6L);
    
            // TODO CoMapFunction
            SingleOutputStreamOperator<String> result = stream1.connect(stream2)
                    .map(new CoMapFunction<Integer, Long, String>() {
                        @Override
                        public String map1(Integer value) throws Exception {
                            return "Integer: " + value;
                        }
    
                        @Override
                        public String map2(Long value) throws Exception {
                            return "Long: " + value;
                        }
                    });
    
            result.print();
    
            env.execute();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    3、程序架构实现思路(风控手段)

    在两条流中的数据到来时分别调用。我们把这种接口叫作“协同处理函数”(co-process function)。与CoMapFunction类似,如果是调用.flatMap()就需要传入一个CoFlatMapFunction,需要实现flatMap1()、flatMap2()两个方法;而调用.process()时,传入的则是一个CoProcessFunction。

    很明显CoProcessFunction也是“处理函数”家族中的一员,用法非常相似。它需要实现的就是processElement1()、processElement2()两个方法,在每个数据到来时,会根据来源的流调用其中的一个方法进行处理。CoProcessFunction同样可以通过上下文ctx来访问timestamp、水位线,并通过TimerService注册定时器;另外也提供了.onTimer()方法,用于定义定时触发的处理操作。

    1. CoProcessFunction 协同处理函数

    功能:

    实现一个实时对账的需求,也就是app的支付操作和第三方的支付操作的一个双流Join
    App的支付事件和第三方的支付事件将会互相等待5秒钟,如果等不来对应的支付事件,那么就输出报警信息

    package com.example.chapter08;
    
    import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
    import org.apache.flink.api.common.eventtime.WatermarkStrategy;
    import org.apache.flink.api.common.state.ValueState;
    import org.apache.flink.api.common.state.ValueStateDescriptor;
    import org.apache.flink.api.common.typeinfo.Types;
    import org.apache.flink.api.java.tuple.Tuple3;
    import org.apache.flink.api.java.tuple.Tuple4;
    import org.apache.flink.configuration.Configuration;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
    import org.apache.flink.util.Collector;
    
    import java.time.Duration;
    
    /**
     * 多流转换
     * 程序架构和实现思路
     * 风控手段
     *
     * 1、生成日志
     * 2、链接流 connect
     * 3、自定义 OrderMatchResult
     * 4、定义状态
     * 5、从上下文获取
     * 5、processElement1和processElement2 处理数据
     * 6、定时监控 onTimer
     *
     *
     * 结果:
     * 对账成功: (order-1,app,1000) (order-1,third-party,success,3000)
     * 对账成功: (order-3,third-party,success,4000) (order-3,app,3500)
     * 对账失败: (order-2,app,2000) 第三方支付平台信息未到
     */
    public class BillCheckExample {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
            //来自APP的支付日志
            SingleOutputStreamOperator<Tuple3<String, String, Long>> appStream = env.fromElements(
                    Tuple3.of("order-1", "app", 1000L),
                    Tuple3.of("order-2", "app", 2000L),
                    Tuple3.of("order-3", "app", 3500L)
    
            ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple3<String, String, Long>>forBoundedOutOfOrderness(Duration.ZERO)
                    .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Long>>() {
                        @Override
                        public long extractTimestamp(Tuple3<String, String, Long> element, long l) {
                            return element.f2;
                        }
                    })
            );
    
    
            // 来自第三方支付平台的支付日志
            SingleOutputStreamOperator<Tuple4<String, String, String, Long>> thirdpartStream = env.fromElements(
                            Tuple4.of("order-1", "third-party", "success", 3000L),
                            Tuple4.of("order-3", "third-party", "success", 4000L)
                    )
                    .assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple4<String, String, String, Long>>forBoundedOutOfOrderness(Duration.ZERO)
                            .withTimestampAssigner(new SerializableTimestampAssigner<Tuple4<String, String, String, Long>>() {
                                @Override
                                public long extractTimestamp(Tuple4<String, String, String, Long> element, long l) {
                                    return element.f3;
                                }
                            }));
    
            //检测同一支付单在两百条流中是否匹配,不匹配就报警
    //         appStream.keyBy(data -> data.f0)
    //                .connect(thirdPartStream.keyBy(data -> data.f0));
    
            appStream.connect(thirdpartStream)
                    .keyBy(data -> data.f0, data -> data.f0)
                    //涉及到等多少时间  使用定时器process
                    .process(new OrderMatchResult())
                    .print();
    
            env.execute();
        }
    
        //自定义实现
        public static class OrderMatchResult extends CoProcessFunction<Tuple3<String, String, Long>, Tuple4<String, String, String, Long>, String> {
    
            // 定义状态变量,用来保存已到达的事件(状态中包含事件)
            private ValueState<Tuple3<String, String, Long>> appEventState;
    
            private ValueState<Tuple4<String, String, String, Long>> thirdPartyEventState;
    
    
            /**
             * TODO 上面的面两个状态要从运行时上下文获取
             *
             * @param parameters
             * @throws Exception
             */
            @Override
            public void open(Configuration parameters) throws Exception {
                appEventState = getRuntimeContext().getState(
                        new ValueStateDescriptor<Tuple3<String, String, Long>>("app-event", Types.TUPLE(Types.STRING
                                , Types.STRING,
                                Types.STRING
                        ))
                );
    
                thirdPartyEventState = getRuntimeContext().getState(
                        new ValueStateDescriptor<Tuple4<String, String, String, Long>>("thirdParty-event",
                                Types.TUPLE(Types.STRING, Types.STRING, Types.STRING, Types.STRING
                                ))
                );
    
            }
    
            /**
             * TODO 来的是app Event,看另一条流中的事件是否来过
             *
             * @param value
             * @param ctx
             * @param out
             * @throws Exception
             */
            @Override
            public void processElement1(Tuple3<String, String, Long> value,
                                        CoProcessFunction<Tuple3<String, String, Long>,
                                                Tuple4<String, String, String, Long>,
                                                String>.Context ctx,
                                        Collector<String> out) throws Exception {
    
                //TODO 从完整的事件中获取
                if (thirdPartyEventState.value() != null) {
                    out.collect("对账成功: " + thirdPartyEventState.value() + " " + value);
    
                    //TODO 清空状态
                    thirdPartyEventState.clear();
    
                } else {
                    //更新状态
                    appEventState.update(value);
    
                    //TODO 注册一个5秒后的定时器。开始等待另一条流的事件
                    ctx.timerService().registerEventTimeTimer(value.f2 + 5000L);
                }
            }
    
            @Override
            public void processElement2(Tuple4<String, String, String, Long> value,
                                        CoProcessFunction<Tuple3<String, String, Long>,
                                                Tuple4<String, String, String, Long>,
                                                String>.Context ctx, Collector<String> out) throws Exception {
    
                //来的是app Event,看另一条流中的事件是否来过
                if (appEventState.value() != null) {
                    out.collect("对账成功: " + appEventState.value() + " " + value);
    
                    //清空状态
                    appEventState.clear();
                } else {
                    //更新状态
                    thirdPartyEventState.update(value);
    
                    //注册一个5秒后的定时器。开始等待另一条流的事件
                    ctx.timerService().registerEventTimeTimer(value.f3);
                }
            }
    
    
            /**
             * 等到定时器触发
             * @param timestamp
             * @param ctx
             * @param out
             * @throws Exception
             */
            @Override
            public void onTimer(long timestamp, CoProcessFunction<Tuple3<String, String, Long>, Tuple4<String, String, String, Long>, String>.OnTimerContext ctx, Collector<String> out) throws Exception {
                //TODO 定时触发器,判断状态,如果某个状态不为空,说明另一条流中事件没来
                if (appEventState.value() != null) {
                    out.collect("对账失败: " + appEventState.value() + " " + "第三方支付平台信息未到");
                }
    
                //定时触发器,判断状态,如果某个状态不为空,说明另一条流中事件没来
                if (thirdPartyEventState.value() != null) {
                    out.collect("对账失败: " + thirdPartyEventState.value() + " " + "app信息未到");
                }
    
                appEventState.clear();
                thirdPartyEventState.clear();
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193

    2. 说明解释

    在程序中,我们声明了两个状态变量分别用来保存App的支付信息和第三方的支付信息。App的支付信息到达以后,会检查对应的第三方支付信息是否已经先到达(先到达会保存在对应的状态变量中),如果已经到达了,那么对账成功,直接输出对账成功的信息,并将保存第三方支付消息的状态变量清空。如果App对应的第三方支付信息没有到来,那么我们会注册一个5秒钟之后的定时器,也就是说等待第三方支付事件5秒钟。当定时器触发时,检查保存app支付信息的状态变量是否还在,如果还在,说明对应的第三方支付信息没有到来,所以输出报警信息。

    4、窗口联结(Window Join)

    基于时间的操作,最基本的当然就是时间窗口了。我们之前已经介绍过Window API的用法,主要是针对单一数据流在某些时间段内的处理计算。那如果我们希望将两条流的数据进行合并、且同样针对某段时间进行处理和统计,又该怎么做呢?

    Flink为这种场景专门提供了一个窗口联结(window join)算子,可以定义时间窗口,并将两条流中共享一个公共键(key)的数据放在窗口中进行配对处理。

    1. 窗口联结的调用

    窗口联结在代码中的实现,首先需要调用DataStream的.join()方法来合并两条流,得到一个JoinedStreams;接着通过.where()和.equalTo()方法指定两条流中联结的key;然后通过.window()开窗口,并调用.apply()传入联结窗口函数进行处理计算

    stream1.join(stream2)
        .where(<KeySelector>)
        .equalTo(<KeySelector>)
        .window(<WindowAssigner>)
        .apply(<JoinFunction>)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    .where()的参数是键选择器(KeySelector),用来指定第一条流中的key;
    .equalTo()传入的KeySelector则指定了第二条流中的key
    两者相同的元素,如果在同一窗口中,就可以匹配起来,并通过一个“联结函数”(JoinFunction)进行处理了。

    .window()传入的就是窗口分配器
    滚动窗口(tumbling window)、滑动窗口(sliding window)和会话窗口(session window)。

    .apply()可以看作实现了一个特殊的窗口函数。注意这里只能调用.apply(),没有其他替代的方法。

    传入的JoinFunction也是一个函数类接口,使用时需要实现内部的.join()方法。这个方法有两个参数,分别表示两条流中成对匹配的数据

    这里需要注意,JoinFunciton并不是真正的“窗口函数”,它只是定义了窗口函数在调用时对匹配数据的具体处理逻辑。

    当然,既然是窗口计算,在.window()和.apply()之间也可以调用可选API去做一些自定义,比如用.trigger()定义触发器,用.allowedLateness()定义允许延迟时间,等等。

    2. 窗口联结的处理流程

    JoinFunction中的两个参数,分别代表了两条流中的匹配的数据。这里就会有一个问题:什么时候就会匹配好数据,调用.join()方法呢?接下来我们就来介绍一下窗口join的具体处理流程。

    两条流的数据到来之后,首先会按照key分组、进入对应的窗口中存储;当到达窗口结束时间时,算子会先统计出窗口内两条流的数据的所有组合,也就是对两条流中的数据做一个笛卡尔积(相当于表的交叉连接,cross join),然后进行遍历,把每一对匹配的数据,作为参数(first,second)传入JoinFunction的.join()方法进行计算处理,得到的结果直接输出如图8-8所示。所以窗口中每有一对数据成功联结匹配,JoinFunction的.join()方法就会被调用一次,并输出一个结果。

    在这里插入图片描述

    除了JoinFunction,在.apply()方法中还可以传入FlatJoinFunction,用法非常类似,只是内部需要实现的.join()方法没有返回值。结果的输出是通过收集器(Collector)来实现的,所以对于一对匹配数据可以输出任意条结果。

    只有两条流中数据按key配对成功的那些;如果某个窗口中一条流的数据没有任何另一条流的数据匹配,那么就不会调用JoinFunction的.join()方法

    3. 窗口联结实例

    在电商网站中,往往需要统计用户不同行为之间的转化,这就需要对不同的行为数据流,按照用户ID进行分组后再合并,以分析它们之间的关联。如果这些是以固定时间周期(比如1小时)来统计的,那我们就可以使用窗口join来实现这样的需求。

    4、编码
    package com.example.chapter08;
    
    import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
    import org.apache.flink.api.common.eventtime.WatermarkStrategy;
    import org.apache.flink.api.common.functions.JoinFunction;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
    import org.apache.flink.streaming.api.windowing.time.Time;
    
    import java.time.Duration;
    
    
    /**
     * 双流
     * 窗口连接
     * (Window Join)
     */
    public class WindowJoinTest {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
            //来自APP的支付日志
            SingleOutputStreamOperator<Tuple2<String, Long>> stream1 = env.fromElements(
                    Tuple2.of("a", 1000L),
                    Tuple2.of("b", 1000L),
                    Tuple2.of("a", 2000L),
                    Tuple2.of("b", 2000L)
            ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ZERO)
                    .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                        @Override
                        public long extractTimestamp(Tuple2<String, Long> element, long l) {
                            return element.f1;
                        }
                    })
            );
    
    
            //来自APP的支付日志
            SingleOutputStreamOperator<Tuple2<String, Integer>> stream2 = env.fromElements(
                    Tuple2.of("a", 3000),
                    Tuple2.of("b", 4000),
                    Tuple2.of("a", 4500),
                    Tuple2.of("b", 5500)
            ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Integer>>forBoundedOutOfOrderness(Duration.ZERO)
                    .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Integer>>() {
                        @Override
                        public long extractTimestamp(Tuple2<String, Integer> element, long l) {
                            return element.f1;
                        }
                    })
            );
    
    
            /**
             *  输出结果
             * (a,1000)->(a,3000)
             * (a,1000)->(a,4500)
             * (a,2000)->(a,3000)
             * (a,2000)->(a,4500)
             * (b,1000)->(b,4000)
             * (b,2000)->(b,4000)
             */
            stream1.join(stream2)
                    .where(data -> data.f0)
                    .equalTo(data -> data.f0)
                    .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                    .apply(new JoinFunction<Tuple2<String, Long>, Tuple2<String, Integer>, String>() {
    
                        @Override
                        public String join(Tuple2<String, Long> first, Tuple2<String, Integer> second) throws Exception {
                            return first + "->" + second;
                        }
                    })
                    .print();
    
            env.execute();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83

    5、间隔联结(Interval Join)

    在有些场景下,我们要处理的时间间隔可能并不是固定的。比如,在交易系统中,需要实时地对每一笔交易进行核验,保证两个账户转入转出数额相等,也就是所谓的“实时对账”。两次转账的数据可能写入了不同的日志流,它们的时间戳应该相差不大,所以我们可以考虑只统计一段时间内是否有出账入账的数据匹配。这时显然不应该用滚动窗口或滑动窗口来处理——因为匹配的两个数据有可能刚好“卡在”窗口边缘两侧,于是窗口内就都没有匹配了;会话窗口虽然时间不固定,但也明显不适合这个场景。 基于时间的窗口联结已经无能为力了。

    为了应对这样的需求,Flink提供了一种叫作“间隔联结”(interval join)的合流操作。顾名思义,间隔联结的思路就是针对一条流的每个数据,开辟出其时间戳前后的一段时间间隔,看这期间是否有来自另一条流的数据匹配。

    1. 间隔联结的原理

    间隔联结具体的定义方式是,我们给定两个时间点,分别叫作间隔的“上界”(upperBound)和“下界”(lowerBound);于是对于一条流(不妨叫作A)中的任意一个数据元素a,就可以开辟一段时间间隔:[a.timestamp + lowerBound, a.timestamp + upperBound],即以a的时间戳为中心,下至下界点、上至上界点的一个闭区间:我们就把这段时间作为可以匹配另一条流数据的“窗口”范围。所以对于另一条流(不妨叫B)中的数据元素b,如果它的时间戳落在了这个区间范围内,a和b就可以成功配对,进而进行计算输出结果。所以匹配的条件为:

    a.timestamp + lowerBound <= b.timestamp <= a.timestamp + upperBound
    这里需要注意,做间隔联结的两条流A和B,也必须基于相同的key;下界lowerBound应该小于等于上界upperBound,两者都可正可负;间隔联结目前只支持事件时间语义。

    在这里插入图片描述

    下方的流A去间隔联结上方的流B,所以基于A的每个数据元素,都可以开辟一个间隔区间。我们这里设置下界为-2毫秒,上界为1毫秒。于是对于时间戳为2的A中元素,它的可匹配区间就是[0, 3],流B中有时间戳为0、1的两个元素落在这个范围内,所以就可以得到匹配数据对(2, 0)和(2, 1)。同样地,A中时间戳为3的元素,可匹配区间为[1, 4],B中只有时间戳为1的一个数据可以匹配,于是得到匹配数据对(3, 1)。

    所以我们可以看到,间隔联结同样是一种内连接(inner join)。与窗口联结不同的是,interval join做匹配的时间段是基于流中数据的,所以并不确定;而且流B中的数据可以不只在一个区间内被匹配。

    2. 间隔联结的调用

    间隔联结在代码中,是基于KeyedStream的联结(join)操作。DataStream在keyBy得到KeyedStream之后,可以调用.intervalJoin()来合并两条流,传入的参数同样是一个KeyedStream,两者的key类型应该一致;得到的是一个IntervalJoin类型。后续的操作同样是完全固定的:先通过.between()方法指定间隔的上下界,再调用.process()方法,定义对匹配数据对的处理操作。调用.process()需要传入一个处理函数,这是处理函数家族的最后一员:“处理联结函数”

    3、编码

    package com.example.chapter08;
    
    import com.example.chapter05.Event;
    import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
    import org.apache.flink.api.common.eventtime.WatermarkStrategy;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
    import org.apache.flink.streaming.api.windowing.time.Time;
    import org.apache.flink.util.Collector;
    
    import java.time.Duration;
    
    /**
     *我们有两条流,一条是下订单的流,一条是浏览数据的流。我们可以针对同一个用户,
     * 来做这样一个联结。也就是使用一个用户的下订单的事件和这个用户的最近十分钟的浏览数据进行一个联结查询。
     */
    public class IntervalJoinTest {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
    
            //来自APP的支付日志
            SingleOutputStreamOperator<Tuple2<String, Long>> orderStream = env.fromElements(
                    Tuple2.of("Mary", 5000L),
                    Tuple2.of("Alice", 5000L),
                    Tuple2.of("Bob", 20000L),
                    Tuple2.of("Alice", 20000L),
                    Tuple2.of("Cary", 51000L)
    
            ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ZERO)
                    .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                        @Override
                        public long extractTimestamp(Tuple2<String, Long> element, long l) {
                            return element.f1;
                        }
                    })
            );
    
    
            //来自APP的支付日志
            SingleOutputStreamOperator<Event> clickStream = env.fromElements(
                    new Event("Bob", "./cart", 2000L),
                    new Event("Alice", "./prod?id=100", 3000L),
                    new Event("Alice", "./prod?id=200", 3500L),
                    new Event("Bob", "./prod?id=2", 2500L),
                    new Event("Alice", "./prod?id=300", 36000L),
                    new Event("Bob", "./home", 30000L),
                    new Event("Bob", "./prod?id=1", 23000L),
                    new Event("Bob", "./prod?id=3", 33000L)
            ).assignTimestampsAndWatermarks(WatermarkStrategy.<Event>forBoundedOutOfOrderness(Duration.ZERO)
                    .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                        @Override
                        public long extractTimestamp(Event event, long l) {
                            return event.timestamp;
                        }
                    })
            );
    
            /**
             * .between(Time.seconds(-5),Time.seconds(10)
             * 之前5秒 之后 10秒
             */
            orderStream.keyBy(data -> data.f0)
                    .intervalJoin(clickStream.keyBy(data -> data.user)
                    ).between(Time.seconds(-5), Time.seconds(10))
                    .process(new ProcessJoinFunction<Tuple2<String, Long>, Event, String>() {
    
                        @Override
                        public void processElement(Tuple2<String, Long> left, Event right, ProcessJoinFunction<Tuple2<String, Long>, Event, String>.Context ctx, Collector<String> out) throws Exception {
                            out.collect(right + " => " + left);
                        }
                    }).print();
    
            env.execute();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    6、窗口同组联结(Window CoGroup)

    除窗口联结和间隔联结之外,Flink还提供了一个“窗口同组联结”(window coGroup)操作。它的用法跟window join非常类似,也是将两条流合并之后开窗处理匹配的元素,调用时只需要将.join()换为.coGroup()就可以了。

    stream1.coGroup(stream2)
        .where(<KeySelector>)
        .equalTo(<KeySelector>)
        .window(TumblingEventTimeWindows.of(Time.hours(1)))
        .apply(<CoGroupFunction>)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    与window join的区别在于,调用.apply()方法定义具体操作时,传入的是一个CoGroupFunction。这也是一个函数类接口,源码中定义如下:

    public interface CoGroupFunction<IN1, IN2, O> extends Function, Serializable {
        void coGroup(Iterable<IN1> first, Iterable<IN2> second, Collector<O> out) throws Exception;
    }
    
    • 1
    • 2
    • 3

    内部的.coGroup()方法,有些类似于FlatJoinFunction中.join()的形式,同样有三个参数,分别代表两条流中的数据以及用于输出的收集器(Collector)。不同的是,这里的前两个参数不再是单独的每一组“配对”数据了,而是传入了可遍历的数据集合。也就是说,现在不会再去计算窗口中两条流数据集的笛卡尔积,而是直接把收集到的所有数据一次性传入,至于要怎样配对完全是自定义的。这样.coGroup()方法只会被调用一次,而且即使一条流的数据没有任何另一条流的数据匹配,也可以出现在集合中、当然也可以定义输出结果了。

    所以能够看出,coGroup操作比窗口的join更加通用,不仅可以实现类似SQL中的“内连接”(inner join),也可以实现左外连接(left outer join)、右外连接(right outer join)和全外连接(full outer join)。事实上,窗口join的底层,也是通过coGroup来实现的。

    1. 编码

    package com.example.chapter08;
    
    import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
    import org.apache.flink.api.common.eventtime.WatermarkStrategy;
    import org.apache.flink.api.common.functions.CoGroupFunction;
    import org.apache.flink.api.common.functions.JoinFunction;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
    import org.apache.flink.streaming.api.windowing.time.Time;
    import org.apache.flink.util.Collector;
    
    import java.time.Duration;
    
    
    /**
     * 更加通用、可以实现各种链接
     * 双流join
     * coGroupTest
     * 窗口同组链接 可以看作更加通用的windowJoin
     */
    public class CoGroupTest {
        public static void main(String[] args) throws Exception {
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
            env.setParallelism(1);
    
            //来自APP的支付日志
            SingleOutputStreamOperator<Tuple2<String, Long>> stream1 = env.fromElements(
                    Tuple2.of("a", 1000L),
                    Tuple2.of("b", 1000L),
                    Tuple2.of("a", 2000L),
                    Tuple2.of("b", 2000L)
            ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ZERO)
                    .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                @Override
                public long extractTimestamp(Tuple2<String, Long> element, long l) {
                    return element.f1;
                }
            }));
    
    
            //来自APP的支付日志
            SingleOutputStreamOperator<Tuple2<String, Integer>> stream2 = env.fromElements(
                    Tuple2.of("a", 3000),
                    Tuple2.of("b", 4000),
                    Tuple2.of("a", 4500),
                    Tuple2.of("b", 5500))
                    .assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Integer>>forBoundedOutOfOrderness(Duration.ZERO).withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Integer>>() {
                @Override
                public long extractTimestamp(Tuple2<String, Integer> element, long l) {
                    return element.f1;
                }
            }));
    
    
            stream1.coGroup(stream2)
                    .where(data -> data.f0)
                    .equalTo(data -> data.f0)
                    .window(TumblingEventTimeWindows
                            .of(Time.seconds(5)))
                    .apply(new CoGroupFunction<Tuple2<String, Long>, Tuple2<String, Integer>, String>() {
                @Override
                public void coGroup(Iterable<Tuple2<String, Long>> first, Iterable<Tuple2<String, Integer>> second, Collector<String> out) throws Exception {
                    out.collect(first + "=> " + second);
                }
            }).print();
    
    
            env.execute();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74

    三、本章总结

    多流转换是流处理在实际应用中常见的需求,主要包括分流和合流两大类,本章分别做了详细讲解。在Flink中,分流操作可以通过处理函数的侧输出流(side output)很容易地实现;而合流则提供不同层级的各种API。

    最基本的合流方式是联合(union)和连接(connect),两者的主要区别在于union可以对多条流进行合并,数据类型必须一致;而connect只能连接两条流,数据类型可以不同。事实上connect提供了最底层的处理函数(process function)接口,可以通过状态和定时器实现任意自定义的合流操作,所以是最为通用的合流方式。

    除此之外,Flink还提供了内置的几个联结(join)操作,它们都是基于某个时间段的双流合并,是需求特化之后的高层级API。主要包括窗口联结(window join)、间隔联结(interval join)和窗口同组联结(window coGroup)。其中window join和coGroup都是基于时间窗口的操作,窗口分配器的定义与之前介绍的相同,而窗口函数则被限定为一种,通过.apply()来调用;interval join则与窗口无关,而是基于每个数据元素截取对应的一个时间段来做联结,最终的处理操作则需调用.process(),由处理函数ProcessJoinFunction实现
    可以看到,基于时间的联结操作的每一步操作都是固定的接口,并没有其他变化,使用起来“专项专用”,非常方便。

  • 相关阅读:
    miniob源码 架构概览
    VLAN 通信过程与特性
    数据治理:为什么不见BI作关联分析
    【JavaEE】Servlet实战案例:表白墙网页实现
    使用java计算crc校验和
    使用nsenter调试k8s网络
    微信小程序学习(十):生命周期
    【Python游戏】用Python实现一个测试你智商的小游戏——24点,过不了三关的都是细狗 | 附带源码
    京东数据分析:2023年9月京东白酒行业品牌销售排行榜
    【01】弄懂共识机制PoW
  • 原文地址:https://blog.csdn.net/qq_42082701/article/details/126759723