DEV Community

Cover image for Study Notes 5.1.1-2 Introduction to Batch Processing & spark
Pizofreude
Pizofreude

Posted on

Study Notes 5.1.1-2 Introduction to Batch Processing & spark

1. Introduction to Batch Processing

What is Batch Processing?

Batch processing is a method of processing data in large chunks at scheduled intervals. It is widely used in data engineering to handle large datasets efficiently. Batch jobs process data in bulk instead of handling individual records in real time.

Batch Processing vs. Streaming Processing

There are two primary ways to process data:

  1. Batch Processing: Processes data in predefined intervals (e.g., hourly, daily, or weekly). Covers ~80% of data processing in DE job.
  2. Streaming Processing: Processes data in real-time as it arrives. Covers ~20% of data processing in DE job.
Feature Batch Processing Streaming Processing
Processing Mode At fixed intervals Real-time
Use Cases Large-scale ETL, reporting, machine learning model training Fraud detection, live dashboards, real-time recommendations
Common Tools Apache Spark, SQL, Python scripts Apache Kafka, Apache Flink, Spark Streaming

Example of Batch vs. Streaming

  • Batch: Processing daily sales data for a retail store at midnight.
  • Streaming: Monitoring live stock market prices and updating dashboards instantly.

2. Why Use Batch Processing?

Advantages of Batch Processing

  • Easy to manage: Jobs run on a schedule and can be retried if they fail.
  • Efficient for large datasets: Can process large amounts of data in a structured way.
  • Cost-effective: Often cheaper than real-time processing, as resources are allocated only when needed.
  • Scalability: Can be scaled by increasing compute resources.

Disadvantages of Batch Processing

  • Delay in data availability: Processed data is only available after the batch job finishes.
  • Resource-intensive: Large batch jobs may require significant computational power.
  • Less suitable for real-time applications: Not ideal for scenarios requiring immediate insights.

3. Tools for Batch Processing

Several tools and technologies are used for batch processing:

  • Python scripts: Often used for simple batch jobs (e.g., reading a CSV file and storing it in a database).
  • SQL: Used for batch data transformations (e.g., aggregating sales data in a database).
  • Apache Spark: A powerful distributed computing framework for large-scale data processing.
  • Apache Flink: Another distributed system but primarily used for stream processing.
  • Airflow: A workflow orchestration tool to manage batch job execution.

4. Introduction to Apache Spark

Apache Spark is one of the most popular tools for batch processing due to its speed, scalability, and support for various programming languages.

Key Features of Spark

  • Distributed Processing: Uses multiple machines to process data efficiently.
  • Supports Multiple Languages: Can be used with Python (PySpark), Scala, Java, and R.
  • Resilient Distributed Datasets (RDDs): A fundamental data structure in Spark for fault-tolerant processing.
  • DataFrames & SQL: Provides SQL-like querying and data manipulation.

Installing Spark

To install Spark on a Linux virtual machine (VM) using Google Cloud Platform:

  1. Create a VM instance on Google Cloud.
  2. Install Java (required for Spark).
  3. Download and extract Apache Spark.
  4. Configure environment variables.
  5. Start using Spark with PySpark.

5. Spark Components

1. Resilient Distributed Datasets (RDDs)

  • Low-level API for distributed data processing.
  • Provides fault tolerance and parallel computation.

2. DataFrames

  • Higher-level abstraction than RDDs.
  • Similar to SQL tables, allowing structured data manipulation.

3. Spark SQL

  • Allows querying data using SQL syntax.
  • Optimized for performance with Catalyst optimizer.

4. Spark Jobs & DAGs (Directed Acyclic Graphs)

  • A Spark Job consists of multiple tasks.
  • Spark builds a DAG to optimize execution.

6. Running Spark Jobs in Docker

  • Docker allows running Spark in an isolated environment.
  • Steps:
    1. Install Docker.
    2. Pull a Spark Docker image.
    3. Run Spark jobs inside Docker containers.

7. Deploying Spark in the Cloud

  • Spark can be deployed on cloud platforms like AWS EMR, Google Dataproc, or Azure Synapse Analytics.
  • Integration with data warehouses like BigQuery, Redshift, and Snowflake.

8. Summary

  • Batch processing is ideal for large-scale data transformations at fixed intervals.
  • Apache Spark is a powerful tool for batch processing.
  • DataFrames & SQL make Spark easier to use.
  • Airflow helps orchestrate batch workflows.
  • Cloud platforms provide scalable deployment options for Spark.

9. Next Steps

  • Practice running Spark jobs using PySpark.
  • Explore using Airflow to schedule batch jobs.
  • Learn about streaming processing in Week 6.

1. Introduction to Apache Spark

  • Definition:

    Apache Spark is a unified, distributed engine for large-scale data processing. It’s designed to support data engineering, data science, and machine learning tasks.

  • Key Characteristics:

    • Distributed Processing: Spark can process data across clusters of many machines, making it suitable for large datasets.
    • In-Memory Computation: Its ability to cache data in memory speeds up iterative algorithms, especially in machine learning.
    • Multi-Language Support: Although Spark is written in Scala, it provides APIs for Python (PySpark), Java, and even R.

2. Core Concepts and Architecture

Spark Engine

  • Role: Acts as the central processing engine that:
    • Pulls data from sources (e.g., data lakes, databases).
    • Distributes computation across many nodes.
    • Outputs the processed data back to a target (data lake, warehouse, etc.).

Cluster Components

  • Driver: Coordinates the execution of Spark jobs by breaking the job into tasks and scheduling them.
  • Executors: Worker processes running on each node in the cluster that execute tasks on data partitions.
  • Cluster Manager: Manages the allocation of resources across applications (can be YARN, Mesos, Kubernetes, or Spark’s standalone manager).

Data Abstractions

  • Resilient Distributed Datasets (RDDs): The fundamental data structure representing immutable, partitioned collections that can be processed in parallel.
  • DataFrames & Datasets: Higher-level abstractions built on top of RDDs that allow structured data operations (similar to SQL tables) and benefit from optimizations like the Catalyst Optimizer.

3. Spark Ecosystem Components

  • Spark Core: Provides the basic functionality of task scheduling, memory management, fault recovery, and interaction with storage systems.
  • Spark SQL: Enables querying of structured data via SQL and DataFrame APIs.
  • MLlib: A library of scalable machine learning algorithms.
  • GraphX: Provides APIs for graph processing and analytics.
  • Spark Streaming / Structured Streaming: Enables processing of real-time data streams by treating them as a sequence of small batch jobs.

4. Language Support and Programming

  • Scala: The native language of Spark, ideal for performance-critical applications.
  • Python (PySpark): The most popular interface due to its simplicity and the rich ecosystem of data science libraries.
  • Java & R: Alternatives available, though less common for typical data engineering tasks in many companies.

5. Use Cases and When to Use Spark

When to Use Spark:

  • Processing Data in Data Lakes: When your data resides in file storage systems (e.g., S3 or Google Cloud Storage) and you need to perform complex processing.
  • Complex Transformations Beyond SQL: If a job’s logic is too complex for SQL or requires modular programming and extensive testing.
  • Machine Learning Workflows: For training and applying machine learning models where you need flexibility that SQL alone can’t offer.
  • Streaming Data (Advanced Use): Although not covered in detail in this transcript, Spark can process streaming data by treating streams as micro-batches.

SQL vs. Spark:

  • SQL-based Tools (e.g., Athena, Presto, BigQuery):
    • Ideal when your processing can be fully expressed with SQL.
    • Best suited for structured data in data warehouses or when using external tables on data lakes.
  • Apache Spark:
    • Preferred when you need additional flexibility for complex workflows.
    • Useful for combining ETL tasks with machine learning or other non-SQL operations.

6. Typical Data Engineering Workflow with Spark

  1. Data Ingestion:

    Raw data is loaded into a data lake.

  2. Initial Transformations:

    • Simple aggregations and joins can be performed using SQL-based tools.
    • Example tools: Athena, Presto, or BigQuery.
  3. Advanced Processing:

    • When transformations are too complex for SQL, Spark jobs (using PySpark or Scala) are employed.
    • Machine learning tasks, such as training models, often fall in this stage.
  4. Model Application:

    • Trained models can be applied on new data using subsequent Spark jobs.
    • Results are typically written back to a data lake or pushed to a data warehouse for further analysis.

7. Advantages of Using Spark

  • Scalability: Efficiently processes huge volumes of data by leveraging distributed computing.
  • Performance: In-memory processing and optimized execution plans (via Catalyst and Tungsten) significantly boost speed.
  • Flexibility: Supports a variety of workloads (batch, streaming, machine learning) and integrates with many data sources.
  • Extensibility: Provides a robust ecosystem for building end-to-end data pipelines.

8. Getting Started with Apache Spark

Local Setup:

  • Installation: Begin by installing Spark locally to experiment with its features and APIs.
  • Development Tools: Use interactive notebooks (e.g., Jupyter) or integrated development environments (IDEs) that support Python or Scala.

Learning Resources:

  • Official Documentation: The Apache Spark website offers comprehensive guides and API references.
  • Online Courses: Courses like DE Zoomcamp provide structured learning paths from beginner to professional levels.
  • Community and Tutorials: Participate in online forums, attend webinars, and work through practical examples to deepen your understanding.

9. Supplementary Information for Advanced Users

  • Optimizations:
    • Learn about caching, partitioning strategies, and tuning Spark configurations.
    • Explore advanced optimization techniques using Spark’s Catalyst Optimizer.
  • Deployment:

    Understand how to deploy Spark applications on various cluster managers (e.g., YARN, Kubernetes) in production environments.

  • Integration:

    Integrate Spark with other big data tools and cloud platforms, and learn best practices for building scalable, fault-tolerant data pipelines.

  • Evolving Workloads:

    Stay updated with Spark’s evolving features (such as Structured Streaming) to handle both batch and real-time data processing.


Conclusion

Apache Spark is a powerful tool that bridges the gap between simple SQL-based transformations and the complex processing demands of modern data engineering. By mastering its core components, understanding its ecosystem, and knowing when to use it versus traditional SQL solutions, you can build scalable and efficient data pipelines. These notes provide a foundational framework that, when combined with hands-on practice and further study, will support your journey from a beginner to a professional data engineer.


Top comments (0)