- 前言
- 1. 概念
- 2. 语法
- 3. 示例
在电商网站中,用户行为往往会有短时间内的强关联。假设有两条流,一条是下订单的流,一条是浏览数据的流,可针对同一个用户做一个联结,使用户的下订单事件和用户的最近十分钟的浏览数据进行一个联结查询(Interval Join)
1. 概念
The interval join joins elements of two streams ( A & B ) with a common key and where elements of stream B have timestamps that lie in a relative time interval to timestamps of elements in stream A
对黄色的流(A)中的任意一个数据元素 a,划分一段时间间隔:[a.timestamp + lowerBound, a.timestamp + upperBound],绿色的流( B)中的数据元素 b, key相同且时间戳在这
个区间范围内,a 和 b 成功配对
2. 语法where a and b are elements of A and B that share a common key. Both the lower and upper bound can be either negative or positive as long as the lower bound is always smaller or equal to the upper bound. The interval join currently only performs inner joins.
When a pair of elements are passed to the ProcessJoinFunction, they will be assigned with the larger timestamp (which can be accessed via the ProcessJoinFunction.Context) of the two elements.
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; ... DataStreamorangeStream = ...; DataStream greenStream = ...; orangeStream .keyBy( ) .intervalJoin(greenStream.keyBy( )) .between(Time.milliseconds(-2), Time.milliseconds(1)) .process (new ProcessJoinFunction @Override public void processElement(Integer left, Integer right, Context ctx, Collector out) { out.collect(left + "," + right); } });
3. 示例
public class IntervalJoinTest {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
SingleOutputStreamOperator> 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
.>forMonotonousTimestamps()
.withTimestampAssigner(new SerializableTimestampAssigner>() {
@Override
public long extractTimestamp(Tuple3
element, long recordTimestamp) {
return element.f2;
}
})
);
SingleOutputStreamOperator 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
.forMonotonousTimestamps()
.withTimestampAssigner(new SerializableTimestampAssigner() {
@Override
public long extractTimestamp(Event element, long recordTimestamp) {
return element.timestamp;
}
})
);
orderStream
.keyBy(new KeySelector, String>() {
@Override
public String getKey(Tuple3 data) throws Exception {
return data.f0;
}
})
.intervalJoin(clickStream.keyBy(new KeySelector() {
@Override
public String getKey(Event data) throws Exception {
return data.user;
}
}))
.between(Time.seconds(-5), Time.seconds(10)) //之前5秒钟 之后10秒钟 范围
.process(new ProcessJoinFunction, Event, String>() {
@Override
public void processElement(Tuple3 left, Event right, Context ctx, Collector out) throws Exception {
out.collect(right + " => " + left);
}
})
.print();
env.execute();
}
}
结果:



