Interactive Queries und State Stores
Lernen Sie, wie Kafka Streams seine lokalen State Stores für direkte Abfragen bereitstellt und dadurch eine Stream-Anwendung in eine latenzarme materialisierte Sicht verwandelt.
Interactive Queries und State Stores ist eine kostenlose Apache Kafka & Stream Processing Fundamentals-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Apache Kafka & Stream Processing Fundamentals-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Apache Kafka & Stream Processing Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
State Stores Recap
Stateful operations like aggregations and joins keep their data in state stores — local key-value stores backed by changelog topics for fault tolerance.
Normally results flow out to a topic, but they also live right inside your app.
What Are Interactive Queries?
Interactive Queries (IQ) let your application read those local state stores directly — no extra database, no re-consuming a topic.
Your streaming app effectively becomes a queryable materialized view.
Naming a Store
To query a store you must name it during materialization.
KTable<String, Long> counts = builder
.stream("clicks")
.groupByKey()
.count(Materialized.as("clicks-store"));Getting a Read Handle
After the app is running, fetch a read-only view of the store from the KafkaStreams instance.
ReadOnlyKeyValueStore<String, Long> store =
streams.store(StoreQueryParameters.fromNameAndType(
"clicks-store",
QueryableStoreTypes.keyValueStore()));Point Lookups & Range Scans
Once you have the store, query it like a map.
Long value = store.get("user-42");
KeyValueIterator<String, Long> all = store.all();
while (all.hasNext()) {
KeyValue<String, Long> kv = all.next();
}
all.close();The Distribution Problem
State is partitioned across app instances. A given key lives on only one instance.
If you query the wrong instance, you won't find the key — so the app needs to know who owns each key.
Discovering Key Owners
Kafka Streams can tell you which instance hosts a key, given the store name and key serializer.
KeyQueryMetadata meta = streams.queryMetadataForKey(
"clicks-store", "user-42", Serdes.String().serializer());
HostInfo host = meta.activeHost();Exposing application.server
Set application.server so each instance advertises its host and port. This metadata powers cross-instance routing.
props.put(StreamsConfig.APPLICATION_SERVER_CONFIG,
"node1.internal:8080");Building a Query REST Layer
A typical pattern: wrap the app in an HTTP server. On a request, find the owning host. If it's local, read the store; otherwise proxy to the remote instance.
Handling Rebalances
During rebalances, stores may be migrating and temporarily unavailable, raising InvalidStateStoreException.
- Retry with backoff.
- Check
KafkaStreams.State.RUNNINGbefore querying.
When to Use IQ
Interactive Queries shine when you want:
- Low-latency reads of aggregated state.
- To avoid a separate serving database.
- A self-contained, scalable materialized view.
For complex ad-hoc queries, a dedicated store may still be better.
Quick Check
Test your understanding of interactive queries.
Recap
You learned Interactive Queries.
- Name a store, then get a read-only handle from KafkaStreams.
- State is partitioned; use queryMetadataForKey to find owners.
- Set application.server and proxy cross-instance requests.
- Handle rebalance exceptions with retries.
Häufig gestellte Fragen
Ist die Lektion „Interactive Queries und State Stores“ kostenlos?
Ja — der vollständige Text von „Interactive Queries und State Stores“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Apache Kafka & Stream Processing Fundamentals-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Apache Kafka & Stream Processing Fundamentals-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Interactive Queries und State Stores“?
Lernen Sie, wie Kafka Streams seine lokalen State Stores für direkte Abfragen bereitstellt und dadurch eine Stream-Anwendung in eine latenzarme materialisierte Sicht verwandelt. Du übst Apache Kafka & Stream Processing Fundamentals mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Apache Kafka & Stream Processing Fundamentals zu starten?
Keine Vorkenntnisse erforderlich. Apache Kafka & Stream Processing Fundamentals auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Interactive Queries und State Stores“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Apache Kafka & Stream Processing Fundamentals-Lektion Code schreiben und ausführen?
Ja. Jede Apache Kafka & Stream Processing Fundamentals-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Fensteroperationen in Kafka Streams
- Joins und Aggregationen in Streams
- Einführung in KSQL für Stream-Analysen
- Interactive Queries und State Stores