#
JAVA: Kafka KTables Explained
This tutorial explains to you what a Kafka KTables stream is.
Kafka Streams is a library for building streaming applications, specifically applications that transform input Kafka topics into output Kafka topics (or call to external services, updates to databases, etc).
A KTable
is an abstraction of a changelog stream, where each data record represents an update. More precisely,
the value in a data record is considered to be an update of the last value for the same record key, if any.
If the corresponding key doesn't exist yet, the update will be considered a 'create'). Using the table analogy,
a data record in a changelog stream is interpreted as an update because any existing row with the same
key is overwritten. This definition was taken from confluent.io.
KTable is created from a KStream in order to group the date by the key
and apply some aggregations on that.
A classic example is when we count the words from an input topic.
Here is an example of doing that.
Below you can see a picture which show you how to transform the KStreams into a KTable stream.
Also, if you want to run the code successfully, you have to add in your pom.xml the following dependency:
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.0</version>
</dependency>
After the "input-kafka-topic" topic is created you can run the Java code and start a producer.
Input some word into the message.
In the output stream you will see how the words are count and displayed on the screen.