0Pricing
Apache Kafka & Stream Processing Fundamentals · Ders

Kafka Streams'te Pencereleme İşlemleri

Kafka Streams'te olayları toplamak için zaman tabanlı pencereler tanımlamayı; devrilen, kayan ve sürgülü pencereleri öğrenin.

Kafka Streams'te Pencereleme İşlemleri, CoddyKit'te ücretsiz bir Apache Kafka & Stream Processing Fundamentals dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Apache Kafka & Stream Processing Fundamentals öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Apache Kafka & Stream Processing Fundamentals kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

Intro to Stream Windows

In stream processing, data arrives continuously. To perform calculations like "total sales per hour" or "average temperature every 5 minutes," we need to group these continuous events into finite, manageable segments.

This grouping of events based on time is called windowing. It allows us to apply aggregations and transformations over specific periods.

Why Windowing Matters

Imagine analyzing website clicks. You might want to know:

  • How many clicks happened in the last minute?
  • What's the average user activity over the past 5 minutes, updated every minute?
  • When did a user become inactive?

Windowing provides the framework to answer these questions by defining boundaries around your data streams.

Event Time vs. Processing Time

Kafka Streams primarily uses event time. This is the timestamp embedded in the event itself, indicating when the event actually happened at the source.

  • Event Time: When the event occurred (preferred).
  • Processing Time: When the event is processed by the stream application (less reliable for ordering).

Using event time ensures that results are consistent, even if events arrive out of order or with delays.

Tumbling Windows: Fixed & Non-Overlapping

Tumbling windows are like non-overlapping, fixed-size buckets of time. Each event belongs to exactly one window.

  • They are fixed in duration (e.g., 5 minutes).
  • They do not overlap.
  • They are contiguous, covering all time.

Think of them as a series of distinct time slots, where each event falls into one specific slot.

Tumbling Window Code

Here's how to define a 5-second tumbling window in Kafka Streams to count messages. We use TimeWindows.of() and count().

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.TimeWindows;
import java.time.Duration;
import java.util.Properties;

public class TumblingWindowApp {

  public static void main(String[] args) {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "tumbling-window-app");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

    StreamsBuilder builder = new StreamsBuilder();
    builder.stream("input-topic", Consumed.with(Serdes.String(), Serdes.String()))
           .groupByKey()
           .windowedBy(TimeWindows.of(Duration.ofSeconds(5))) // 5-second tumbling window
           .count(Materialized.as("tumble-counts"))
           .toStream()
           .print(org.apache.kafka.streams.kstream.Printed.toSysOut());

    KafkaStreams streams = new KafkaStreams(builder.build(), props);
    streams.start();
    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
  }
}

Hopping Windows: Overlapping & Moving

Hopping windows are also fixed-size, but they can overlap. They "hop" forward by a specified interval, which is typically smaller than the window size.

  • Fixed size (e.g., 10 minutes).
  • Overlap with previous/next windows.
  • Hop interval (e.g., moves every 5 minutes).

This allows for smoother, more frequently updated aggregations, as a new window result is emitted more often.

Hopping Window Code

Here's how to create a 10-second hopping window that advances every 5 seconds. Events will be counted in multiple overlapping windows.

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.TimeWindows;
import java.time.Duration;
import java.util.Properties;

public class HoppingWindowApp {

  public static void main(String[] args) {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "hopping-window-app");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

    StreamsBuilder builder = new StreamsBuilder();
    builder.stream("input-topic", Consumed.with(Serdes.String(), Serdes.String()))
           .groupByKey()
           .windowedBy(TimeWindows.of(Duration.ofSeconds(10)) // 10-second window
                                  .advanceBy(Duration.ofSeconds(5))) // hops every 5 seconds
           .count(Materialized.as("hop-counts"))
           .toStream()
           .print(org.apache.kafka.streams.kstream.Printed.toSysOut());

    KafkaStreams streams = new KafkaStreams(builder.build(), props);
    streams.start();
    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
  }
}

Session Windows: Data-Driven Activity

Session windows are different. They are data-driven, not fixed-time. They group events that occur within a specified "inactivity gap."

  • Window closes if no new event for a key arrives within the gap duration.
  • Variable size, non-overlapping for a given key.
  • Useful for user activity tracking or network sessions.

If a new event arrives for the same key after the inactivity gap, a new session window starts.

Session Window Code

This example shows how to define a session window with a 5-second inactivity gap. If a key doesn't receive an event for 5 seconds, its current session window closes.

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.Consumed;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.SessionWindows;
import java.time.Duration;
import java.util.Properties;

public class SessionWindowApp {

  public static void main(String[] args) {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG, "session-window-app");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());

    StreamsBuilder builder = new StreamsBuilder();
    builder.stream("input-topic", Consumed.with(Serdes.String(), Serdes.String()))
           .groupByKey()
           .windowedBy(SessionWindows.with(Duration.ofSeconds(5))) // 5-second inactivity gap
           .count(Materialized.as("session-counts"))
           .toStream()
           .print(org.apache.kafka.streams.kstream.Printed.toSysOut());

    KafkaStreams streams = new KafkaStreams(builder.build(), props);
    streams.start();
    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
  }
}

Grace Period for Late Events

Events don't always arrive in order or on time. Kafka Streams handles this with a grace period.

  • It's extra time a window stays open to accept late-arriving records.
  • Defined as part of the window definition (e.g., .grace(Duration.ofSeconds(10))).
  • Events arriving after the grace period are typically dropped or forwarded to a late record topic.

This helps ensure accuracy for aggregations over event time.

Check Your Understanding

Which of the following statements about Kafka Streams windowing are TRUE?

Recap: Windowing in Kafka Streams

We've explored how windowing allows us to perform time-based aggregations on continuous data streams in Kafka Streams.

  • Tumbling windows: Fixed, non-overlapping.
  • Hopping windows: Fixed, overlapping, advance by an interval.
  • Session windows: Data-driven, based on an inactivity gap.
  • Grace period: Important for handling late-arriving events.

Mastering these window types is crucial for building robust real-time analytics and stream processing applications.

Sıkça Sorulan Sorular

“Kafka Streams'te Pencereleme İşlemleri” dersi ücretsiz mi?

Evet — “Kafka Streams'te Pencereleme İşlemleri” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Apache Kafka & Stream Processing Fundamentals kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Apache Kafka & Stream Processing Fundamentals kursu toplamda 4 dersten oluşur.

“Kafka Streams'te Pencereleme İşlemleri” dersinde ne öğreneceğim?

Kafka Streams'te olayları toplamak için zaman tabanlı pencereler tanımlamayı; devrilen, kayan ve sürgülü pencereleri öğrenin. Apache Kafka & Stream Processing Fundamentals ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Apache Kafka & Stream Processing Fundamentals öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Apache Kafka & Stream Processing Fundamentals, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“Kafka Streams'te Pencereleme İşlemleri” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Apache Kafka & Stream Processing Fundamentals dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Apache Kafka & Stream Processing Fundamentals dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Kafka Streams'te Pencereleme İşlemleri
  2. Akışlarda Birleştirmeler ve Toplamalar
  3. Akış Analizi için KSQL'e Giriş
  4. Etkileşimli Sorgular ve Durum Depoları
← Apache Kafka & Stream Processing Fundamentals Sayfasına Dön