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<String, KStream<String, Long>> 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<String, Long> stream = ...;
+KStream<...> stream1 = stream.map(...);
+KStream<...> stream2 = stream.mapValue(...);
+KStream<...> 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<String, Long> stream = ...;
+KStream<...> stream1 = stream.filter((key, value) -> key.startsWith("A")); // contains all records whose keys start with "A"
+KStream<...> stream2 = stream.filter((key, value) -> key.startsWith("AB")); // contains all records whose keys start with "AB" (subset of stream1)
+KStream<...> stream3 = stream.filter((key, value) -> key.contains("B")); // 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 → KStream</li>
<li>KTable → KTable</li>
@@ -398,12 +421,11 @@ Map<String, KStream<String, Long>> branches =
<pre class="line-numbers"><code class="language-java">KStream<String, Long> stream = ...;
// A filter that selects (keeps) only positive numbers
-// Java 8+ example, using lambda expressions
KStream<String, Long> onlyPositives = stream.filter((key, value) -> value > 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 → KStream</li>
<li>KTable → KTable</li>
@@ -415,13 +437,12 @@ KStream<String, Long> onlyPositives = stream.filter((key, value) -> val
<pre class="line-numbers"><code class="language-java">KStream<String, Long> stream = ...;
// An inverse filter that discards any negative numbers or zero
-// Java 8+ example, using lambda expressions
KStream<String, Long> onlyPositives = stream.filterNot((key, value) -> value <= 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 → KStream</li>
</ul>
@@ -447,7 +468,7 @@ KStream<String, Integer> 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 → KStream</li>
</ul>
@@ -463,7 +484,7 @@ KStream<byte[], String> words = sentences.flatMapValues(value -> 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 → void</li>
<li>KStream → void</li>
@@ -479,12 +500,11 @@ KStream<byte[], String> words = sentences.flatMapValues(value -> Arrays
<pre class="line-numbers"><code class="language-java">KStream<String, Long> stream = ...;
// Print the contents of the KStream to the local console.
-// Java 8+ example, using lambda expressions
stream.foreach((key, value) -> System.out.println(key + " => " + 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 → KGroupedStream</li>
</ul>
@@ -524,7 +544,7 @@ KGroupedStream<byte[], String> 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 → KGroupedStream</li>
<li>KTable → KGroupedTable</li>
@@ -554,8 +574,6 @@ KGroupedStream<byte[], String> groupedStream = stream.groupByKey(
<pre class="line-numbers"><code class="language-java">KStream<byte[], String> stream = ...;
KTable<byte[], String> table = ...;
-// Java 8+ examples, using lambda expressions
-
// Group the stream by a new key and key type
KGroupedStream<String, String> groupedStream = stream.groupBy(
(key, value) -> value,
@@ -575,7 +593,7 @@ KGroupedTable<String, Integer> 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 → CogroupedKStream</li>
<li>CogroupedKStream → CogroupedKStream</li>
@@ -601,7 +619,7 @@ KTable<byte[], String> table = cogroupedStream.aggregate(initializer);
KTable<byte[], String> 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 → KStream</li>
</ul>
@@ -613,7 +631,6 @@ KTable<byte[], String> 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<byte[], String> 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<String, Integer> transformed = stream.map(
@@ -621,7 +638,7 @@ KStream<String, Integer> 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 → KStream</li>
<li>KTable → KTable</li>
@@ -635,12 +652,11 @@ KStream<String, Integer> 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<byte[], String> stream = ...;
-// Java 8+ example, using lambda expressions
KStream<byte[], String> uppercased = stream.mapValues(value -> 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 → KStream</li>
</ul>
@@ -657,7 +673,7 @@ KStream<byte[], String> stream2 = ...;
KStream<byte[], String> 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 → KStream</li>
</ul>
@@ -672,13 +688,12 @@ KStream<byte[], String> merged = stream1.merge(stream2);</code></pre>
trackable by Kafka, which means they will typically not benefit from Kafka’s processing guarantees.</p>
<pre class="line-numbers"><code class="language-java">KStream<byte[], String> stream = ...;
-// Java 8+ example, using lambda expressions
KStream<byte[], String> unmodifiedStream = stream.peek(
(key, value) -> System.out.println("key=" + key + ", value=" + 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 → void</li>
</ul>
@@ -696,7 +711,7 @@ stream.print();
stream.print(Printed.toFile("streams.out").withLabel("streams"));</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 → KStream</li>
</ul>
@@ -709,12 +724,11 @@ stream.print(Printed.toFile("streams.out").withLabel("streams&quo
<pre class="line-numbers"><code class="language-java">KStream<byte[], String> stream = ...;
// Derive a new record key from the record's value. Note how the key type changes, too.
-// Java 8+ example, using lambda expressions
KStream<String, String> rekeyed = stream.selectKey((key, value) -> value.split(" ")[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 → KStream</li>
</ul>
@@ -728,7 +742,7 @@ KStream<String, String> rekeyed = stream.selectKey((key, value) -> valu
KStream<byte[], String> 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 → KTable</li>
</ul>
@@ -740,7 +754,7 @@ KStream<byte[], String> stream = table.toStream();</code></pre>
KTable<byte[], String> 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 → KStream</li>
@@ -791,7 +805,7 @@ KStream<byte[], String> 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<String, String> textLines = ...;
@@ -850,8 +864,6 @@ KStream<String, Long> wordCounts = textLines
<pre class="line-numbers"><code class="language-java">KGroupedStream<byte[], String> groupedStream = ...;
KGroupedTable<byte[], String> groupedTable = ...;
-// Java 8+ examples, using lambda expressions
-
// Aggregating a KGroupedStream (note how the value type changes from String to Long)
KTable<byte[], Long> aggregatedStream = groupedStream.aggregate(
() -> 0L, /* initializer */
@@ -913,8 +925,6 @@ KTable<byte[], Long> aggregatedTable = groupedTable.aggregate(
<pre class="line-numbers"><code class="language-java">import java.time.Duration;
KGroupedStream<String, Long> groupedStream = ...;
-// Java 8+ examples, using lambda expressions
-
// Aggregating with time-based windowing (here: with 5-minute tumbling windows)
KTable<Windowed<String>, Long> timeWindowedAggregatedStream = groupedStream.windowedBy(Duration.ofMinutes(5))
.aggregate(
@@ -1035,8 +1045,6 @@ KTable<Windowed<String>, Long> aggregatedStream = groupedStream.wind
<pre class="line-numbers"><code class="language-java">KGroupedStream<String, Long> groupedStream = ...;
KGroupedTable<String, Long> groupedTable = ...;
-// Java 8+ examples, using lambda expressions
-
// Reducing a KGroupedStream
KTable<String, Long> aggregatedStream = groupedStream.reduce(
(aggValue, newValue) -> aggValue + newValue /* adder */);
@@ -1092,8 +1100,6 @@ KTable<String, Long> aggregatedTable = groupedTable.reduce(
<pre class="line-numbers"><code class="language-java">import java.time.Duration;
KGroupedStream<String, Long> groupedStream = ...;
-// Java 8+ examples, using lambda expressions
-
// Aggregating with time-based windowing (here: with 5-minute tumbling windows)
KTable<Windowed<String>, Long> timeWindowedAggregatedStream = groupedStream.windowedBy(
TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)) /* time-based window */)
@@ -1565,7 +1571,6 @@ KeyValue<K, JV> joinOutputRecord = KeyValue.pair(
KStream<String, Long> left = ...;
KStream<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KStream<String, String> joined = left.join(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue, /* ValueJoiner */
JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofMinutes(5)),
@@ -1608,7 +1613,6 @@ KStream<String, String> joined = left.join(right,
KStream<String, Long> left = ...;
KStream<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KStream<String, String> joined = left.leftJoin(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue, /* ValueJoiner */
JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofMinutes(5)),
@@ -1655,7 +1659,6 @@ KStream<String, String> joined = left.leftJoin(right,
KStream<String, Long> left = ...;
KStream<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KStream<String, String> joined = left.outerJoin(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue, /* ValueJoiner */
JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofMinutes(5)),
@@ -1913,7 +1916,6 @@ KeyValue<K, JV> joinOutputRecord = KeyValue.pair(
<pre class="line-numbers"><code class="language-java">KTable<String, Long> left = ...;
KTable<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KTable<String, String> joined = left.join(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue /* ValueJoiner */
);
@@ -1952,7 +1954,6 @@ KTable<String, String> joined = left.join(right,
<pre class="line-numbers"><code class="language-java">KTable<String, Long> left = ...;
KTable<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KTable<String, String> joined = left.leftJoin(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue /* ValueJoiner */
);
@@ -1994,7 +1995,6 @@ KTable<String, String> joined = left.leftJoin(right,
<pre class="line-numbers"><code class="language-java">KTable<String, Long> left = ...;
KTable<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KTable<String, String> joined = left.outerJoin(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue /* ValueJoiner */
);
@@ -2218,7 +2218,6 @@ KTable<String, String> joined = left.outerJoin(right,
//This foreignKeyExtractor simply uses the left-value to map to the right-key.
Function<Long, Long> foreignKeyExtractor = (x) -> x;
-// Java 8+ example, using lambda expressions
KTable<String, String> joined = left.join(right, foreignKeyExtractor,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue /* ValueJoiner */
);</code></pre>
@@ -2281,7 +2280,6 @@ Function<Long, Long> foreignKeyExtractor = (x) -> x;
//This foreignKeyExtractor simply uses the left-value to map to the right-key.
Function<Long, Long> foreignKeyExtractor = (x) -> x;
-// Java 8+ example, using lambda expressions
KTable<String, String> joined = left.leftJoin(right, foreignKeyExtractor,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue /* ValueJoiner */
);</code></pre>
@@ -2505,7 +2503,6 @@ KeyValue<K, JV> joinOutputRecord = KeyValue.pair(
<pre class="line-numbers"><code class="language-java">KStream<String, Long> left = ...;
KTable<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KStream<String, String> joined = left.join(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue, /* ValueJoiner */
Joined.keySerde(Serdes.String()) /* key */
@@ -2555,7 +2552,6 @@ KStream<String, String> joined = left.join(right,
<pre class="line-numbers"><code class="language-java">KStream<String, Long> left = ...;
KTable<String, Double> right = ...;
-// Java 8+ example, using lambda expressions
KStream<String, String> joined = left.leftJoin(right,
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue, /* ValueJoiner */
Joined.keySerde(Serdes.String()) /* key */
@@ -2768,7 +2764,6 @@ KeyValue<K, JV> joinOutputRecord = KeyValue.pair(
<pre class="line-numbers"><code class="language-java">KStream<String, Long> left = ...;
GlobalKTable<Integer, Double> right = ...;
-// Java 8+ example, using lambda expressions
KStream<String, String> joined = left.join(right,
(leftKey, leftValue) -> leftKey.length(), /* derive a (potentially) new key by which to lookup against the table */
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue /* ValueJoiner */
@@ -2806,7 +2801,6 @@ KStream<String, String> joined = left.join(right,
<pre class="line-numbers"><code class="language-java">KStream<String, Long> left = ...;
GlobalKTable<Integer, Double> right = ...;
-// Java 8+ example, using lambda expressions
KStream<String, String> joined = left.leftJoin(right,
(leftKey, leftValue) -> leftKey.length(), /* derive a (potentially) new key by which to lookup against the table */
(leftValue, rightValue) -> "left=" + leftValue + ", right=" + rightValue /* ValueJoiner */