From 7bfe008ae1a2bb2fa98c3a765be5022e75cd151d Mon Sep 17 00:00:00 2001
From: Eno Thereska
-Kafka Streams is a client library of Kafka for real-time stream processing and analyzing data stored in Kafka brokers.
-This quickstart example will demonstrate how to run a streaming application coded in this library. Here is the gist
-of the
-It implements the WordCount
-algorithm, which computes a word occurrence histogram from the input text. However, unlike other WordCount examples
-you might have seen before that operate on bounded data, the WordCount demo application behaves slightly differently because it is
-designed to operate on an infinite, unbounded stream of data. Similar to the bounded variant, it is a stateful algorithm that
-tracks and updates the counts of words. However, since it must assume potentially
-unbounded input data, it will periodically output its current state and results while continuing to process more data
-because it cannot know when it has processed "all" the input data.
-
-As the first step, we will prepare input data to a Kafka topic, which will subsequently be processed by a Kafka Streams application.
+ Kafka Streams is a client library for building mission-critical real-time applications and microservices,
+ where the input and/or output data is stored in Kafka clusters. Kafka Streams combines the simplicity of
+ writing and deploying standard Java and Scala applications on the client side with the benefits of Kafka's
+ server-side cluster technology to make these applications highly scalable, elastic, fault-tolerant, distributed,
+ and much more. This quickstart example will demonstrate how
+ to run a streaming application coded in this library.
-Next, we send this input data to the input topic named streams-file-input using the console producer,
-which reads the data from STDIN line-by-line, and publishes each line as a separate Kafka message with null key and value encoded a string to the topic (in practice,
-stream data will likely be flowing continuously into Kafka where the application will be up and running):
-
-We can now run the WordCount demo application to process the input data:
-
-The demo application will read from the input topic streams-file-input, perform the computations of the WordCount algorithm on each of the read messages,
-and continuously write its current results to the output topic streams-wordcount-output.
-Hence there won't be any STDOUT output except log entries as the results are written back into in Kafka.
-The demo will run for a few seconds and then, unlike typical stream processing applications, terminate automatically.
-
-We can now inspect the output of the WordCount demo application by reading from its output topic:
-
-with the following output data being printed to the console:
-
-Here, the first column is the Kafka message key in
-The two diagrams below illustrate what is essentially happening behind the scenes.
-The first column shows the evolution of the current state of the
-First the text line “all streams lead to kafka” is being processed.
-The
-When the second text line “hello kafka streams” is processed, we observe, for the first time, that existing entries in the
-And so on (we skip the illustration of how the third line is being processed). This explains why the output topic has the contents we showed above, because it contains the full record of changes.
-
-Looking beyond the scope of this concrete example, what Kafka Streams is doing here is to leverage the duality between a table and a changelog stream (here: table = the KTable, changelog stream = the downstream KStream): you can publish every change of the table to a stream, and if you consume the entire changelog stream from beginning to end, you can reconstruct the contents of the table.
-
-Now you can write more input messages to the streams-file-input topic and observe additional messages added
-to streams-wordcount-output topic, reflecting updated word counts (e.g., using the console producer and the
-console consumer, as described above).
- You can stop the console consumer via Ctrl-C.Step 8: Use Kafka Streams to process data
WordCountDemo
example code (converted to use Java 8 lambda expressions for easy reading).
-
-// Serializers/deserializers (serde) for String and Long types
-final Serde<String> stringSerde = Serdes.String();
-final Serde<Long> longSerde = Serdes.Long();
-
-// Construct a `KStream` from the input topic ""streams-file-input", where message values
-// represent lines of text (for the sake of this example, we ignore whatever may be stored
-// in the message keys).
-KStream<String, String> textLines = builder.stream(stringSerde, stringSerde, "streams-file-input");
-
-KTable<String, Long> wordCounts = textLines
- // Split each text line, by whitespace, into words.
- .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("\\W+")))
-
- // Group the text words as message keys
- .groupBy((key, value) -> value)
-
- // Count the occurrences of each word (message key).
- .count("Counts")
-
-// Store the running counts as a changelog stream to the output topic.
-wordCounts.to(stringSerde, longSerde, "streams-wordcount-output");
-
-
-
-> echo -e "all streams lead to kafka\nhello kafka streams\njoin kafka summit" > file-input.txt
-
-Or on Windows:
-
-> echo all streams lead to kafka> file-input.txt
-> echo hello kafka streams>> file-input.txt
-> echo|set /p=join kafka summit>> file-input.txt
-
-
-
-> bin/kafka-topics.sh --create \
- --zookeeper localhost:2181 \
- --replication-factor 1 \
- --partitions 1 \
- --topic streams-file-input
-
-
-
-
-> bin/kafka-console-producer.sh --broker-list localhost:9092 --topic streams-file-input < file-input.txt
-
-
-
-> bin/kafka-run-class.sh org.apache.kafka.streams.examples.wordcount.WordCountDemo
-
-
-
-> bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 \
- --topic streams-wordcount-output \
- --from-beginning \
- --formatter kafka.tools.DefaultMessageFormatter \
- --property print.key=true \
- --property print.value=true \
- --property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer \
- --property value.deserializer=org.apache.kafka.common.serialization.LongDeserializer
-
-
-
-all 1
-lead 1
-to 1
-hello 1
-streams 2
-join 1
-kafka 3
-summit 1
-
-
-java.lang.String
format, and the second column is the message value in java.lang.Long
format.
-Note that the output is actually a continuous stream of updates, where each data record (i.e. each line in the original output above) is
-an updated count of a single word, aka record key such as "kafka". For multiple records with the same key, each later record is an update of the previous one.
-KTable<String, Long>
that is counting word occurrences for count
.
-The second column shows the change records that result from state updates to the KTable and that are being sent to the output Kafka topic streams-wordcount-output.
-
-
-
-
KTable
is being built up as each new word results in a new table entry (highlighted with a green background), and a corresponding change record is sent to the downstream KStream
.
-KTable
are being updated (here: for the words “kafka” and for “streams”). And again, change records are being sent to the output topic.
-num.standby.replicas
in the Kafka Streams Configs Section.
+ the task (re)initialization cost. See num.standby.replicas
in the Kafka Streams Configs section.
processing.guarantee
config value to exactly_once (default value is at_least_once).
- More details can be found in the Kafka Streams Configs section.
+ More details can be found in the Kafka Streams Configs section.
diff --git a/docs/streams/developer-guide.html b/docs/streams/developer-guide.html
index b8921948798..a113fd79058 100644
--- a/docs/streams/developer-guide.html
+++ b/docs/streams/developer-guide.html
@@ -504,7 +504,7 @@
A Kafka Streams application is typically running on many instances.
The state that is locally available on any given instance is only a subset of the application's entire state.
Querying the local stores on an instance will, by definition, only return data locally available on that particular instance.
- We explain how to access data in state stores that are not locally available in section Querying remote state stores (for the entire application).
+ We explain how to access data in state stores that are not locally available in section Querying remote state stores (for the entire application).
@@ -535,7 +535,7 @@ This read-only constraint is important to guarantee that the underlying state stores will never be mutated (e.g. new entries added) out-of-band, i.e. only the corresponding processing topology of Kafka Streams is allowed to mutate and update the state stores in order to ensure data consistency.
- You can also implement your own QueryableStoreType
as described in section Querying local custom stores
+ You can also implement your own QueryableStoreType
as described in section Querying local custom stores
diff --git a/docs/streams/index.html b/docs/streams/index.html index 2d301690a7f..7a3c36bd98b 100644 --- a/docs/streams/index.html +++ b/docs/streams/index.html @@ -21,6 +21,9 @@
- If you want to upgrade from 0.10.1.x to 0.10.2, see the Upgrade Section for 0.10.2. + If you want to upgrade from 0.10.1.x to 0.10.2, see the Upgrade Section for 0.10.2. It highlights incompatible changes you need to consider to upgrade your code and application. See below a complete list of 0.10.2 API and semantical changes that allow you to advance your application and/or simplify your code base, including the usage of new features.
- If you want to upgrade from 0.10.0.x to 0.10.1, see the Upgrade Section for 0.10.1. + If you want to upgrade from 0.10.0.x to 0.10.1, see the Upgrade Section for 0.10.1. It highlights incompatible changes you need to consider to upgrade your code and application. See below a complete list of 0.10.1 API changes that allow you to advance your application and/or simplify your code base, including the usage of new features.