Flink多流转换

无论是基本的简单转换和聚合,还是基于窗口的计算,我们都是针对某一条流上的数据进行处理。而在实际应用中,可能将多条流合并为一条流,也可能将一条流分为多条。

分流

使用处理函数的侧输出流。处理函数本身可以认为是一个转换算子,其输出类型单一,仍然是DataStream;然而侧输出流却能不受限制的任意自定义输出数据,它们就像从”主流“拆分下来的"支流"。

需要调用上下文ctx的output()方法,就可以输出任意类型的数据了。而侧数据流的标记和提取都离不开一个输出标签(OutputTag)

例:通浏览数据的用户名称进行拆分,分别拣选Mary、Bob的浏览事件。保留基本信息,剩余事件则直接输出到主流,类型依然保留Event

import com.yingzi.chapter05.Source.ClickSource;
import com.yingzi.chapter05.Source.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 element, long recordTimestamp) {
                                return element.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> processedStream = stream.process(new ProcessFunction<Event, Event>() {
            @Override
            public void processElement(Event value, 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);
            }
        });

        processedStream.print("else");
        processedStream.getSideOutput(maryTag).print("Mary");
        processedStream.getSideOutput(bobTag).print("Bob");

        env.execute();
    }

}

基本合流操作

联合(Union)

最简单的合流操作,就是直接将多条流合在一起,联合操作要求流中的数据类型必须相同,合并之后的新流会包括所有流中的元素,数据类型不变。

代码实现上,基于DataStream直接调用union()方法,传入其他DataStream作为参数,得到结果仍然是DataStream类型

stream.union(stream2,stream3,...)

思考一个问题?在事件时间语义下,水位线是时间的进度标志,那么不同流中的水位线可能不一样,那么合并之后以谁为准呢?

回到水位线的本质含义:“之前的所有数据已经到齐了”。故答案显而易见,是选择最小的那个为准。这与之前介绍的并行任务水位线传递的规则一致,多条流的合并,某种意义上也可以看作是多个并行任务向同一个下游任务汇合的过程。

import com.yingzi.chapter05.Source.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;

public class UnionTest {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<Event> stream1 = env.socketTextStream("hadoop102", 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 element, long recordTimestamp) {
                                return element.timestamp;
                            }
                        })
                );
        stream1.print("stream1");

        SingleOutputStreamOperator<Event> stream2 = env.socketTextStream("hadoop103", 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))
                        .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                            @Override
                            public long extractTimestamp(Event element, long recordTimestamp) {
                                return element.timestamp;
                            }
                        })
                );
        stream2.print("stream2");

        // 合并两条流
        stream1.union(stream2)
                .process(new ProcessFunction<Event, String>() {
                    @Override
                    public void processElement(Event value, Context ctx, Collector<String> out) throws Exception {
                        out.collect("水位线:" + ctx.timerService().currentWatermark());
                    }
                }).print();

        env.execute();
    }
}

这里创建了两条流来读取socket文本数据,并从数据中提取时间戳作为生成水位线的依据。现在我们来分析一下程序运行过程中水位线的情况。

在Flink的流开始处,为两条插入Long.MIN_VALUE的水位线,合流后的最小水位线为Long.MIN_VALUE

插入第一条socket文本流输入数据据[Alice, ./home, 1000] 时,水位线不会立即改变,只有到水位线生成周期的时间点(200ms 一次)才会推进到 1000 - 1 = 999 毫秒。此时两条流的最小值仍为Long.MIN_VALUE,故合并水位线还是Long.MIN_VALUE

接着在第二条sockett 文本流输入数据[Alice, ./home, 2000],那么第二条流的水位线会随之推进到 2000 – 1 = 1999 毫秒。两条流水位线取最小值,此时合并水位线为999

再接着,我们再向第一条输入数据[Alice, ./home, 3000],,那么第二条流的水位线会随之推进到 3000 – 1 = 2999 毫秒。两条流水位线取最小值,此时合并水位线为1999

连接(Connect)

将两条流像线一样对接起来

1、连接流(ConnectedStreams)

处理更加灵活,连接操作允许流的数据类型不同。DataStream中的数据只能有唯一的类型,故连接得到的并不是DataStream,而是一个连接流(ConnectedStreams),其可以看成是两条流形式上的统一,在同一条流中却在内部仍然保持着各自的数据形式不变,彼此独立。若要得到新的DataStream,可进一步定义一个同处理(co-process)转换操作,用来说明对于不同来源、不同类型的数据怎,如何进行处理转换、得到统一的输出类型。

代码实现上,基于一条DataStream调用connect()方法,传入另外一条DataStream作为参数,将两条流连接起来,得到一个ConnectedStreams;然后再调用同处理方法得到DataStream,这里可以调用的同处理方法有map()、flatMap()、process()方法

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

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, 7L);

        stream2.connect(stream1)
                .map(new CoMapFunction<Long, Integer, String >() {
                    @Override
                    public String map1(Long value) throws Exception {
                        return "Long:" + value.toString();
                    }

                    @Override
                    public String map2(Integer value) throws Exception {
                        return "Integer:" + value.toString();
                    }
                }).print();

        env.execute();
    }
}

ConnectedStreams有两个类型参数,分别表示内部包含的两条流各自的数据类型,此时调用的map()方法内部传入的是一个CoMapFunction,表示将分别对两条流中的数据执行map操作

public interface CoMapFunction<IN1, IN2, OUT> extends Function, Serializable {
	OUT map1(IN1 value) throws Exception;
    OUT map2(IN2 value) throws Exception;
}

另外,ConnectedStreams也可以直接调用keyBy()进行按键分区,得到还是一个ConnectedStreams

connectedStreams.keyBy(keySelector1, keySelector2);

传入的两个参数keySelector1和keySelector2是两条流中各自的键选择器,当然也可以直接传入键的位置值(keyPosition)或者键的字段名(field),与普通的keyBy用法完全一致,注意:两条流定义的键的类型必须相同,否则会抛出异常

两条流的连接(connect)与联合(union)操作相比,最大的优势就是可以处理不同类型流的合并,使用更加灵活、应用更加广泛。当然受到限制的是合并流数量只能是2,而union可以同时进行多条流合并。

2、CoProcessFunction

对于连接流ConnectedStreams的处理操作,需要分别定义对两条流的处理转换,因此接口中就会有两个相同的方法需要实现,在两条流中的数据到来时分别调用,我们把这种接口叫做协同处理函数(co-process function),与CoMapFunction类似,如果是调用flatMap()就需要传入一个CoFlatMapFunction,需要实现flatMap1()、flatMap2()两个方法;调用CoProcessFunction

public abstract class CoProcessFunction<IN1, IN2, OUT> extends AbstractRichFunction {
    public abstract void processElement1(IN1 value, Context ctx, Collector<OUT> out) throws Exception;
    public abstract void processElement2(IN2 value, Context ctx, Collector<OUT> out) throws Exception;
    public void onTimer(long timestamp, OnTimerContext ctx, Collector<OUT> out) throws Exception {}
    public abstract class Context{...}

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

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

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;

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.ofSeconds(2))
                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Long>>() {
                    @Override
                    public long extractTimestamp(Tuple3<String, String, Long> element, long recordTimestamp) {
                        return element.f2;
                    }
                }));

        //来自第三方支付平台的支付日志
        SingleOutputStreamOperator<Tuple4<String, String, String, Long>> thirdpartyStream = env.fromElements(
                Tuple4.of("order-1", "third-party", "success", 3000L),
                Tuple4.of("order-2", "third-party", "success", 4000L)
        ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple4<String, String, String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple4<String, String, String, Long>>() {
                    @Override
                    public long extractTimestamp(Tuple4<String, String, String, Long> element, long recordTimestamp) {
                        return element.f3;
                    }
                }));

        //检测同一支付单在两条流中是否匹配,不匹配就报警
//        appStream.keyBy(data -> data.f0)
//                .connect(thirdpartyStream.keyBy(data -> data.f0));

        appStream.connect(thirdpartyStream)
                .keyBy(data -> data.f0, data -> data.f0)
                .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;

        @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.LONG))
            );
            thirdPartyEventState = getRuntimeContext().getState(
                    new ValueStateDescriptor<Tuple4<String, String, String, Long>>("thirdparty-event", Types.TUPLE(Types.STRING, Types.STRING, Types.STRING, Types.LONG))
            );
        }

        @Override
        public void processElement1(Tuple3<String, String, Long> value, Context ctx, Collector<String> out) throws Exception {
            //来的是third-party,看另一条流中事件是否来过
            if (thirdPartyEventState.value() != null) {
                out.collect("对账成功" + value + " " + thirdPartyEventState.value());
                //清空状态
                thirdPartyEventState.clear();
            } else {
                //更新状态
                appEventState.update(value);
                //注册一个5秒后的定时器,开始等待另一条流的事件
                ctx.timerService().registerEventTimeTimer(value.f2 + 5000L);
            }
        }

        @Override
        public void processElement2(Tuple4<String, String, String, Long> value, 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 + 5000L);
            }
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
            //定时器触发,判断状态,如果某个状态部为空,说明另一条中事件没来
            if (appEventState.value() != null) {
                out.collect("对账失败:" + appEventState.value() + " " + " 第三方支付平台信息未到");
            }
            if (thirdPartyEventState.value() != null) {
                out.collect("对账失败:" + thirdPartyEventState.value() + " " + "app信息未到");
            }
            appEventState.clear();
            thirdPartyEventState.clear();
        }
    }
}

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

3、广播连接流(BroadcastConnectedStream)

在DataStream调用connect()方法时,传入的参数也可以不是一个DataStream,而是一个广播流(BroadcastStream),这时合并两条流得到的就变成了一个广播连接流(BroadcastConnectedStream),这种连接方式往往需要动态定义某些规则或配置。因为规则是实时变动的,所以我们可以用一个单独的流来获取规则数据,而这些规则是对整个应用全局有效的,所以不能只把这些数据传递给一个下游并行子任务处理,而是要广播给所有的并行子任务。下游子任务收到广播出来的规则后会将其保存成一个状态,这就是所谓的广播状态(broadcast state)

广播状态底层是用一个映射(map)结构来保存的,在代码实现上,可以直接调用DataStream的broadcast()方法,传入一个映射状态描述器(MapStateDescriptor)说明状态的名称和类型,就可以得到规则数据的广播流(BroadcastStream)

MapStateDescriptor<String, Rule> ruleStateDescriptor = new MapStateDescriptor<>(...);
BroadcastStream<Rule> ruleBroadcastStream = ruleStream.broadcast(ruleStateDescriptor);

基于BroadcastConnectedStream调用process()就可以同时获取规则和数据,进行动态处理了。传入的参数也是处理函数大家族中一员——如果对数据流调用keyBy进行了按键分区,那么要传入的就是KeyedBroadcastProcessFunction;如果没有按键分区,就传入BroadcastProcessFunction

DataStream<String> output = stream
	.connect(ruleBroadcastStream)
	.process( new BroadcastProcessFunction<>() {...} );

BroadcastProcessFunction是一个抽象类,需要实现两个方法,针对合并的两条流中元素分别定义处理操作。区别在于这里一条流式正常处理的数据,而另一条流则是新规则来更新广播状态,所以对应的两个方法叫作processElement()和processBroadcastElement()

public abstract class BroadcastProcessFunction<IN1, IN2, OUT> extends
    BaseBroadcastProcessFunction {
    ...
     public abstract void processElement(IN1 value, ReadOnlyContext ctx, Collector<OUT> out) throws Exception;
     public abstract void processBroadcastElement(IN2 value, Context ctx, Collector<OUT> out) throws Exception;
    ...
}

基于时间的合流——双流联结(Join)

SQL中join一般会翻译为连接;我们这里为了区分不用的算子,一般的合流操作connect翻译为连接,而把join翻译为联结

窗口联结(Window Join)
1、窗口联结的调用

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

stream1.join(stream2)
 .where(<KeySelector>)
 .equalTo(<KeySelector>)
 .window(<WindowAssigner>)
 .apply(<JoinFunction>)

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

这里的window()传入的就是窗口分配器,之前讲的三种时间窗口都可以在这里使用:滚动窗口(tumbling window)、滑动窗口(sliding window)、会话窗口(session window)

在window()和apply()之间也可以调用可选的API去做一些自定义,比如用trigger()定义触发器,用allowedLateness()定义允许延迟时间。

后面调用的apply()可以看作是实现了一个特殊的窗口函数,注意这里只能调用apply(),没有其他替代的方法,传入的JoinFunction也是一个函数类接口,使用时需要实现内部的join()方法。JoinFunction又不是一个真正的窗口函数,它只是定义了窗口函数在调用时对匹配数据的具体处理逻辑。

public interface JoinFunction<IN1, IN2, OUT> extends Function, Serializable {
 	OUT join(IN1 first, IN2 second) throws Exception;
}
2、窗口联结的处理流程

两条流的数据到来之后,首先会按照key分组、进入到对应的窗口中存储;当到达窗口结束时间时,算子会先统计会先统计出窗口内两条流的数据的所有组合,也就是对两条流中的数据做一个笛卡尔积,然后进行遍历,把每一对匹配的数据作为参数传入JoinFunction的join()方法计算处理。

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

窗口jion的调用语法和我们熟悉的SQL中表的join非常相似:

SELECT * FROM table t1,table t2 WHERE t1.id = t2.id;

这句SQL中where字句的表达等价于:inner join … on,所以本身表示的是两张表基于id的内连接。而Flink中的window join,同样类似于inner join。也就是说,最后处理输出,只有两条流中数据按key配对成功。如果一条流的数据没有任何另一条流的数据匹配,那么就不会调用JoinFunction的join()方法,也就没有任何任何输出了。

3、窗口联结实例

例子:统计用户不同行为之间的转化,这就需要对不同的行为数据流按照用户ID进行分组后再合并,以很细它们之间的关联,如果这些是以固定时间周期来统计的,那么我们就可以使用窗口join来实现这样的需求。

public class WindowJoinExample {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStream<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>>forMonotonousTimestamps().withTimestampAssigner(
                                new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                                    @Override
                                    public long extractTimestamp(Tuple2<String,
                                            Long> stringLongTuple2, long l) {
                                        return stringLongTuple2.f1;
                                    }
                                }
                        )
                );
        DataStream<Tuple2<String, Long>> stream2 = env.fromElements(
                Tuple2.of("a", 3000L),
                Tuple2.of("b", 3000L),
                Tuple2.of("a", 4000L),
                Tuple2.of("b", 4000L)
        ).assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<Tuple2<String, Long>>forMonotonousTimestamps().withTimestampAssigner(
                        new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                            @Override
                            public long extractTimestamp(Tuple2<String,
                                    Long> stringLongTuple2, long l) {
                                return stringLongTuple2.f1;
                            }
                        }
                )
        );
        stream1.join(stream2)
                .where(r -> r.f0)
                .equalTo(r -> r.f0)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .apply(new JoinFunction<Tuple2<String, Long>, Tuple2<String, Long>, String>() {
                    @Override
                    public String join(Tuple2<String, Long> left, Tuple2<String,
                            Long> right) throws Exception {
                        return left + "=>" + right;
                    }
                })
                .print();
        env.execute();
        
    }
}
间隔联结(Interval Join)

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

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

1、间隔联结的原理

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

a.timestamp + lowerBound <= b.timestamp <= a.timestamp + upperBound

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

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

2、间隔联结的调用

间隔联结的代码中,是基于KeyedStream的连接操作,DataStream在keyBy得到KeyedStream之后,可以调用intervalJoin类型。后续的操作同样是完全固定的:先通过between()方法指定间隔的上下界,再调用process()方法,定义匹配数据对的处理操作。

抽象类 ProcessJoinFunction 就像是 ProcessFunction 和 JoinFunction 的结合,内部同样有一个抽象方法.processElement()。与其他处理函数不同的是,它多了一个参数,这自然是因为有来自两条流的数据。参数中 left 指的就是第一条流中的数据,right 则是第二条流中与它匹配的数据。每当检测到一组匹配,就会调用这里的.processElement()方法,经处理转换 之后输出结果

stream1
     .keyBy(<KeySelector>)
     .intervalJoin(stream2.keyBy(<KeySelector>))
     .between(Time.milliseconds(-2), Time.milliseconds(1))
     .process (new ProcessJoinFunction<Integer, Integer, String(){
     @Override
     public void processElement(Integer left, Integer right, Context ctx,Collector<String> out) {
     	out.collect(left + "," + right);
     }
 });
3、间隔联结实例

例子:

import com.yingzi.chapter05.Source.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.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

public class IntervalJoinExample {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<Tuple3<String, String, Long>> orderStream = env.fromElements(
                Tuple3.of("Mary", "order-1", 5000L),
                Tuple3.of("Alice", "order-2", 5000L),
                Tuple3.of("Bob", "order-3", 20000L),
                Tuple3.of("Alice", "order-4", 20000L),
                Tuple3.of("Cary", "order-5", 51000L)
        ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple3<String,
                String, Long>>forMonotonousTimestamps()
                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Long>>() {
                    @Override
                    public long extractTimestamp(Tuple3<String, String, Long> element, long recordTimestamp) {
                        return element.f2;
                    }
                })
        );
        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>forMonotonousTimestamps()
                .withTimestampAssigner(new SerializableTimestampAssigner<Event>() {
                    @Override
                    public long extractTimestamp(Event element, long recordTimestamp) {
                        return element.timestamp;
                    }
                })
        );
        orderStream.keyBy(data -> data.f0)
                .intervalJoin(clickStream.keyBy(data -> data.user))
                .between(Time.seconds(-5), Time.seconds(10))
                .process(new ProcessJoinFunction<Tuple3<String, String, Long>, Event, String>() {
                    @Override
                    public void processElement(Tuple3<String, String, Long> left, Event right, Context ctx, Collector<String> out) throws Exception {
                        out.collect(right + " => " + left);
                    }
                })
                .print();
        env.execute();
    }
}
窗口同组联结(Window CoGroup)

用法跟window join非常类似,也就是两条流合并之后开窗处理匹配的元素,调用时只需要将join()换为coGroup()即可

stream1.coGroup(stream2)
     .where(<KeySelector>)
     .equalTo(<KeySelector>)
     .window(TumblingEventTimeWindows.of(Time.hours(1)))
     .apply(<CoGroupFunction>)

调用apply()方法传入一个CoGroupFunction,这是一个函数类接口,源码如下:

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

传入可遍历的数据集合,现在不会再去计算窗口中两条数据集的笛卡尔积,而是直接把收集到的所有数据一次性传入,配对方式完全自定义。coGroup()方法只会被调用一次,而且即使一条流的数据没有被另一条数据匹配,也可以出现在集合中,当然也可以定义输出结果了。

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

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.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;

public class CoGroupTest {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<Tuple2<String, Long>> stream1 = env.fromElements(
                Tuple2.of("a", 1000L),
                Tuple2.of("b", 1000L),
                Tuple2.of("a", 2000L),
                Tuple2.of("b", 2000L),
                Tuple2.of("b",5100L)
        ).assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(2))
                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                    @Override
                    public long extractTimestamp(Tuple2<String, Long> element, long recordTimestamp) {
                        return element.f1;
                    }
                }));


        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.ofSeconds(2))
                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Integer>>() {
                    @Override
                    public long extractTimestamp(Tuple2<String, Integer> element, long recordTimestamp) {
                        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();
    }
}

总结

多流转换操作时流处理在实际应用中常见的需求,主要包括合流和分流两大类。在Flink中,分流操作可以通过处理函数的侧输出流(side output)实现;而合流则提供不同层级的各种API

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

Flink还提供了内置的几个联结操作,基于某个时间段的双流合并,是需求特化之后的高层级API:窗口联结(window join)、间隔联结(interval join)、窗口同组联结(window coGroup)。其中 window join 和 coGroup 都是基于时间窗口的操作, 窗口分配器的定义与之前介绍的相同,而窗口函数则被限定为一种,通过.apply()来调用; interval join 则与窗口无关,而是基于每个数据元素截取对应的一个时间段来做联结,最终的处理操作则需调用.process(),由处理函数 ProcessJoinFunction 实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值