44、Flink 的 Interval Join 详解

Interval Join

Interval join 组合元素的条件为 :两个流(暂时称为 A 和 B)中 key 相同且 B 中元素的 timestamp 处于 A 中元素 timestamp 的一定范围内,即 b.timestamp ∈ [a.timestamp + lowerBound; a.timestamp + upperBound]a.timestamp + lowerBound <= b.timestamp <= a.timestamp + upperBound

这里的 a 和 b 为 A 和 B 中共享相同 key 的元素,上界和下界可正可负,只要下界永远小于等于上界即可,Interval join 目前仅执行 inner join

当一对元素被传递给 ProcessJoinFunction,他们的 timestamp 会从两个元素的 timestamp 中取最大值 (timestamp 可以通过 ProcessJoinFunction.Context 访问)。

Interval join 目前仅支持 event time。

上例中,join 了橙色和绿色两个流,join 的条件是:以 -2 毫秒为下界、+1 毫秒为上界。

默认情况下,上下界也被包括在区间内,但 .lowerBoundExclusive().upperBoundExclusive() 可以将它们排除在外。

图中三角形所表示的条件也可以写成更加正式的表达式:

复制代码
orangeElem.ts + lowerBound <= greenElem.ts <= orangeElem.ts + upperBound

代码示例

复制代码
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;

...

DataStream<Integer> orangeStream = ...;
DataStream<Integer> greenStream = ...;

orangeStream
    .keyBy(<KeySelector>)
    .intervalJoin(greenStream.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);
        }
    });
相关推荐
中科岩创1 天前
河北某铁矿绿色矿山建设二期自动化监测项目
大数据
❀͜͡傀儡师1 天前
docker 部署Flink和传统部署
docker·容器·flink
java水泥工1 天前
基于Echarts+HTML5可视化数据大屏展示-物流大数据展示
大数据·前端·echarts·html5·可视化大屏
paperxie_xiexuo1 天前
学术与职场演示文稿的结构化生成机制探析:基于 PaperXie AI PPT 功能的流程解构与适用性研究
大数据·数据库·人工智能·powerpoint
汤姆yu1 天前
基于大数据的出行方式推荐系统
大数据·出行方式推荐
bigdata-rookie1 天前
Spark 部署模式
大数据·分布式·spark
芝麻开门-新起点1 天前
贝壳GIS数据存储与房屋3D展示技术解析
大数据
玖日大大1 天前
Gemini 3 全维度技术解析:从认知到落地实战指南
大数据
little_xianzhong1 天前
把一个本地项目导入gitee创建的仓库中
大数据·elasticsearch·gitee