Spark structured streaming in Fabric

By Hatnest · Reviewed by Saurav Raj Joshi · Updated Nov 30, 2026

Structured streaming is Spark's engine for continuous data. You write the query as if the input were a table that keeps growing, and Spark runs it incrementally. In Fabric you run it from a notebook or, for production, a Spark job definition. Sources include Event Hubs, Kafka, files and Delta tables. The usual sink is a lakehouse Delta table.

The shape of a query

events = (spark.readStream.format("delta").table("bronze_events"))   # source
cleaned = events.filter("amount > 0")                                  # transform
query = (cleaned.writeStream
    .format("delta")
    .option("checkpointLocation", "Files/chk/clean_events")           # progress
    .outputMode("append")
    .trigger(processingTime="30 seconds")
    .toTable("silver_events"))
query.awaitTermination()

Four things define a query: the source, the transformation, the sink, and the checkpoint. The checkpoint stores offsets, progress and state, so a restarted query resumes without gaps or repeats. Use a durable location and never share one checkpoint between two queries. Parsing needs an explicit schema for file sources; schema inference exists but is not recommended in production.

Triggers and output modes

The trigger decides when work happens: default or processingTime (microbatches), availableNow (process what exists, then stop; good for scheduled incremental jobs), or real-time mode (Spark Runtime 2.0 or later, ultra-low latency, with a smaller set of supported sources, sinks and operators). The output mode decides what is written each time:

  • append: only new rows. Fine for stateless queries; for aggregations it is valid only when Spark knows a result row is final, which means an event-time window with a watermark.
  • update: only changed rows, typical for running aggregates.
  • complete: the whole result every trigger; it needs an aggregation and should only be used when the result stays small.

Event time, windows and watermarks

Aggregations over event time use window(): tumbling (fixed, no overlap), sliding (add a slide interval) and session windows (a gap of inactivity closes the session). A watermark, set with withWatermark("event_time", "10 minutes"), tells Spark how late data may arrive. Spark uses it to finish windows and to delete old state. Too short and valid late events are dropped; too long and state and checkpoints grow. A watermark only advances when new data arrives, so results can be delayed on a quiet stream. Session-window aggregations do not support update mode.

State

Stateful operations (aggregations, joins, deduplication) keep state in a state store saved with the checkpoint. Bound it with watermarks. Enabling the RocksDB state store provider keeps active state off the JVM heap, and changelog checkpointing reduces checkpoint latency; set the provider before the query's first run. Large changes to stateful logic usually require a new checkpoint.

Deduplication and joins

Sources that retry can send the same event twice. dropDuplicates remembers keys; add the watermark column to the key, or use dropDuplicatesWithinWatermark(["event_id"]) when the ID alone defines a duplicate.

dedup = (events.withWatermark("event_time", "10 minutes")
               .dropDuplicatesWithinWatermark(["event_id"]))

Stream-stream joins buffer both sides. Add watermarks and an event-time range condition so old rows can be dropped; unmatched outer-join rows appear only after the watermark proves no match can arrive.

Writing to several places

foreachBatch hands each microbatch to a function, where you can run MERGE into a Delta table or write to several sinks. Processing is at-least-once, so make writes idempotent, for example with Delta txnAppId and txnVersion options. Alternatively run independent queries, each with its own checkpoint, or land everything in a bronze Delta table first and stream from it.

Production habits

Run always-on queries as Spark job definitions with a retry policy; use availableNow for scheduled incremental ones. Enable Optimize Write and sensible trigger intervals to avoid small files, and consider Auto Compaction. start() and toTable() are asynchronous, so call awaitTermination() or the job may finish early. Watch input rate, process rate and batch duration.

Common mistakes

  • Using complete mode on a high-cardinality aggregation.
  • Aggregating by event time with no watermark, so state grows without bound.
  • Sharing a checkpoint, or deleting it and expecting state to survive.
  • Assuming exactly-once behavior from foreachBatch without idempotent writes.

For the exam

  • The checkpoint is what makes a query restartable; one per query.
  • availableNow suits scheduled incremental loads; append is the default output mode.
  • A watermark controls lateness and state cleanup; it is required for append with event-time aggregation.
  • Window types: tumbling, sliding, session.
  • Dedup with dropDuplicates (include the watermark column) or dropDuplicatesWithinWatermark.
  • foreachBatch gives at-least-once delivery.

Sources