Skip to main content

session 11- Spark Structured streaming - Real Time Data processing.

 101. Streaming source data Generation.

Normally streaming process involves streaming the data to Kafka, Kenesis(AWS) and PUb SUb (GCP )


The data is continuously flowing into the bronze layer This kind of data is called as unbounded data.






As far as now, we have only used the reading data from pyspark using Spark. Read the file format (file name ), and we will write using Spark. write to write the data into the destination.

So to read the streaming data, we have to use Spark. readstream and spark.writestream



If we haven't specified any format default format will be the delta format in Azure. For normal write, we will use option (mode, 'override') In Spark streaming, we have to use outputmode ("append")

the spark write stream output will be 




Python
sinkStreamJSONCheckpointPath = 'abfss://bronze@adlsudadatalakedev.dfs.core.windows.net/daily-pricing-streaming-data/json/checkpoint/'

streamProcessingQuery = (sourceStreamJSONFileDF
                       .writeStream
                       .outputMode("append")
                       .format("json")
                       .queryName("stream-processing")
                       .trigger(processingTime = "5 Minutes")
                       .option("checkpointLocation", sinkStreamJSONCheckpointPath)
                       .start(sinkStreamJSONFilePath)
                       )

The key additions compared to the previous snippet are:

  1. .queryName("stream-processing"):

    • This line assigns a logical name to your streaming query: "stream-processing".
    • Benefits of setting a query name:
      • Monitoring and Management: It makes it easier to identify and monitor your specific streaming query in Spark's UI (e.g., the Spark History Server or the Spark UI's Streaming tab).
      • Debugging: When troubleshooting issues, the query name helps in distinguishing logs and metrics related to this particular stream.
      • Programmatic Access: You can potentially use the query name to programmatically interact with or manage the running stream (though this is less common in basic write scenarios).
  2. .trigger(processingTime = "5 Minutes"):

    • This line configures the trigger interval for your streaming query.
    • processingTime = "5 Minutes" specifies that Spark will attempt to process new data and update the sink every 5 minutes.
    • Trigger Modes: Spark Structured Streaming supports different trigger modes:
      • processingTime: As used here, it triggers processing at fixed time intervals.
      • once: The query will process all available data in the source and then terminate. This is useful for one-time batch-like processing on streaming data.
      • continuous (experimental): For low-latency processing with certain sources and sinks.
      • availableNow (as of Spark 3.4): Triggers processing as soon as new data is available.
    • Impact: Setting a trigger interval controls the latency of your streaming pipeline. A shorter interval leads to lower latency but potentially higher resource consumption. A longer interval reduces resource usage but increases latency. Choosing the appropriate trigger interval depends on your specific application requirements.

The rest of the code remains the same and performs the following actions:

  • Reads a streaming DataFrame (sourceStreamJSONFileDF).
  • Writes the output in "append" mode (only new data is written).
  • Formats the output as "json" files.
  • Uses the specified sinkStreamJSONCheckpointPath for maintaining the query's state for fault tolerance.
  • Starts the streaming query and writes the output to sinkStreamJSONFilePath (assumed to be defined elsewhere).

In summary, this updated code snippet enhances the previous one by:

  • Providing a logical name to the streaming query for better monitoring and management.
  • Defining a specific processing interval of 5 minutes, controlling how frequently new data is processed and written to the sink.

This configuration is more explicit about how the streaming pipeline will operate in terms of processing frequency and provides better metadata for monitoring.




he output streamProcessingQuery.Id shows the unique identifier of the Spark Structured Streaming query that you just started. This confirms that the .start() action was successful in launching the streaming process. The query is now running in the background, continuously processing data according to the configured trigger and writing the results to the specified sink.



Module Summary

Spark Structured Streaming Functionalities:

  1. Spark Structured Streaming Use Case Scenarios

  2. Spark Structured Streaming Reader / Writer Configurations

  3. Spark Structured Streaming CHECKPOINT options Purpose and Usability


Comments

Popular posts from this blog

Entity Relationship (ER) Diagram Model with DBMS Example

Reference :   Entity Relationship (ER) Diagram Model with DBMS Example What is ER Diagram? ER Diagram  stands for Entity Relationship Diagram, also known as ERD is a diagram that displays the relationship of entity sets stored in a database. In other words, ER diagrams help to explain the logical structure of databases. ER diagrams are created based on three basic concepts: entities, attributes and relationships. ER Diagrams contain different symbols that use rectangles to represent entities, ovals to define attributes and diamond shapes to represent relationships. At first look, an ER diagram looks very similar to the flowchart. However, ER Diagram includes many specialized symbols, and its meanings make this model unique. The purpose of ER Diagram is to represent the entity framework infrastructure. Entity Relationship Diagram Example Table of Content: What is ER Diagram? What is ER Model? History of ER models Why use ER Diagrams? Facts about ER Diagram Model ER Diagram...

GCP Buckets

  CREATING BUCKET Method 1: Using GCP Console (UI) Go to the  Google Cloud Console . Navigate to  Cloud Storage . Click  Create Bucket . Enter a  unique name  for your bucket. Select a  storage class  (Standard, Nearline, Coldline, or Archive)... Choose a  location  (region/multi-region). Set  access control  (Uniform or Fine-grained). Click  Create . how do you optimise the class storage buckets - we can talk about cloud storage choosing wisely standard vs nearline vs coldline vs archive... how you managed your buckets - how did you manage? life cycle rule - regions multi regions etc

Pyspark

pyspark link the above link for a sample code for Pyspark  Transformations create new datasets, actions return values, or write data. Transformations: Definition: Transformations are operations that create a new RDD or data frame from an existing one. They are "lazy," meaning they don't execute immediately. Instead, Spark builds a lineage graph (DAG - Directed Acyclic Graph) of transformations. Examples: map() : Applies a function to each element. filter() : Select elements based on a condition. flatMap() : Similar to a map, but flattens the results. groupByKey() : Groups elements by key. reduceByKey() : Reduces elements by key. join() : Joins two datasets. distinct() : Removes duplicate elements. union() : Combines two datasets. coalesce() : Reduces the number of partitions. repartition() : Increases or decreases the number of partitions. Actions: Definition: Actions trigger the execution of the lineage graph, which returns a result to the driver progra...