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...

SQL Joins and advanced joins and Subqueries

  Refernce :  Expert Guide to Advanced SQL Joins: What You Need to Know It's helpful to visualize how these different SQL joins work. Here's a breakdown in a table-like format, along with explanations: SQL Join Types Overview Join Type Description Key Characteristics Use Cases INNER JOIN Returns rows where there is a match in both tables. - Shows only matching records. - Excludes unmatched rows from both tables. - Retrieving related data that exists in both tables. - Finding records with corresponding entries in another table. LEFT OUTER JOIN (LEFT JOIN) Returns all rows from the left table, and the matched rows from the right table. - Includes all records from the left table. - Fills in NULL values for columns from the right table where there's no match. - Retrieving all records from one table and their related data from another, even if some records don't have matches. - Finding records in one table that don't have corresponding entries in another. RIGHT OUTER JO...

GIT BASH

  Bash Shell: Git Bash uses the Bash (Bourne Again SHell) command-line interpreter. This means you can use many of the same commands you'd find in a Linux or macOS terminal. Git Integration: Git Bash is tightly integrated with Git, making it easy to execute Git commands Essential Commands: Navigation: pwd : Prints the current working directory. ls : Lists files and directories in the current directory. cd <directory> : Changes the current directory. cd .. : Moves to the parent directory. File Management: mkdir <directory> : Creates a new directory. touch <file> : Creates a new file. rm <file> : Removes a file. rmdir <directory> : Removes an empty directory. Git Commands: git init : Initializes a new Git repository. git clone <repository URL> : Clones an existing Git repository. git status : Displays the status of your working directory. git add <file> : Adds a file to the staging area. git commit -m "commit message" : Commits chan...