インタラクティブクエリとステートストア
Kafka Streamsがローカルのステートストアを直接クエリできるように公開する方法を学び、ストリームアプリを低レイテンシのマテリアライズドビューへ変えます。
「インタラクティブクエリとステートストア」はCoddyKit上の無料Apache Kafka & Stream Processing Fundamentalsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.
よくある質問
「インタラクティブクエリとステートストア」レッスンは無料ですか?
はい。「インタラクティブクエリとステートストア」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Apache Kafka & Stream Processing Fundamentalsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Apache Kafka & Stream Processing Fundamentalsコースには全4レッスンが含まれています。
「インタラクティブクエリとステートストア」で何を学びますか?
Kafka Streamsがローカルのステートストアを直接クエリできるように公開する方法を学び、ストリームアプリを低レイテンシのマテリアライズドビューへ変えます。 ブラウザで直接実行するハンズオンコードでApache Kafka & Stream Processing Fundamentalsを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Kafka Streamsのウィンドウ処理
- ストリームの結合と集約
- ストリーム分析のためのKSQL入門
- インタラクティブクエリとステートストア