CoProcessFunction
对于两条输入流,DataStream API提供了CoProcessFunction这样的low-level操作。CoProcessFunction提供了操作每一个输入流的方法: processElement1()和processElement2()。
类似于ProcessFunction,这两种方法都通过Context对象来调用。这个Context对象可以访问事件数据,定时器时间戳,TimerService,以及side outputs。CoProcessFunction也提供了onTimer()回调函数。下面的例子展示了如何使用CoProcessFunction来合并两条流。
实现低阶join通常遵循此套路:
1.为一个(或两个)输入创建一个状态对象。
2.当从输入源收到元素时,更新状态。
3.从另一个输入接收元素后,检索状态并生成连接的结果。
实例
根据id将两个流中的数据匹配在一起组合成新的流数据,默认两个流的最大延迟时间为60s。超过60s还未匹配成功,意味着当前只有一个流来临,则任务流信息异常,需要将数据侧流输出。
// 流1 要先按照id分组 DataStreamSource<String> sourceStream1 = env.addSource(consumer); KeyedStream<String, Tuple> stream1 = sourceStream1.keyBy(1); // 流2 要先按照id分组 DataStreamSource<String> sourceStream2 = env.addSource(consumer); KeyedStream<String, Tuple> stream2 = sourceStream1.keyBy(1); // 定义两个侧切流的outputTag OutputTag<String> outputTag1 = new OutputTag<>("stream1"); OutputTag<String> outputTag2 = new OutputTag<>("stream2");
stream1.connect(stream2).process(new CoProcessFunction<String, String, Tuple2<String, String>>() { // 流1的状态 ValueState<String> state1; // 流2的状态 ValueState<String> state2; // 定义一个用于删除定时器的状态 ValueState<Long> timeState; @Override public void open(Configuration parameters) throws Exception { super.open(parameters); // 初始化状态 state1 = getRuntimeContext().getState(new ValueStateDescriptor<>("state1", String.class)); state2 = getRuntimeContext().getState(new ValueStateDescriptor<>("state2", String.class)); timeState = getRuntimeContext().getState(new ValueStateDescriptor<>("timeState", Long.class)); } // 流1的处理逻辑 @Override public void processElement1(String value, Context ctx, Collector<Tuple2<String, String>> out) throws Exception { String value2 = state2.value(); // 流2不为空表示流2先来了,直接将两个流拼接发到下游 if (value2 != null) { out.collect(Tuple2.of(value, value2)); // 清空流2对用的state信息 state2.clear(); // 流2来了就可以删除定时器了,并把定时器的状态清除 ctx.timerService().deleteEventTimeTimer(timeState.value()); timeState.clear(); } else { // 流2还没来,将流1放入state1中, state1.update(value); // 并注册一个1分钟的定时器,流1中的 eventTime + 60s long time = 1111L + 60000; timeState.update(time); ctx.timerService().registerEventTimeTimer(time); } } // 流2的处理逻辑与流1的处理逻辑类似 @Override public void processElement2(String value, Context ctx, Collector<Tuple2<String, String>> out) throws Exception { String value1 = state1.value(); if (value1 != null) { out.collect(Tuple2.of(value1, value)); state1.clear(); ctx.timerService().deleteEventTimeTimer(timeState.value()); timeState.clear(); } else { state2.update(value); long time = 1111L + 60000; timeState.update(time); ctx.timerService().registerEventTimeTimer(time); } } @Override public void onTimer(long timestamp, OnTimerContext ctx, Collector<Tuple2<String, String>> out) throws Exception { super.onTimer(timestamp, ctx, out); // 定时器触发了,即1分钟内没有收到两个流 // 流1不为空,则将流1侧切输出 if (state1.value() != null) { ctx.output(outputTag1, state1.value()); } // 流2不为空,则将流2侧切输出 if (state2.value() != null) { ctx.output(outputTag2, state2.value()); } state1.clear(); state2.clear(); } });
注意:整体的逻辑思路是:
流1先来,先把流1保存进流1的状态;
流2先来,先把流2保存进流2的状态;
再注册一个60s的定时器,如果60s内流2来了,则把两个流连接发送下游;如果60内流2没有来,则把流1数据测流输出
流2的处理逻辑也是这样。
另外再加一个定时器的状态,用于清除定时器,因为60s内如果另一个流数据来的话,此时已经不需要定时器了,及时删除定时器。所以这里用了一个状态标志定时器。
原文:https://www.cnblogs.com/mn-lily/p/14735934.html