Skip to content

Streaming

  • Treats continuous data stream as bounded dataset with driver process remains active
  • Each streaming application will have a trigger that causes Spark to create and process new data as Micro-batch
  • the planning and scheduling steps happen for all micro-batches and incur non-trivial overhead
  • uses readStream, writeStream instead of read, write
  • DataStreamWriter get converted to StreamingQuery using toTable or start
    • starts a background task
  • StreamingQuery requires .awaitTermination
  • Trigger can optionally be specified with writeStream:
    • continuous (default): omit any trigger spec, runs the next micro-batch immediately after the first one finishes
    • Interval: .trigger(processingTime='2 seconds')
    • one-shot: .trigger(availableNow=True), unlike batch it offers
      • fault-tolerance, exactly-once guarantee, stateful operations (agg/dedup etc), checkpoint/offset management
from pyspark.sql import SparkSession
from pyspark.sql.functions import explode, split
spark = SparkSession.builder \
.appName("MinimalSparkStream") \
.getOrCreate()
df = (
spark.readStream
.format("kafka")
.option("kafka.bootstrap.servers", "host1:port1,host2:port2")
.option("startingTimestamp", 1) # optional, value in epoch-time, ignored if checkpoint file exists
.option("subscribe", "topic1")
.load()
.groupBy(col("timestamp"), col("value"))
.count()
)
query = (
.writeStream()
.format("kafka")
.option(...)
.outputMode("update")
.trigger(ProcessingTimeTrigger(0)) # or .trigger(RealTimeTrigger()) for real-time streaming
.start()
)
query.awaitTermination()
  • outputMode: determines how the target is written
    • append: results of micro-batch are appended to the target
    • complete: target is rewritten after merging last result with results of current micro-batch
      • used for aggregates, running aggregates are maintained in the checkpoint directory
      • although new results are computed incrementally, target is completely rewritten
    • update: only the new or updated results are used to update the target
      • used with foreachBatch and using MERGE sql with spark.sql() call
  • micro-batch size can be limited by maxFilesPerTrigger or maxBytesPerTrigger or both (the first to hit applies)
  • sources: directory, delta table, kafka …
    • directory sources can specify option clearSource as either delete or archive to move processed files to be deleted or moved (to sourceArchiveDir)
  • targets: file/directory, delta table, kafka, foreach …
  • Kafka options:
  • checkpoint directory is used for storing offset and the state information
    • state information enables Spark to calculate aggregates across all micro-batches when outputMode="complete"
  • certain transformations require state to be maintained: aggregation, windowing, joins
  • stateless transformations: filter, select, explode etc
    • are not supported in complete outputMode
  • checkpoint directory is the default location for maintaining state
  • Spark supports RocksDB for storing state
    • spark.conf.set("spark.sql.streaming.stateStore.providerClass", "org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider")
  • on a time-based micro-batches, a delay from the latest timed event received, df.withWatermark("eventTimestamp", "10 minutes")
  • it is possible to receive late-arriving event up to watermark delay. example
  • outputMode=append: write records once after watermark delay
  • Databricks introduced Reat-time Mode
  • Differences between micro-batch and real-time
    • stages are scheduled in sequnce, whereas stages run concurrently
    • trigger creates a micro-batch v/s a longer batch continuously reading data