📖 Scenario: You are working on a simple Kafka Streams application that processes user activity data. The data comes as key-value pairs where the key is a user ID and the value is the activity type. You want to build a stream topology that reads from an input topic, processes the data, and writes to an output topic.
🎯 Goal: Build a Kafka Streams topology that reads from the user-activity-input topic, processes the stream by filtering only login activities, and writes the filtered stream to the user-login-output topic.
📋 What You'll Learn
Create a Kafka Streams builder
Define a stream from the user-activity-input topic
Filter the stream to keep only records where the value is login
Write the filtered stream to the user-login-output topic
💡 Why This Matters
🌍 Real World
Kafka Streams is widely used for real-time data processing in applications like monitoring user activity, fraud detection, and event-driven microservices.
💼 Career
Understanding stream topology is essential for roles involving real-time data engineering, backend development, and building scalable event-driven systems.
Progress0 / 4 steps
1
Create the Kafka Streams builder and input stream
Create a StreamsBuilder object called builder. Then create a stream called userActivityStream from the topic "user-activity-input" using builder.stream("user-activity-input").
Kafka
Hint
Use StreamsBuilder to create the builder. Then use builder.stream("user-activity-input") to get the stream.
2
Add a filter to keep only login activities
Create a new stream called loginStream by filtering userActivityStream to keep only records where the value equals "login". Use filter((key, value) -> value.equals("login")).
Kafka
Hint
Use the filter method on userActivityStream with a lambda that checks if value.equals("login").
3
Write the filtered stream to the output topic
Write the loginStream to the topic "user-login-output" using loginStream.to("user-login-output").
Kafka
Hint
Use the to method on loginStream to send data to the "user-login-output" topic.
4
Print the topology description
Create a Topology object by calling builder.build(). Then print the topology description using System.out.println(topology.describe()).
Kafka
Hint
Call builder.build() to get the topology, then print its description with System.out.println(topology.describe()).
Practice
(1/5)
1. What does a Kafka Streams topology represent?
easy
A. The configuration settings for Kafka brokers
B. The flow and processing steps of data in a Kafka Streams application
C. The list of Kafka topics available in the cluster
D. The schema of messages in a Kafka topic
Solution
Step 1: Understand the role of topology in Kafka Streams
A topology defines how data flows and is processed in a Kafka Streams app.
Step 2: Differentiate topology from other Kafka concepts
It is not about broker configs, topic lists, or message schemas, but about processing steps.
Final Answer:
The flow and processing steps of data in a Kafka Streams application -> Option B
Quick Check:
Topology = Data flow and processing [OK]
Hint: Topology means data flow and processing steps [OK]
Common Mistakes:
Confusing topology with Kafka broker settings
Thinking topology lists topics
Assuming topology defines message schema
2. Which code snippet correctly builds a Kafka Streams topology?
easy
A. StreamsBuilder builder = new StreamsBuilder(); builder.run();
B. StreamsBuilder builder = new StreamsBuilder(); builder.start();
C. StreamsBuilder builder = new StreamsBuilder(); builder.create();
D. StreamsBuilder builder = new StreamsBuilder(); builder.build();
Solution
Step 1: Recall the correct method to build topology
The method to finalize and get the topology is build().
Step 2: Check each option's method
Only StreamsBuilder builder = new StreamsBuilder(); builder.build(); uses build(), others use invalid methods.
Final Answer:
StreamsBuilder builder = new StreamsBuilder(); builder.build(); -> Option D
Quick Check:
Use build() to create topology [OK]
Hint: Use build() method on StreamsBuilder to create topology [OK]
Common Mistakes:
Using start(), create(), or run() instead of build()
Confusing topology building with starting the stream
A. The filtered stream is not sent to any sink topic, so no output node is created
B. The filter method is invalid and causes a syntax error
C. StreamsBuilder cannot build topology without a source topic
D. Calling build() before defining streams causes runtime error
Solution
Step 1: Check the stream processing chain
The code filters the stream but does not write the result to any output topic.
Step 2: Understand topology implications
Without a sink node, the topology has no output, which is usually an error or incomplete.
Final Answer:
The filtered stream is not sent to any sink topic, so no output node is created -> Option A
Quick Check:
Missing sink node causes incomplete topology [OK]
Hint: Always send processed stream to sink topic [OK]
Common Mistakes:
Assuming filter causes syntax error
Thinking build() requires no streams
Ignoring missing sink topic after processing
5. You want to create a Kafka Streams topology that reads from two topics, merges their streams, filters messages containing 'error', and writes to an output topic. Which code snippet correctly builds this topology?