Featured Article

Kafka Streams Tutorial Java Mastering Real Time Data Processing In 2024

Kenneth Jul 13, 2026

Apache Kafka is a powerful open-source stream-processing platform that handles real-time data feeds. If you're a Java developer eager to dive into the world of Kafka Streams, you've come to the right place. This comprehensive tutorial will guide you through understanding Kafka Streams, its benefits, and hands-on examples to build stream processing applications in Java.

Brij kishore Pandey on LinkedIn: ๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—ž๐—ฎ๐—ณ๐—ธ๐—ฎ? Apache Kafka, initially developed by LinkedInโ€ฆ | 25 comments
Brij kishore Pandey on LinkedIn: ๐—ช๐—ต๐—ฎ๐˜ ๐—ถ๐˜€ ๐—ž๐—ฎ๐—ณ๐—ธ๐—ฎ? Apache Kafka, initially developed by LinkedInโ€ฆ | 25 comments

Before we delve into the code, let's ensure you have a good grasp of what Kafka Streams is. In essence, Kafka Streams is a customer library for building highly scalable, distributed, and fault-tolerant stream processing applications on top of Kafka. It enables Java developers to build applications that read from, transform, and write to Kafka topics in real-time.

Kafka intro
Kafka intro

Getting Started with Kafka Streams Java API

To start your Kafka Streams journey, you'll first need to add the Kafka Streams dependency to your project. If you're using Maven, add this to your pom.xml:

Alex Xu (@alexxubyte) on X
Alex Xu (@alexxubyte) on X

<dependency>
  <groupId>org.apache.kafka</groupId>
  <artifactId>kafka-streams</artifactId>
  <version>2.7.1</version>
</dependency>

Setting up the Kafka Streams Developer Local Environment

kafka
kafka

For local development, you'll need to set up a single-node Kafka cluster. Download and install the latest version of Kafka from the official website. Start the ZooKeeper server and the Kafka server using the provided scripts.

Create a new topic named streams-plaintext-input and streams-plaintext-output for testing purposes. Verify the topic creation by listing the available topics in the Kafka server.

Building a Simple Kafka Streams Processor Topology

the diagram shows how kafka works and what it is like to work with them
the diagram shows how kafka works and what it is like to work with them

A Kafka Streams application is built around a processor topology, a series of processing steps defined by a KStream or KTable. Start by importing the necessary Kafka Streams dependencies:

import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;

Define the processor topology in the buildTopology method:

public static final String INPUT_TOPIC = "streams-plaintext-input";
public static final String OUTPUT_TOPIC = "streams-plaintext-output";

public static Topology buildTopology() {
    final StreamsBuilder builder = new StreamsBuilder();

    // Read from the input topic
    KStream<String, String> input = builder.stream(INPUT_TOPIC);

    // Perform processing using a simple transformation
    KStream<String, String> output = input
        .transform(() -> new Processor<String, String>() {
            @Override
            public void init(ProcessorContext context) { }

            @Override
            public void process(String key, String value) {
                context.forward(key, value.toUpperCase());
            }

            @Override
            public void close() { }
        });

    // Write to the output topic
    output.to(OUTPUT_TOPIC);

    return builder.build();
}

Configuring and Running the Kafka Streams Application

GitHub - kaiwaehner/kafka-streams-machine-learning-examples: This project contains examples which demonstrate how to deploy analytic models to mission-critical, scalable production environments leveraging Apache Kafka and its Streams API. Models are built with Python, H2O, TensorFlow, Keras, DeepLearning4 and other technologies.
GitHub - kaiwaehner/kafka-streams-machine-learning-examples: This project contains examples which demonstrate how to deploy analytic models to mission-critical, scalable production environments leveraging Apache Kafka and its Streams API. Models are built with Python, H2O, TensorFlow, Keras, DeepLearning4 and other technologies.

Configure the Kafka Streams properties, including the bootstrap servers and input/output topics:

Properties config = new Properties();
config.setProperty(StreamsConfig.APPLICATION_ID_CONFIG, "wordcount-application");
config.setProperty(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
config.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

config.setProperty(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());
config.setProperty(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass().getName());

Create an instance of KafkaStreams and start the application:

โ†’ Top 6 Kafka Use Cases
โ†’ Top 6 Kafka Use Cases
the top 5 kafka use cases
the top 5 kafka use cases
the kafka at user diagram
the kafka at user diagram
Kafka Streams FTW
Kafka Streams FTW
Data Science
Data Science
Shubham Wadekar on LinkedIn: Master Kafka in 8 Easy Steps! Here's a quick breakdown: 1. What isโ€ฆ | 39 comments
Shubham Wadekar on LinkedIn: Master Kafka in 8 Easy Steps! Here's a quick breakdown: 1. What isโ€ฆ | 39 comments
Kafka Internals - Apache Kafka
Kafka Internals - Apache Kafka
Putting Apache Kafka To Use: A Practical Guide To Building an Event Streaming Platform (Part 1) | Confluent
Putting Apache Kafka To Use: A Practical Guide To Building an Event Streaming Platform (Part 1) | Confluent

KafkaStreams streams = new KafkaStreams(buildTopology(), config);
streams.start();
Runtime.getRuntime().addShutdownHook(new Thread(streams::close));

Rolling Upstream and Downstream Updates in Kafka Streams

Kafka Streams provides twoosz Atlantis APIs for handling state: KTable and GlobalKTable. In this section, we'll explore how to create a simple KTable and how to update it with incoming data.

Creating a Simple KTable

Create a KTable by grouping a stream of records by key and aggregating them using a GroupedKStream:

KStream<String, String> input = builder.stream(INPUT_TOPIC);

KTable<String, Long> wordCounts = input
    .groupByKey(Grouped.with(Serdes.String(), Serdes.String()))
    .reduce((agg, newVal) -> {
        Long count = agg.get() + 1;
        return new KeyValue<String, Long>(agg.key(), count);
    }, Materialized.as("wordcounts-store"));

Updating a KTable

To update the KTable, merge it with an incoming stream of updates:

KStream<String, Long> updates = builder.stream("wordcount-updates-topic");

wordCounts.merge(updates);

Remember to produce records to the wordcount-updates-topic to see updates reflected in the KTable.

Embracing Kafka Streams opens up a world of possibilities for real-time data processing in Java. Whether you're processing logs, monitoring key metrics, or building Bitcoin ticker applications, Kafka Streams has you covered. Start exploring Kafka Streams today and unlock the power of stream processing in your next Java project!