交互式查询与状态存储
学习 Kafka Streams 如何开放本地状态存储以供直接查询,将流应用转变为低延迟物化视图。
交互式查询与状态存储 是 CoddyKit 上的免费 Apache Kafka & Stream Processing Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Apache Kafka & Stream Processing Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「交互式查询与状态存储」课时是免费的吗?
是的 — 「交互式查询与状态存储」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Apache Kafka & Stream Processing Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Apache Kafka & Stream Processing Fundamentals 课程共包含 4 节课。
「交互式查询与状态存储」这节课中我会学到什么?
学习 Kafka Streams 如何开放本地状态存储以供直接查询,将流应用转变为低延迟物化视图。 你通过在浏览器中直接运行的动手代码来练习 Apache Kafka & Stream Processing Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Apache Kafka & Stream Processing Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Apache Kafka & Stream Processing Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「交互式查询与状态存储」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Apache Kafka & Stream Processing Fundamentals 课中编写并运行代码吗?
能。每节 Apache Kafka & Stream Processing Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。