Apply watermarks and window aggregation in Spark Structured Streaming

domain: data-engineering · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Parse the event timestamp field and cast it to TimestampType in your streaming DataFrame.
  2. Apply a watermark: df.withWatermark('event_time', '10 minutes') tells Spark to tolerate up to 10 minutes of late data and to advance state cleanup accordingly.
  3. Apply a window aggregation: df.groupBy(window('event_time', '5 minutes'), 'key').agg(count('*').alias('cnt')).
  4. Write with outputMode('append') to emit only finalized windows (after watermark passes window end + late threshold) or outputMode('update') for partial results.
  5. Monitor state store size and watermark progress via Spark UI Structured Streaming tab.

Known gotchas

Related routes

Implement stream-stream join with watermark in Spark Structured Streaming
data-engineering · 5 steps · unrated
Configure Spark Structured Streaming watermarking to handle late-arriving data and bound state size
spark.apache.org · 6 steps · unrated
Implement arbitrary stateful aggregation in Spark Structured Streaming with flatMapGroupsWithState or applyInPandasWithState
data-engineering · 5 steps · unrated

Give your agent this knowledge — and 15,600+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans