You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by mj...@apache.org on 2025/01/03 18:40:38 UTC

(kafka) branch 4.0 updated: MINOR: add broadcast/multicast to Kafka Streams docs (#18341)

This is an automated email from the ASF dual-hosted git repository.

mjsax pushed a commit to branch 4.0
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/4.0 by this push:
     new 4784daa91fd MINOR: add broadcast/multicast to Kafka Streams docs (#18341)
4784daa91fd is described below

commit 4784daa91fd53d1bc985fed1b167a2983d1e7045
Author: Matthias J. Sax <ma...@confluent.io>
AuthorDate: Fri Jan 3 10:38:27 2025 -0800

    MINOR: add broadcast/multicast to Kafka Streams docs (#18341)
    
    Reviewer: Lucas Brutschy <lb...@confluent.io>
---
 docs/streams/developer-guide/dsl-api.html | 88 ++++++++++++++-----------------
 1 file changed, 41 insertions(+), 47 deletions(-)

diff --git a/docs/streams/developer-guide/dsl-api.html b/docs/streams/developer-guide/dsl-api.html
index 32822c27094..97b1c63b22c 100644
--- a/docs/streams/developer-guide/dsl-api.html
+++ b/docs/streams/developer-guide/dsl-api.html
@@ -386,7 +386,30 @@ Map&lt;String, KStream&lt;String, Long&gt;&gt; branches =
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>Filter</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>Broadcast/Multicast</strong></p>
+                        <ul class="last simple">
+                            <li>no operator</li>
+                        </ul>
+                    </td>
+                        <td><p class="first">Broadcasting a <code class="docutils literal"><span class="pre">KStream</span></code> into multiple downstream operators.</p>
+                            <p>A record is sent to more than one operator by applying multiple operators to the same <code class="docutils literal"><span class="pre">KStream</span></code> instance.</p>
+                            <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; stream = ...;
+KStream&lt;...&gt; stream1 = stream.map(...);
+KStream&lt;...&gt; stream2 = stream.mapValue(...);
+KStream&lt;...&gt; stream3 = stream.flatMap(...);
+</code></pre>
+                            <p class="first">Multicasting a <code class="docutils literal"><span class="pre">KStream</span></code> into multiple downstream operators.</p>
+                            <p>In contrast to <strong>branching</strong>, which sends each record to at most one downstream branch,
+                               a multicast may send a record to any number of downstream <code class="docutils literal"><span class="pre">KStream</span></code> instances.</p>
+                            <p>A multicast is implemented as a broadcast plus filters.</p>
+                            <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; stream = ...;
+KStream&lt;...&gt; stream1 = stream.filter((key, value) -&gt; key.startsWith(&quot;A&quot;)); // contains all records whose keys start with "A"
+KStream&lt;...&gt; stream2 = stream.filter((key, value) -&gt; key.startsWith(&quot;AB&quot;)); // contains all records whose keys start with "AB" (subset of stream1)
+KStream&lt;...&gt; stream3 = stream.filter((key, value) -&gt; key.contains(&quot;B&quot;)); // contains all records whose keys contains a "B" (superset of stream2)
+</code></pre>
+                        </td>
+                    </tr>
+                    <tr class="row-even"><td><p class="first"><strong>Filter</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                             <li>KTable &rarr; KTable</li>
@@ -398,12 +421,11 @@ Map&lt;String, KStream&lt;String, Long&gt;&gt; branches =
                             <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; stream = ...;
 
 // A filter that selects (keeps) only positive numbers
-// Java 8+ example, using lambda expressions
 KStream&lt;String, Long&gt; onlyPositives = stream.filter((key, value) -&gt; value &gt; 0);
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>Inverse Filter</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>Inverse Filter</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                             <li>KTable &rarr; KTable</li>
@@ -415,13 +437,12 @@ KStream&lt;String, Long&gt; onlyPositives = stream.filter((key, value) -&gt; val
                             <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; stream = ...;
 
 // An inverse filter that discards any negative numbers or zero
-// Java 8+ example, using lambda expressions
 KStream&lt;String, Long&gt; onlyPositives = stream.filterNot((key, value) -&gt; value &lt;= 0);
 
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>FlatMap</strong></p>
+                    <tr class="row-even"><td><p class="first"><strong>FlatMap</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                         </ul>
@@ -447,7 +468,7 @@ KStream&lt;String, Integer&gt; transformed = stream.flatMap(
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>FlatMapValues</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>FlatMapValues</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                         </ul>
@@ -463,7 +484,7 @@ KStream&lt;byte[], String&gt; words = sentences.flatMapValues(value -&gt; Arrays
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>Foreach</strong></p>
+                    <tr class="row-even"><td><p class="first"><strong>Foreach</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; void</li>
                             <li>KStream &rarr; void</li>
@@ -479,12 +500,11 @@ KStream&lt;byte[], String&gt; words = sentences.flatMapValues(value -&gt; Arrays
                             <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; stream = ...;
 
 // Print the contents of the KStream to the local console.
-// Java 8+ example, using lambda expressions
 stream.foreach((key, value) -&gt; System.out.println(key + &quot; =&gt; &quot; + value));
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>GroupByKey</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>GroupByKey</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KGroupedStream</li>
                         </ul>
@@ -524,7 +544,7 @@ KGroupedStream&lt;byte[], String&gt; groupedStream = stream.groupByKey(
   );</code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>GroupBy</strong></p>
+                    <tr class="row-even"><td><p class="first"><strong>GroupBy</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KGroupedStream</li>
                             <li>KTable &rarr; KGroupedTable</li>
@@ -554,8 +574,6 @@ KGroupedStream&lt;byte[], String&gt; groupedStream = stream.groupByKey(
                             <pre class="line-numbers"><code class="language-java">KStream&lt;byte[], String&gt; stream = ...;
 KTable&lt;byte[], String&gt; table = ...;
 
-// Java 8+ examples, using lambda expressions
-
 // Group the stream by a new key and key type
 KGroupedStream&lt;String, String&gt; groupedStream = stream.groupBy(
     (key, value) -&gt; value,
@@ -575,7 +593,7 @@ KGroupedTable&lt;String, Integer&gt; groupedTable = table.groupBy(
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>Cogroup</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>Cogroup</strong></p>
                         <ul class="last simple">
                             <li>KGroupedStream &rarr; CogroupedKStream</li>
                             <li>CogroupedKStream &rarr; CogroupedKStream</li>
@@ -601,7 +619,7 @@ KTable&lt;byte[], String&gt; table = cogroupedStream.aggregate(initializer);
 KTable&lt;byte[], String&gt; table2 = cogroupedStream.windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMillis(500))).aggregate(initializer);</code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>Map</strong></p>
+                    <tr class="row-even"><td><p class="first"><strong>Map</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                         </ul>
@@ -613,7 +631,6 @@ KTable&lt;byte[], String&gt; table2 = cogroupedStream.windowedBy(TimeWindows.ofS
                                 If possible use <code class="docutils literal"><span class="pre">mapValues</span></code> instead, which will not cause data re-partitioning.</p>
                             <pre class="line-numbers"><code class="language-java">KStream&lt;byte[], String&gt; stream = ...;
 
-// Java 8+ example, using lambda expressions
 // Note how we change the key and the key type (similar to `selectKey`)
 // as well as the value and the value type.
 KStream&lt;String, Integer&gt; transformed = stream.map(
@@ -621,7 +638,7 @@ KStream&lt;String, Integer&gt; transformed = stream.map(
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>Map (values only)</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>Map (values only)</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                             <li>KTable &rarr; KTable</li>
@@ -635,12 +652,11 @@ KStream&lt;String, Integer&gt; transformed = stream.map(
                                 allow you to modify the key or key type like <code class="docutils literal"><span class="pre">map</span></code> does.</p>
                             <pre class="line-numbers"><code class="language-java">KStream&lt;byte[], String&gt; stream = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;byte[], String&gt; uppercased = stream.mapValues(value -&gt; value.toUpperCase());
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>Merge</strong></p>
+                    <tr class="row-even"><td><p class="first"><strong>Merge</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                         </ul>
@@ -657,7 +673,7 @@ KStream&lt;byte[], String&gt; stream2 = ...;
 KStream&lt;byte[], String&gt; merged = stream1.merge(stream2);</code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>Peek</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>Peek</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                         </ul>
@@ -672,13 +688,12 @@ KStream&lt;byte[], String&gt; merged = stream1.merge(stream2);</code></pre>
                                 trackable by Kafka, which means they will typically not benefit from Kafka&#8217;s processing guarantees.</p>
                             <pre class="line-numbers"><code class="language-java">KStream&lt;byte[], String&gt; stream = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;byte[], String&gt; unmodifiedStream = stream.peek(
     (key, value) -&gt; System.out.println(&quot;key=&quot; + key + &quot;, value=&quot; + value));
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>Print</strong></p>
+                    <tr class="row-even"><td><p class="first"><strong>Print</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; void</li>
                         </ul>
@@ -696,7 +711,7 @@ stream.print();
 stream.print(Printed.toFile(&quot;streams.out&quot;).withLabel(&quot;streams&quot;));</code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>SelectKey</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>SelectKey</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KStream</li>
                         </ul>
@@ -709,12 +724,11 @@ stream.print(Printed.toFile(&quot;streams.out&quot;).withLabel(&quot;streams&quo
                             <pre class="line-numbers"><code class="language-java">KStream&lt;byte[], String&gt; stream = ...;
 
 // Derive a new record key from the record&#39;s value.  Note how the key type changes, too.
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; rekeyed = stream.selectKey((key, value) -&gt; value.split(&quot; &quot;)[0])
 </code></pre>
                         </td>
                     </tr>
-                    <tr class="row-odd"><td><p class="first"><strong>Table to Stream</strong></p>
+                    <tr class="row-even"><td><p class="first"><strong>Table to Stream</strong></p>
                         <ul class="last simple">
                             <li>KTable &rarr; KStream</li>
                         </ul>
@@ -728,7 +742,7 @@ KStream&lt;String, String&gt; rekeyed = stream.selectKey((key, value) -&gt; valu
 KStream&lt;byte[], String&gt; stream = table.toStream();</code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even"><td><p class="first"><strong>Stream to Table</strong></p>
+                    <tr class="row-odd"><td><p class="first"><strong>Stream to Table</strong></p>
                         <ul class="last simple">
                             <li>KStream &rarr; KTable</li>
                         </ul>
@@ -740,7 +754,7 @@ KStream&lt;byte[], String&gt; stream = table.toStream();</code></pre>
 KTable&lt;byte[], String&gt; table = stream.toTable();</code></pre>
                         </td>
                     </tr>
-                    <tr class="row-even">
+                    <tr class="row-odd">
                         <td><p class="first"><strong>Repartition</strong></p>
                             <ul class="last simple">
                                 <li>KStream &rarr; KStream</li>
@@ -791,7 +805,7 @@ KStream&lt;byte[], String&gt; repartitionedStream = stream.repartition(Repartiti
                     <p class="caption"><span class="caption-text">Stateful transformations in the DSL.</span></p>
                 </div>
                 <p>Here is an example of a stateful application: the WordCount algorithm.</p>
-                <p>WordCount example in Java 8+, using lambda expressions:</p>
+                <p>WordCount example:</p>
                 <pre class="line-numbers"><code class="language-java">// Assume the record values represent lines of text.  For the sake of this example, you can ignore
 // whatever may be stored in the record keys.
 KStream&lt;String, String&gt; textLines = ...;
@@ -850,8 +864,6 @@ KStream&lt;String, Long&gt; wordCounts = textLines
                                 <pre class="line-numbers"><code class="language-java">KGroupedStream&lt;byte[], String&gt; groupedStream = ...;
 KGroupedTable&lt;byte[], String&gt; groupedTable = ...;
 
-// Java 8+ examples, using lambda expressions
-
 // Aggregating a KGroupedStream (note how the value type changes from String to Long)
 KTable&lt;byte[], Long&gt; aggregatedStream = groupedStream.aggregate(
     () -&gt; 0L, /* initializer */
@@ -913,8 +925,6 @@ KTable&lt;byte[], Long&gt; aggregatedTable = groupedTable.aggregate(
                                 <pre class="line-numbers"><code class="language-java">import java.time.Duration;
 KGroupedStream&lt;String, Long&gt; groupedStream = ...;
 
-// Java 8+ examples, using lambda expressions
-
 // Aggregating with time-based windowing (here: with 5-minute tumbling windows)
 KTable&lt;Windowed&lt;String&gt;, Long&gt; timeWindowedAggregatedStream = groupedStream.windowedBy(Duration.ofMinutes(5))
     .aggregate(
@@ -1035,8 +1045,6 @@ KTable&lt;Windowed&lt;String&gt;, Long&gt; aggregatedStream = groupedStream.wind
                                 <pre class="line-numbers"><code class="language-java">KGroupedStream&lt;String, Long&gt; groupedStream = ...;
 KGroupedTable&lt;String, Long&gt; groupedTable = ...;
 
-// Java 8+ examples, using lambda expressions
-
 // Reducing a KGroupedStream
 KTable&lt;String, Long&gt; aggregatedStream = groupedStream.reduce(
     (aggValue, newValue) -&gt; aggValue + newValue /* adder */);
@@ -1092,8 +1100,6 @@ KTable&lt;String, Long&gt; aggregatedTable = groupedTable.reduce(
                                 <pre class="line-numbers"><code class="language-java">import java.time.Duration;
 KGroupedStream&lt;String, Long&gt; groupedStream = ...;
 
-// Java 8+ examples, using lambda expressions
-
 // Aggregating with time-based windowing (here: with 5-minute tumbling windows)
 KTable&lt;Windowed&lt;String&gt;, Long&gt; timeWindowedAggregatedStream = groupedStream.windowedBy(
   TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)) /* time-based window */)
@@ -1565,7 +1571,6 @@ KeyValue&lt;K, JV&gt; joinOutputRecord = KeyValue.pair(
 KStream&lt;String, Long&gt; left = ...;
 KStream&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; joined = left.join(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue, /* ValueJoiner */
     JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofMinutes(5)),
@@ -1608,7 +1613,6 @@ KStream&lt;String, String&gt; joined = left.join(right,
 KStream&lt;String, Long&gt; left = ...;
 KStream&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; joined = left.leftJoin(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue, /* ValueJoiner */
     JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofMinutes(5)),
@@ -1655,7 +1659,6 @@ KStream&lt;String, String&gt; joined = left.leftJoin(right,
 KStream&lt;String, Long&gt; left = ...;
 KStream&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; joined = left.outerJoin(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue, /* ValueJoiner */
     JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofMinutes(5)),
@@ -1913,7 +1916,6 @@ KeyValue&lt;K, JV&gt; joinOutputRecord = KeyValue.pair(
                                     <pre class="line-numbers"><code class="language-java">KTable&lt;String, Long&gt; left = ...;
 KTable&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KTable&lt;String, String&gt; joined = left.join(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue /* ValueJoiner */
   );
@@ -1952,7 +1954,6 @@ KTable&lt;String, String&gt; joined = left.join(right,
                                     <pre class="line-numbers"><code class="language-java">KTable&lt;String, Long&gt; left = ...;
 KTable&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KTable&lt;String, String&gt; joined = left.leftJoin(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue /* ValueJoiner */
   );
@@ -1994,7 +1995,6 @@ KTable&lt;String, String&gt; joined = left.leftJoin(right,
                                     <pre class="line-numbers"><code class="language-java">KTable&lt;String, Long&gt; left = ...;
 KTable&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KTable&lt;String, String&gt; joined = left.outerJoin(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue /* ValueJoiner */
   );
@@ -2218,7 +2218,6 @@ KTable&lt;String, String&gt; joined = left.outerJoin(right,
 //This foreignKeyExtractor simply uses the left-value to map to the right-key.
 Function&lt;Long, Long&gt; foreignKeyExtractor = (x) -&gt; x;
 
-// Java 8+ example, using lambda expressions
                 KTable&lt;String, String&gt; joined = left.join(right, foreignKeyExtractor,
                     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue /* ValueJoiner */
                   );</code></pre>
@@ -2281,7 +2280,6 @@ Function&lt;Long, Long&gt; foreignKeyExtractor = (x) -&gt; x;
 //This foreignKeyExtractor simply uses the left-value to map to the right-key.
 Function&lt;Long, Long&gt; foreignKeyExtractor = (x) -&gt; x;
 
-// Java 8+ example, using lambda expressions
                 KTable&lt;String, String&gt; joined = left.leftJoin(right, foreignKeyExtractor,
                     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue /* ValueJoiner */
                   );</code></pre>
@@ -2505,7 +2503,6 @@ KeyValue&lt;K, JV&gt; joinOutputRecord = KeyValue.pair(
                                     <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; left = ...;
 KTable&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; joined = left.join(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue, /* ValueJoiner */
     Joined.keySerde(Serdes.String()) /* key */
@@ -2555,7 +2552,6 @@ KStream&lt;String, String&gt; joined = left.join(right,
                                     <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; left = ...;
 KTable&lt;String, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; joined = left.leftJoin(right,
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue, /* ValueJoiner */
     Joined.keySerde(Serdes.String()) /* key */
@@ -2768,7 +2764,6 @@ KeyValue&lt;K, JV&gt; joinOutputRecord = KeyValue.pair(
                                     <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; left = ...;
 GlobalKTable&lt;Integer, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; joined = left.join(right,
     (leftKey, leftValue) -&gt; leftKey.length(), /* derive a (potentially) new key by which to lookup against the table */
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue /* ValueJoiner */
@@ -2806,7 +2801,6 @@ KStream&lt;String, String&gt; joined = left.join(right,
                                     <pre class="line-numbers"><code class="language-java">KStream&lt;String, Long&gt; left = ...;
 GlobalKTable&lt;Integer, Double&gt; right = ...;
 
-// Java 8+ example, using lambda expressions
 KStream&lt;String, String&gt; joined = left.leftJoin(right,
     (leftKey, leftValue) -&gt; leftKey.length(), /* derive a (potentially) new key by which to lookup against the table */
     (leftValue, rightValue) -&gt; &quot;left=&quot; + leftValue + &quot;, right=&quot; + rightValue /* ValueJoiner */