Use foreachBatch sink 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. Define a function with signature (batchDF: DataFrame, batchId: Long) => Unit (Scala/Python equivalent) that processes each micro-batch as a static DataFrame.
  2. Register the function with writeStream.foreachBatch(myFunc).start().
  3. Inside the function, use batchId to implement idempotent writes (e.g., skip or overwrite if batchId already processed) for exactly-once semantics.
  4. You can write to multiple sinks in one function call, apply arbitrary DataFrame transformations, or call external APIs.
  5. Cache the batchDF if you materialize it more than once inside the function to avoid recomputation.

Known gotchas

Related routes

Implement arbitrary stateful aggregation in Spark Structured Streaming with flatMapGroupsWithState or applyInPandasWithState
data-engineering · 5 steps · unrated
Implement stream-stream join with watermark in Spark Structured Streaming
data-engineering · 5 steps · unrated
Choose and apply Spark Structured Streaming output modes (append, update, complete)
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