0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · Lekcja

Okna czasowe i agregacje stanowe w Kafka Streams

Nauczą się Państwo wykonywać agregacje w oknach czasowych i zarządzać magazynami stanu w Kafka Streams, aby obliczać bieżące zliczenia, sumy i metryki dla strumieni zdarzeń.

Okna czasowe i agregacje stanowe w Kafka Streams to bezpłatna lekcja Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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 Advanced Spring Boot 4: Event-Driven Architecture (Kafka), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Advanced Spring Boot 4: Event-Driven Architecture (Kafka) zawiera 4 lekcji w sumie.

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

Stateful vs Stateless

Operations like map and filter are stateless. Aggregations such as counting or summing require state that persists across records.

Kafka Streams manages this state for you in local state stores.

Grouping a Stream

Before aggregating you group records by key with groupByKey or groupBy.

KGroupedStream<String, Order> grouped =
    orders.groupBy((key, order) -> order.getCustomerId());

Counting per Key

A simple aggregation: count orders per customer. The result is a KTable backed by a state store.

KTable<String, Long> counts = grouped.count();

General Aggregation

Use aggregate for custom accumulation. You provide an initializer and an adder function.

KTable<String, Double> totals = grouped.aggregate(
    () -> 0.0,
    (key, order, sum) -> sum + order.getAmount());

Introducing Windows

Often you want aggregations over a time window, e.g. orders in the last 5 minutes. Kafka Streams supports tumbling, hopping, and session windows.

Tumbling Windows

A tumbling window is fixed-size and non-overlapping. Each record belongs to exactly one window.

grouped
  .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(5)))
  .count();

Hopping Windows

Hopping windows have a size and a smaller advance, so they overlap. A record can fall into multiple windows.

TimeWindows.ofSizeAndGrace(Duration.ofMinutes(5), Duration.ofSeconds(30))
           .advanceBy(Duration.ofMinutes(1));

Session Windows

Session windows group records separated by gaps of inactivity — ideal for user sessions where activity bursts then pauses.

grouped
  .windowedBy(SessionWindows.ofInactivityGapWithNoGrace(Duration.ofMinutes(10)))
  .count();

State Store Fault Tolerance

State stores are backed by compacted changelog topics in Kafka. If an instance fails, its state is rebuilt from the changelog on another instance.

Querying State Interactively

Interactive Queries let you read state store values directly from the application, exposing aggregations via a REST endpoint without an external database.

ReadOnlyKeyValueStore<String, Long> store =
    streams.store(StoreQueryParameters.fromNameAndType(
        "counts", QueryableStoreTypes.keyValueStore()));

Putting It Together

Windowed, stateful aggregations turn raw event streams into live metrics. Group by key, choose a window type, aggregate, and optionally expose the state via interactive queries.

Quick Check

Test your understanding of windowing.

Recap

You learned windowing and stateful aggregations.

  • Group with groupByKey before aggregating.
  • Use count or aggregate to build a KTable.
  • Tumbling, hopping, and session windows handle time differently.
  • State stores are fault-tolerant via changelog topics.

Często zadawane pytania

Czy lekcja „Okna czasowe i agregacje stanowe w Kafka Streams” jest bezpłatna?

Tak — pełny tekst „Okna czasowe i agregacje stanowe w Kafka Streams” 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 Advanced Spring Boot 4: Event-Driven Architecture (Kafka), przejdź na CoddyKit PRO. Kurs Advanced Spring Boot 4: Event-Driven Architecture (Kafka) zawiera 4 lekcji w sumie.

Co nauczysz się w „Okna czasowe i agregacje stanowe w Kafka Streams”?

Nauczą się Państwo wykonywać agregacje w oknach czasowych i zarządzać magazynami stanu w Kafka Streams, aby obliczać bieżące zliczenia, sumy i metryki dla strumieni zdarzeń. Ćwiczysz Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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ąć Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?

Nie wymagamy żadnego doświadczenia. Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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 „Okna czasowe i agregacje stanowe w Kafka Streams”?

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 Advanced Spring Boot 4: Event-Driven Architecture (Kafka)?

Tak. Każda lekcja Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 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. Wprowadzenie do Kafka Streams
  2. Przetwarzanie strumieni za pomocą KStream i KTable
  3. Tworzenie prostej aplikacji strumieniowej
  4. Okna czasowe i agregacje stanowe w Kafka Streams
← Powrót do Advanced Spring Boot 4: Event-Driven Architecture (Kafka)