0
0
Kafkadevops~20 mins

State stores in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka State Store Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kafka Streams state store code?
Consider the following Kafka Streams processor code snippet that uses a state store to count occurrences of keys. What will be the printed output after processing the input records?
Kafka
StreamsBuilder builder = new StreamsBuilder();

KeyValueBytesStoreSupplier storeSupplier = Stores.inMemoryKeyValueStore("counts-store");

KStream<String, String> input = builder.stream("input-topic");

input.groupByKey()
     .count(Materialized.<String, Long>as(storeSupplier))
     .toStream()
     .foreach((key, count) -> System.out.println(key + ": " + count));

// Assume input records processed in order: ("apple", "1"), ("banana", "1"), ("apple", "1")
A
apple: 1
banana: 1
apple: 2
B
apple: 1
banana: 1
apple: 1
C
banana: 1
apple: 2
apple: 3
D
apple: 2
banana: 1
apple: 3
Attempts:
2 left
💡 Hint
Think about how the count state store updates counts for each key as records arrive.
🧠 Conceptual
intermediate
1:30remaining
Which statement best describes a Kafka Streams state store?
Choose the correct description of a Kafka Streams state store.
AA temporary cache that is cleared after each record is processed.
BA Kafka topic used to store intermediate results.
CA remote database that Kafka Streams connects to for storing data.
DA local storage that holds data for stream processing and can be queried during processing.
Attempts:
2 left
💡 Hint
Think about where Kafka Streams keeps data it needs to remember during processing.
🔧 Debug
advanced
2:30remaining
Why does this Kafka Streams code throw a NullPointerException?
Examine the following code snippet that uses a state store. Why does it throw a NullPointerException at runtime?
Kafka
StreamsBuilder builder = new StreamsBuilder();

builder.addStateStore(Stores.keyValueStoreBuilder(
    Stores.inMemoryKeyValueStore("my-store"),
    Serdes.String(),
    Serdes.Long()));

KStream<String, String> stream = builder.stream("input-topic");

stream.process(() -> new Processor<String, String>() {
    private KeyValueStore<String, Long> store;
    @Override
    public void init(ProcessorContext context) {
        store = (KeyValueStore<String, Long>) context.getStateStore("my-store");
    }
    @Override
    public void process(String key, String value) {
        Long count = store.get(key);
        store.put(key, (count == null ? 0L : count) + 1);
    }
    @Override
    public void close() {}
}, "my-store");
AThe store.get(key) returns null and causes a NullPointerException.
BThe state store was not added to the topology before usage.
CThe processor did not specify the state store name in the process() method call.
DThe state store is not initialized because the init() method is missing.
Attempts:
2 left
💡 Hint
Check how the state store is connected to the processor.
📝 Syntax
advanced
1:30remaining
Which option correctly creates a persistent key-value state store in Kafka Streams?
Select the correct Java code snippet that creates a persistent RocksDB key-value state store named "persistent-store".
AStores.persistentKeyValueStore("persistent-store")
BStores.inMemoryKeyValueStore("persistent-store")
CStores.persistentWindowStore("persistent-store", Duration.ofMinutes(5), 1, false)
DStores.persistentTimestampedKeyValueStore("persistent-store")
Attempts:
2 left
💡 Hint
Look for the method that creates a persistent key-value store, not in-memory or windowed.
🚀 Application
expert
2:00remaining
How many entries will the state store contain after processing these records?
Given a Kafka Streams application with a key-value state store that counts occurrences of keys, and the following input records processed in order: ("cat", "1"), ("dog", "1"), ("cat", "1"), ("bird", "1"), ("dog", "1"), ("cat", "1") How many unique keys will the state store contain after processing all records?
A6
B3
C2
D1
Attempts:
2 left
💡 Hint
Count how many different keys appear in the input records.