Skip to the page

PySpark interview question · hard

What is Structured Streaming, and how does it differ from DStreams?

The short answerStructured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark SQL engine.

The full answer

Structured Streaming is a scalable and fault-tolerant stream processing engine built on the Spark SQL engine. It allows users to process real-time data using a high-level API based on DataFrames and Datasets. Compared to DStreams, Structured Streaming provides more flexibility, better performance, and ease of use.

Structured Streaming:

  • Abstraction: Works with DataFrames and Datasets, treating streaming data as a continuously updating table.
  • Model: Uses the concept of "unbounded tables," where data is processed incrementally as new rows are appended.
  • API Integration: Fully integrates with the Spark SQL API, enabling seamless use of SQL queries and DataFrame operations.
  • Fault Tolerance: Leverages Spark’s Catalyst optimizer and the write-ahead log for reliable processing and fault recovery.
  • Output Modes: Supports different output modes, such as append, update, and complete, to define how results are emitted.

DStreams:

  • Abstraction: Based on Resilient Distributed Datasets (RDDs), processing data as a sequence of micro-batches.
  • Model: Divides streams into discrete micro-batches for processing, treating each as an independent Spark job.
  • API Integration: Provides an RDD-like API, which is less expressive and harder to use for complex operations.
  • Fault Tolerance: Uses RDD lineage for recomputation in case of failures.
  • Output Modes: Offers fewer options compared to Structured Streaming, primarily focused on RDD actions like saveAsTextFiles.

Key Differences Between Structured Streaming and DStreams:

AspectStructured StreamingDStreams
AbstractionBased on DataFrames and Datasets.Based on RDDs.
Processing ModelContinuous processing with incremental updates.Micro-batch processing with discrete RDDs.
API IntegrationFully integrates with Spark SQL and DataFrame APIs.Offers an RDD-like API with limited functionality.
Ease of UseHigh-level API simplifies real-time data processing.Low-level API requires more effort for complex tasks.
Fault ToleranceUses write-ahead logs and checkpointing.Relies on RDD lineage for recomputation.
PerformanceOptimized with Catalyst and Tungsten for faster queries.Less optimized due to RDD-based processing.
Output ModesSupports append, update, and complete.Limited output actions like saveAsTextFiles.

Code Example Using Structured Streaming:

from pyspark.sql import SparkSession

# Initialize SparkSession
spark = SparkSession.builder.appName("Structured Streaming Example").getOrCreate()

# Read streaming data
streaming_df = spark.readStream.format("socket").option("host", "localhost").option("port", "9999").load()

# Process the stream (e.g., word count)
words = streaming_df.selectExpr("explode(split(value, ' ')) as word")
word_counts = words.groupBy("word").count()

# Write the result to the console
query = word_counts.writeStream.outputMode("complete").format("console").start()
query.awaitTermination()

Nuances to Consider:

  • Structured Streaming provides a declarative API and supports complex aggregations, windowing, and SQL queries natively, making it more versatile than DStreams.
  • DStreams are suitable for simpler, micro-batch-oriented workloads but require more manual effort for fault tolerance and processing logic.
  • Structured Streaming is the recommended approach for new applications due to its integration with modern Spark features and improved scalability.

Tecyfy Takeaway:
Structured Streaming is a modern, higher-level alternative to DStreams, offering enhanced functionality, integration with SQL, and better performance. While DStreams rely on RDDs and micro-batch processing, Structured Streaming’s declarative model simplifies real-time data processing, making it the preferred choice for new applications.

Last updated 24 January 2025