Joining

Window Join

A window join joins the elements of two streams that share a common key and lie in the same window. These windows can be defined by using a window assigner and are evaluated on elements from both of the streams.

The elements from both sides are then passed to a user-defined JoinFunction or FlatJoinFunction where the user can emit results that meet the join criteria.

The general usage can be summarized as follows:

  1. stream.join(otherStream)
  2. .where(<KeySelector>)
  3. .equalTo(<KeySelector>)
  4. .window(<WindowAssigner>)
  5. .apply(<JoinFunction>)

Some notes on semantics:

  • The creation of pairwise combinations of elements of the two streams behaves like an inner-join, meaning elements from one stream will not be emitted if they don’t have a corresponding element from the other stream to be joined with.
  • Those elements that do get joined will have as their timestamp the largest timestamp that still lies in the respective window. For example a window with [5, 10) as its boundaries would result in the joined elements having 9 as their timestamp.

In the following section we are going to give an overview over how different kinds of window joins behave using some exemplary scenarios.

Tumbling Window Join

When performing a tumbling window join, all elements with a common key and a common tumbling window are joined as pairwise combinations and passed on to a JoinFunction or FlatJoinFunction. Because this behaves like an inner join, elements of one stream that do not have elements from another stream in their tumbling window are not emitted!

Joining - 图1

As illustrated in the figure, we define a tumbling window with the size of 2 milliseconds, which results in windows of the form [0,1], [2,3], .... The image shows the pairwise combinations of all elements in each window which will be passed on to the JoinFunction. Note that in the tumbling window [6,7] nothing is emitted because no elements exist in the green stream to be joined with the orange elements ⑥ and ⑦.

  1. import org.apache.flink.api.java.functions.KeySelector;
  2. import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
  3. import org.apache.flink.streaming.api.windowing.time.Time;
  4. ...
  5. DataStream<Integer> orangeStream = ...
  6. DataStream<Integer> greenStream = ...
  7. orangeStream.join(greenStream)
  8. .where(<KeySelector>)
  9. .equalTo(<KeySelector>)
  10. .window(TumblingEventTimeWindows.of(Time.milliseconds(2)))
  11. .apply (new JoinFunction<Integer, Integer, String> (){
  12. @Override
  13. public String join(Integer first, Integer second) {
  14. return first + "," + second;
  15. }
  16. });
  1. import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
  2. import org.apache.flink.streaming.api.windowing.time.Time;
  3. ...
  4. val orangeStream: DataStream[Integer] = ...
  5. val greenStream: DataStream[Integer] = ...
  6. orangeStream.join(greenStream)
  7. .where(elem => /* select key */)
  8. .equalTo(elem => /* select key */)
  9. .window(TumblingEventTimeWindows.of(Time.milliseconds(2)))
  10. .apply { (e1, e2) => e1 + "," + e2 }

Sliding Window Join

When performing a sliding window join, all elements with a common key and common sliding window are joined as pairwise combinations and passed on to the JoinFunction or FlatJoinFunction. Elements of one stream that do not have elements from the other stream in the current sliding window are not emitted! Note that some elements might be joined in one sliding window but not in another!

Joining - 图2

In this example we are using sliding windows with a size of two milliseconds and slide them by one millisecond, resulting in the sliding windows [-1, 0],[0,1],[1,2],[2,3], …. The joined elements below the x-axis are the ones that are passed to the JoinFunction for each sliding window. Here you can also see how for example the orange ② is joined with the green ③ in the window [2,3], but is not joined with anything in the window [1,2].

  1. import org.apache.flink.api.java.functions.KeySelector;
  2. import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
  3. import org.apache.flink.streaming.api.windowing.time.Time;
  4. ...
  5. DataStream<Integer> orangeStream = ...
  6. DataStream<Integer> greenStream = ...
  7. orangeStream.join(greenStream)
  8. .where(<KeySelector>)
  9. .equalTo(<KeySelector>)
  10. .window(SlidingEventTimeWindows.of(Time.milliseconds(2) /* size */, Time.milliseconds(1) /* slide */))
  11. .apply (new JoinFunction<Integer, Integer, String> (){
  12. @Override
  13. public String join(Integer first, Integer second) {
  14. return first + "," + second;
  15. }
  16. });
  1. import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
  2. import org.apache.flink.streaming.api.windowing.time.Time;
  3. ...
  4. val orangeStream: DataStream[Integer] = ...
  5. val greenStream: DataStream[Integer] = ...
  6. orangeStream.join(greenStream)
  7. .where(elem => /* select key */)
  8. .equalTo(elem => /* select key */)
  9. .window(SlidingEventTimeWindows.of(Time.milliseconds(2) /* size */, Time.milliseconds(1) /* slide */))
  10. .apply { (e1, e2) => e1 + "," + e2 }

Session Window Join

When performing a session window join, all elements with the same key that when “combined” fulfill the session criteria are joined in pairwise combinations and passed on to the JoinFunction or FlatJoinFunction. Again this performs an inner join, so if there is a session window that only contains elements from one stream, no output will be emitted!

Joining - 图3

Here we define a session window join where each session is divided by a gap of at least 1ms. There are three sessions, and in the first two sessions the joined elements from both streams are passed to the JoinFunction. In the third session there are no elements in the green stream, so ⑧ and ⑨ are not joined!

  1. import org.apache.flink.api.java.functions.KeySelector;
  2. import org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows;
  3. import org.apache.flink.streaming.api.windowing.time.Time;
  4. ...
  5. DataStream<Integer> orangeStream = ...
  6. DataStream<Integer> greenStream = ...
  7. orangeStream.join(greenStream)
  8. .where(<KeySelector>)
  9. .equalTo(<KeySelector>)
  10. .window(EventTimeSessionWindows.withGap(Time.milliseconds(1)))
  11. .apply (new JoinFunction<Integer, Integer, String> (){
  12. @Override
  13. public String join(Integer first, Integer second) {
  14. return first + "," + second;
  15. }
  16. });
  1. import org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows;
  2. import org.apache.flink.streaming.api.windowing.time.Time;
  3. ...
  4. val orangeStream: DataStream[Integer] = ...
  5. val greenStream: DataStream[Integer] = ...
  6. orangeStream.join(greenStream)
  7. .where(elem => /* select key */)
  8. .equalTo(elem => /* select key */)
  9. .window(EventTimeSessionWindows.withGap(Time.milliseconds(1)))
  10. .apply { (e1, e2) => e1 + "," + e2 }

Interval Join

The interval join joins elements of two streams (we’ll call them A & B for now) 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.

This can also be expressed more formally as b.timestamp ∈ [a.timestamp + lowerBound; a.timestamp + upperBound] or a.timestamp + lowerBound <= b.timestamp <= a.timestamp + upperBound

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

Note The interval join currently only supports event time.

Joining - 图4

In the example above, we join two streams ‘orange’ and ‘green’ with a lower bound of -2 milliseconds and an upper bound of +1 millisecond. Be default, these boundaries are inclusive, but .lowerBoundExclusive() and .upperBoundExclusive can be applied to change the behaviour.

Using the more formal notation again this will translate to

orangeElem.ts + lowerBound <= greenElem.ts <= orangeElem.ts + upperBound

as indicated by the triangles.

  1. import org.apache.flink.api.java.functions.KeySelector;
  2. import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
  3. import org.apache.flink.streaming.api.windowing.time.Time;
  4. ...
  5. DataStream<Integer> orangeStream = ...
  6. DataStream<Integer> greenStream = ...
  7. orangeStream
  8. .keyBy(<KeySelector>)
  9. .intervalJoin(greenStream.keyBy(<KeySelector>))
  10. .between(Time.milliseconds(-2), Time.milliseconds(1))
  11. .process (new ProcessJoinFunction<Integer, Integer, String(){
  12. @Override
  13. public void processElement(Integer left, Integer right, Context ctx, Collector<String> out) {
  14. out.collect(first + "," + second);
  15. }
  16. });
  1. import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
  2. import org.apache.flink.streaming.api.windowing.time.Time;
  3. ...
  4. val orangeStream: DataStream[Integer] = ...
  5. val greenStream: DataStream[Integer] = ...
  6. orangeStream
  7. .keyBy(elem => /* select key */)
  8. .intervalJoin(greenStream.keyBy(elem => /* select key */))
  9. .between(Time.milliseconds(-2), Time.milliseconds(1))
  10. .process(new ProcessJoinFunction[Integer, Integer, String] {
  11. override def processElement(left: Integer, right: Integer, ctx: ProcessJoinFunction[Integer, Integer, String]#Context, out: Collector[String]): Unit = {
  12. out.collect(left + "," + right);
  13. }
  14. });
  15. });