Skip to main content
PYSPARK โ€ข LESSON 182

Watermarking & Stateful Streaming Aggregations

How do we define a 10-minute watermark to drop late data and count events in 5-minute tumbling windows?

Productionโฑ 3 Minutesโญ 1250 XP
๐Ÿค” THE QUESTION

How do we define a 10-minute watermark to drop late data and count events in 5-minute tumbling windows?

๐Ÿ’ก WHAT IS IT?

withWatermark() specifies how late data can arrive before being discarded from in-memory state stores.

๐ŸŽฏ WHAT IS IT USED FOR?

Real-time windowed metrics, DDoS detection, and rolling traffic volume monitoring.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, window

windowed_counts = streaming_df \
    .withWatermark("event_time", "10 minutes") \
    .groupBy(window(col("event_time"), "5 minutes"), col("device_type")) \
    .count()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Watermarking & Stateful Streaming Aggregations.

  • โœ“ Define 10-minute watermark on event_time
  • โœ“ Group by 5-minute tumbling window
  • โœ“ Execute stateful streaming aggregation