What if you could draw your data flow like a map and let the system handle the hard work?
Why Stream topology in Kafka? - Purpose & Use Cases
Imagine you have many streams of data coming from different sources like sensors, user clicks, and transactions. You want to process them step-by-step to get useful results. Doing this by hand means writing separate code for each step and managing how data moves between them.
Manually handling each data stream and its processing steps is slow and confusing. It's easy to make mistakes like mixing up data order or losing messages. Also, updating or changing the process means rewriting lots of code, which wastes time and causes bugs.
Stream topology lets you define the whole flow of data processing as a clear map. You describe how streams connect and transform, and the system handles the details. This makes your code simpler, more reliable, and easier to change.
read stream A process data write to stream B read stream B process data write to stream C
builder.stream('A').map(...).to('B'); builder.stream('B').filter(...).to('C');
It enables building complex, real-time data pipelines that are easy to understand, maintain, and scale.
Think of a ride-sharing app that processes driver locations, rider requests, and payments in real time. Stream topology helps connect these data flows smoothly to match riders with drivers instantly.
Manual stream processing is complicated and error-prone.
Stream topology maps out data flow clearly and simply.
This approach makes real-time data processing reliable and flexible.
Practice
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 BQuick Check:
Topology = Data flow and processing [OK]
- Confusing topology with Kafka broker settings
- Thinking topology lists topics
- Assuming topology defines message schema
Solution
Step 1: Recall the correct method to build topology
The method to finalize and get the topology isbuild().Step 2: Check each option's method
Only StreamsBuilder builder = new StreamsBuilder(); builder.build(); usesbuild(), others use invalid methods.Final Answer:
StreamsBuilder builder = new StreamsBuilder(); builder.build(); -> Option DQuick Check:
Use build() to create topology [OK]
- Using start(), create(), or run() instead of build()
- Confusing topology building with starting the stream
StreamsBuilder builder = new StreamsBuilder();
builder.stream("input-topic").mapValues(value -> value.toUpperCase()).to("output-topic");
Topology topology = builder.build();
System.out.println(topology.describe());What will the printed topology description include?
Solution
Step 1: Analyze the stream processing steps
The code reads from 'input-topic', applies mapValues to uppercase, then writes to 'output-topic'.Step 2: Understand topology description output
The description shows source nodes (input), processor nodes (mapValues), and sink nodes (output).Final Answer:
A source node for 'input-topic', a processor node for mapValues, and a sink node for 'output-topic' -> Option CQuick Check:
Topology describe shows source, processor, sink [OK]
- Thinking mapValues is not part of topology
- Confusing source and sink nodes
- Expecting no processor nodes in description
StreamsBuilder builder = new StreamsBuilder();
builder.stream("input-topic").filter(value -> value.length() > 5);
Topology topology = builder.build();
System.out.println(topology.describe());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 AQuick Check:
Missing sink node causes incomplete topology [OK]
- Assuming filter causes syntax error
- Thinking build() requires no streams
- Ignoring missing sink topic after processing
Solution
Step 1: Understand the requirement
Read from two topics, merge streams, filter messages with 'error', write to output.Step 2: Evaluate each option
StreamsBuilder builder = new StreamsBuilder(); KStream stream1 = builder.stream("topic1"); KStream stream2 = builder.stream("topic2"); KStream merged = stream1.merge(stream2); merged.filter((k, v) -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); merges streams correctly, filters, then writes once. StreamsBuilder builder = new StreamsBuilder(); KStream stream = builder.stream("topic1", "topic2"); stream.filter(v -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); incorrectly tries to stream two topics in one call. StreamsBuilder builder = new StreamsBuilder(); KStream stream1 = builder.stream("topic1"); KStream stream2 = builder.stream("topic2"); KStream merged = stream1.join(stream2, (v1, v2) -> v1 + v2); merged.filter((k, v) -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); joins streams (not merge). StreamsBuilder builder = new StreamsBuilder(); builder.stream("topic1").filter(v -> v.contains("error")).to("output-topic"); builder.stream("topic2").filter(v -> v.contains("error")).to("output-topic"); Topology topology = builder.build(); filters separately and writes twice, not merging.Final Answer:
Option A correctly merges, filters, and writes to output topic -> Option AQuick Check:
Merge streams, filter, then write [OK]
- Using join instead of merge for combining streams
- Trying to stream multiple topics in one call incorrectly
- Writing filtered streams separately without merging
Ready to prove it?
Pass the test and this pattern moves unknown
