Save a Dataframe in PySpark Streaming

What will you learn?

In this tutorial, you will master the art of saving a Dataframe in PySpark streaming for real-time data processing. Dive deep into the world of stream processing with Apache Spark and learn how to efficiently store and process streaming data.

Introduction to the Problem and Solution

Working with streaming data in PySpark requires effective strategies for saving processed data. By saving transformed Dataframes to output sinks like file systems or databases, you ensure persistent storage of results for further analysis or reporting.

The solution involves setting up a streaming query that processes incoming data and saves resulting Dataframes at specified intervals or conditions. The saved data can be written in various formats such as Parquet, CSV, JSON, etc., catering to your specific requirements.


# Import necessary libraries
from pyspark.sql import SparkSession

# Create a Spark session
spark = SparkSession.builder \
    .appName("StreamingDataProcessor") \

# Read streaming data from source (e.g., Kafka)
streaming_data = spark.readStream.format("kafka").option("kafka.bootstrap.servers", "localhost:9092").load()

# Perform transformations on streaming_data (example: word count)
transformed_data = streaming_data.groupBy("value").count()

# Define output sink and write mode
query = transformed_data.writeStream \
    .outputMode("append") \
    .format("parquet") \
    .option("path", "output_path") \  # Specify the output directory path here


# Copyright PHD

Note: Replace “output_path” with your desired location for saving processed stream data.


To save a Dataframe in PySpark streaming: 1. Create a Spark session. 2. Read streaming data from a source like Kafka. 3. Apply transformations on the streamed Dataframe. 4. Specify an output sink and write mode for saving processed data. 5. Start the stream processing query using .start() method and await termination.

This process ensures continuous processing and storage of streamed Dataframes based on defined logic.

  1. How can I specify multiple output sinks for saving my PySpark streamed Dataframes?

  2. You can specify multiple sinks by creating separate writeStream queries with different configurations for each sink.

  3. Can I apply custom functions while saving my PySpark streamed Dataframes?

  4. Yes, use foreachBatch() function to apply custom processing logic before writing out each micro-batch of your streamed dataframe.

  5. Is it possible to dynamically change the output path based on certain conditions during runtime?

  6. Yes, programmatically modify options like path within your code based on dynamic conditions before starting your stream query.

  7. What happens if there is an error during stream processing? How is fault-tolerance handled?

  8. PySpark’s structured streaming provides fault-tolerance mechanisms by checkpointing progress information at regular intervals allowing recovery from failures without reprocessing all previous batches again.


Saving Dataframes in PySpark streaming enables efficient storage of real-time processed data for analysis or downstream consumption tasks. Mastering stream processing pipelines is essential for building scalable real-time analytics applications using Apache Spark ecosystem tools.

Leave a Comment