From issues-return-180937-apmail-flink-issues-archive=flink.apache.org@flink.apache.org Mon Jul 30 09:20:53 2018 Return-Path: X-Original-To: apmail-flink-issues-archive@minotaur.apache.org Delivered-To: apmail-flink-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BF031187DF for ; Mon, 30 Jul 2018 09:20:52 +0000 (UTC) Received: (qmail 28487 invoked by uid 500); 30 Jul 2018 09:20:52 -0000 Delivered-To: apmail-flink-issues-archive@flink.apache.org Received: (qmail 28359 invoked by uid 500); 30 Jul 2018 09:20:52 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 28236 invoked by uid 99); 30 Jul 2018 09:20:52 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Jul 2018 09:20:52 +0000 From: GitBox To: issues@flink.apache.org Subject: [GitHub] zentol commented on a change in pull request #6407: [FLINK-9877][docs] Add documentation page for different datastream joins Message-ID: <153294245186.31601.17977986774485977006.gitbox@gitbox.apache.org> Date: Mon, 30 Jul 2018 09:20:51 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit zentol commented on a change in pull request #6407: [FLINK-9877][docs] Add documentation page for different datastream joins URL: https://github.com/apache/flink/pull/6407#discussion_r206064145 ########## File path: docs/dev/stream/operators/joining.md ########## @@ -0,0 +1,284 @@ +--- +title: "Joining" +nav-id: streaming_joins +nav-show_overview: true +nav-parent_id: streaming_operators +nav-pos: 11 +--- + + +* toc +{:toc} + +# 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]({{ site.baseurl}}/dev/stream/operators/windows.html#window-assigners) 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: + +{% highlight java %} +stream.join(otherStream) + .where() + .equalTo() + .window() + .apply() +{% endhighlight %} + +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! + + + +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 ⑦. + +
+
+{% highlight java %} +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows; +import org.apache.flink.streaming.api.windowing.time.Time; + +... + +DataStream orangeStream = ... +DataStream greenStream = ... + +orangeStream.join(greenStream) + .where() + .equalTo() + .window(TumblingEventTimeWindows.of(Time.seconds(2))) + .apply (new JoinFunction () { + @Override + public String join(Integer first, Integer second) { + return first + "," + second; + } + }); + {% endhighlight %} +
+
+ +{% highlight scala %} +import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows; +import org.apache.flink.streaming.api.windowing.time.Time; + +... + +val orangeStream: DataStream[Integer] = ... +val greenStream: DataStream[Integer] = ... + +orangeStream.join(greenStream) + .where(elem => /* select key */) + .equalTo(elem => /* select key */) + .window(TumblingEventTimeWindows.of(Time.milliseconds(2))) + .apply { (e1, e2) => e1 + "," + e2 } + {% endhighlight %} + +
+
+ +## Sliding Window Join +When performing a sliding window join, all elements with a common key and common sliding window are joined are 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! + + + +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]`. + +
+
+ +{% highlight java %} +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows; +import org.apache.flink.streaming.api.windowing.time.Time; + +... + +DataStream orangeStream = ... +DataStream greenStream = ... + +orangeStream.join(greenStream) + .where() + .equalTo() + .window(SlidingEventTimeWindows.of(Time.milliseconds(2) /* size */, Time.milliseconds(1) /* slide */)) + .apply (new JoinFunction () { + @Override + public String join(Integer first, Integer second) { + return first + "," + second; + } + }); + {% endhighlight %} +
+
+ +{% highlight scala %} +import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows; +import org.apache.flink.streaming.api.windowing.time.Time; + +... + +val orangeStream: DataStream[Integer] = ... +val greenStream: DataStream[Integer] = ... + +orangeStream.join(greenStream) + .where(elem => /* select key */) + .equalTo(elem => /* select key */) + .window(SlidingEventTimeWindows.of(Time.milliseconds(2) /* size */, Time.milliseconds(1) /* slide */)) + .apply { (e1, e2) => e1 + "," + e2 } + {% endhighlight %} +
+
+ +## 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! + + + +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! + +
+
+ +{% highlight java %} +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows; +import org.apache.flink.streaming.api.windowing.time.Time; + +... + +DataStream orangeStream = ... +DataStream greenStream = ... + +orangeStream.join(greenStream) + .where() + .equalTo() + .window(EventTimeSessionWindows.withGap(Time.milliseconds(1))) + .apply (new JoinFunction () { + @Override + public String join(Integer first, Integer second) { + return first + "," + second; + } + }); + {% endhighlight %} +
+
+ +{% highlight scala %} +import org.apache.flink.streaming.api.windowing.assigners.EventTimeSessionWindows; +import org.apache.flink.streaming.api.windowing.time.Time; + +... + +val orangeStream: DataStream[Integer] = ... +val greenStream: DataStream[Integer] = ... + +orangeStream.join(greenStream) + .where(elem => /* select key */) + .equalTo(elem => /* select key */) + .window(EventTimeSessionWindows.withGap(Time.milliseconds(1))) + .apply { (e1, e2) => e1 + "," + e2 } + {% endhighlight %} Review comment: remove leading space ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org With regards, Apache Git Services