0Pricing
Apache Kafka & Stream Processing Fundamentals · Lekcja

Zapytania interaktywne i magazyny stanu

Dowiedzą się Państwo, jak Kafka Streams udostępnia lokalne magazyny stanu do bezpośredniego odpytywania, zmieniając aplikację strumieniową w zmaterializowany widok o małych opóźnieniach.

Zapytania interaktywne i magazyny stanu to bezpłatna lekcja Apache Kafka & Stream Processing Fundamentals na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Apache Kafka & Stream Processing Fundamentals, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Apache Kafka & Stream Processing Fundamentals zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.RUNNING before 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.

Często zadawane pytania

Czy lekcja „Zapytania interaktywne i magazyny stanu” jest bezpłatna?

Tak — pełny tekst „Zapytania interaktywne i magazyny stanu” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Apache Kafka & Stream Processing Fundamentals, przejdź na CoddyKit PRO. Kurs Apache Kafka & Stream Processing Fundamentals zawiera 4 lekcji w sumie.

Co nauczysz się w „Zapytania interaktywne i magazyny stanu”?

Dowiedzą się Państwo, jak Kafka Streams udostępnia lokalne magazyny stanu do bezpośredniego odpytywania, zmieniając aplikację strumieniową w zmaterializowany widok o małych opóźnieniach. Ćwiczysz Apache Kafka & Stream Processing Fundamentals z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Apache Kafka & Stream Processing Fundamentals?

Nie wymagamy żadnego doświadczenia. Apache Kafka & Stream Processing Fundamentals w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Zapytania interaktywne i magazyny stanu”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Apache Kafka & Stream Processing Fundamentals?

Tak. Każda lekcja Apache Kafka & Stream Processing Fundamentals zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Operacje okien czasowych w Kafka Streams
  2. Złączenia i agregacje w strumieniach
  3. Wprowadzenie do KSQL na potrzeby analizy strumieni
  4. Zapytania interaktywne i magazyny stanu
← Powrót do Apache Kafka & Stream Processing Fundamentals