0
0
Kafkadevops~20 mins

Interactive queries in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Interactive Queries 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 interactive query?
Consider a Kafka Streams application with a state store named user-store. The following code snippet queries the store for a user ID and prints the result.

ReadOnlyKeyValueStore<String, String> store = streams.store(StoreQueryParameters.fromNameAndType("user-store", QueryableStoreTypes.keyValueStore()));
String user = store.get("user123");
System.out.println(user);

Assuming the store contains the entry {"user123": "Alice"}, what will be printed?
Kafka
ReadOnlyKeyValueStore<String, String> store = streams.store(StoreQueryParameters.fromNameAndType("user-store", QueryableStoreTypes.keyValueStore()));
String user = store.get("user123");
System.out.println(user);
AAlice
Bnull
Cuser123
DThrows a NullPointerException
Attempts:
2 left
💡 Hint
The store.get() method returns the value associated with the key or null if not found.
🧠 Conceptual
intermediate
1:30remaining
Which statement about Kafka Streams interactive queries is true?
Select the correct statement about interactive queries in Kafka Streams.
AInteractive queries require stopping the Kafka Streams application to access state.
BInteractive queries allow querying the local state store of a Kafka Streams instance.
CInteractive queries can only be performed on remote Kafka brokers, not local stores.
DInteractive queries automatically replicate state stores to all instances.
Attempts:
2 left
💡 Hint
Think about where the state store lives and how queries access it.
🔧 Debug
advanced
2:30remaining
Why does this interactive query code throw an exception?
Given the following code snippet:

ReadOnlyKeyValueStore<String, String> store = streams.store("user-store", QueryableStoreTypes.keyValueStore());
String user = store.get("user123");
System.out.println(user);

When running, it throws a IllegalArgumentException. What is the cause?
AThe key "user123" does not exist in the store, causing the exception.
BThe QueryableStoreTypes.keyValueStore() is not supported for interactive queries.
CThe store name must be passed using StoreQueryParameters, not as separate arguments.
DThe streams object is not started before querying the store.
Attempts:
2 left
💡 Hint
Check the method signature for streams.store() in recent Kafka versions.
📝 Syntax
advanced
2:30remaining
Which code snippet correctly performs an interactive query to get all entries from a key-value store?
You want to iterate over all entries in a local key-value store named user-store. Which snippet is correct?
A
ReadOnlyKeyValueStore&lt;String, String&gt; store = streams.store(StoreQueryParameters.fromNameAndType("user-store", QueryableStoreTypes.keyValueStore()));
KeyValueIterator&lt;String, String&gt; iter = store.all();
while (iter.hasNext()) {
  KeyValue&lt;String, String&gt; kv = iter.next();
  System.out.println(kv.key + ": " + kv.value);
}
iter.close();
B
ReadOnlyKeyValueStore&lt;String, String&gt; store = streams.store("user-store", QueryableStoreTypes.keyValueStore());
for (KeyValue&lt;String, String&gt; kv : store) {
  System.out.println(kv.key + ": " + kv.value);
}
C
KeyValueIterator&lt;String, String&gt; iter = streams.store(StoreQueryParameters.fromNameAndType("user-store", QueryableStoreTypes.keyValueStore())).all();
for (KeyValue&lt;String, String&gt; kv : iter) {
  System.out.println(kv.key + ": " + kv.value);
}
D
ReadOnlyKeyValueStore&lt;String, String&gt; store = streams.store(StoreQueryParameters.fromNameAndType("user-store", QueryableStoreTypes.keyValueStore()));
Iterator&lt;KeyValue&lt;String, String&gt;&gt; iter = store.iterator();
while (iter.hasNext()) {
  KeyValue&lt;String, String&gt; kv = iter.next();
  System.out.println(kv.key + ": " + kv.value);
}
Attempts:
2 left
💡 Hint
Check the correct method to get all entries and how to iterate the KeyValueIterator.
🚀 Application
expert
3:00remaining
How to implement a fault-tolerant interactive query across multiple Kafka Streams instances?
You have a Kafka Streams application running on 3 instances, each with a local state store user-store. You want to query the state for a user ID, but the key may be on any instance. Which approach ensures fault-tolerant interactive queries?
AStore all keys redundantly on all instances to query any local store directly.
BQuery the local store on each instance sequentially until the key is found or all checked.
CBroadcast the query to all instances simultaneously and merge results client-side.
DUse the Kafka Streams Metadata API to find the instance hosting the key, then query that instance's store via a REST call.
Attempts:
2 left
💡 Hint
Think about how Kafka Streams partitions state and how to locate the correct instance.