From 7bfe008ae1a2bb2fa98c3a765be5022e75cd151d Mon Sep 17 00:00:00 2001 From: Eno Thereska Date: Sun, 9 Jul 2017 17:26:11 -0700 Subject: [PATCH] MINOR: Move quickstart under streams Author: Eno Thereska Reviewers: Michael G. Noll , Damian Guy , Guozhang Wang Closes #3494 from enothereska/minor-quickstart-docs rename section name and bold font for section names --- docs/documentation/streams/quickstart.html | 19 ++ docs/quickstart.html | 167 +------------ docs/streams/architecture.html | 2 +- docs/streams/core-concepts.html | 4 +- docs/streams/developer-guide.html | 4 +- docs/streams/index.html | 5 +- docs/streams/quickstart.html | 258 +++++++++++++++++++++ docs/streams/upgrade-guide.html | 4 +- 8 files changed, 294 insertions(+), 169 deletions(-) create mode 100644 docs/documentation/streams/quickstart.html create mode 100644 docs/streams/quickstart.html diff --git a/docs/documentation/streams/quickstart.html b/docs/documentation/streams/quickstart.html new file mode 100644 index 00000000000..f69c0d5ae75 --- /dev/null +++ b/docs/documentation/streams/quickstart.html @@ -0,0 +1,19 @@ + + + + diff --git a/docs/quickstart.html b/docs/quickstart.html index 40aa2fbe1b4..c50df29c80e 100644 --- a/docs/quickstart.html +++ b/docs/quickstart.html @@ -280,169 +280,14 @@ data in the topic (or use custom consumer code to process it):

Step 8: Use Kafka Streams to process data

-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 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");
-
- -

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

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

-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): -

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

-We can now run the WordCount demo application to process the input data: -

- -
-> bin/kafka-run-class.sh org.apache.kafka.streams.examples.wordcount.WordCountDemo
-
- -

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

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

-with the following output data being printed to the console: -

- -
-all     1
-lead    1
-to      1
-hello   1
-streams 2
-join    1
-kafka   3
-summit  1
-
- -

-Here, the first column is the Kafka message key in 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. -

- -

-The two diagrams below illustrate what is essentially happening behind the scenes. -The first column shows the evolution of the current state of the 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. -

- - - - -

-First the text line “all streams lead to kafka” is being processed. -The 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. -

-

-When the second text line “hello kafka streams” is processed, we observe, for the first time, that existing entries in the KTable are being updated (here: for the words “kafka” and for “streams”). And again, change records are being sent to the output topic. -

-

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

diff --git a/docs/streams/architecture.html b/docs/streams/architecture.html index f0def6ac491..70c5c79f522 100644 --- a/docs/streams/architecture.html +++ b/docs/streams/architecture.html @@ -126,7 +126,7 @@ Note that the cost of task (re)initialization typically depends primarily on the time for restoring the state by replaying the state stores' associated changelog topics. To minimize this restoration time, users can configure their applications to have standby replicas of local states (i.e. fully replicated copies of the state). When a task migration happens, Kafka Streams then attempts to assign a task to an application instance where such a standby replica already exists in order to minimize - the task (re)initialization cost. See num.standby.replicas in the Kafka Streams Configs Section. + the task (re)initialization cost. See num.standby.replicas in the Kafka Streams Configs section.