Unit 3 – Data Processing with Spark

Theory

1. Introduction to Apache Spark

Apache Spark is an open-source, distributed, general-purpose cluster computing framework designed for large-scale data processing. Developed at UC Berkeley's AMPLab in 2009 and later donated to the Apache Software Foundation, Spark has become the de facto standard for big data processing.

Spark provides an interface for programming entire clusters with implicit data parallelism and fault tolerance. It supports multiple languages including Python, Scala, Java, and R, making it accessible to a wide range of developers and data scientists.

Unlike Hadoop's MapReduce, which writes intermediate results to disk, Spark leverages in-memory computing, making it up to 100x faster for certain workloads. This speed advantage makes Spark ideal for iterative algorithms, interactive queries, and real-time stream processing.

Key Point: Spark's core abstraction is the Resilient Distributed Dataset (RDD), an immutable, partitioned collection of elements that can be operated on in parallel. RDDs are fault-tolerant through lineage information, enabling automatic recovery from node failures.
1
Spark Ecosystem Overview
graph TD A[Apache Spark Core] --> B[Spark SQL] A --> C[Spark Streaming] A --> D[MLlib] A --> E[GraphX] B --> B1[Structured Data Processing] B --> B2[SQL Queries & DataFrames] C --> C1[Real-time Stream Processing] C --> C2[Micro-batch Architecture] D --> D1[Machine Learning Algorithms] D --> D2[Classification, Clustering, Regression] E --> E1[Graph Processing] E --> E2[PageRank, Connected Components] classDef decision fill:#6366f1,stroke:#818cf8,color:#fff,font-size:18px,font-weight:bold classDef process fill:#0ea5e9,stroke:#38bdf8,color:#fff,font-size:18px,font-weight:bold classDef success fill:#22c55e,stroke:#4ade80,color:#fff,font-size:18px,font-weight:bold classDef warning fill:#f59e0b,stroke:#fbbf24,color:#1e1b4b,font-size:18px,font-weight:bold classDef info fill:#8b5cf6,stroke:#a78bfa,color:#fff,font-size:18px,font-weight:bold class A decision class B,C,D,E process class B1,C1,D1,E1 success class B2,C2,D2,E2 info
Spark Core provides foundational computing capabilities, while specialized libraries (SQL, Streaming, MLlib, GraphX) extend functionality for specific use cases. All libraries share the same core RDD abstraction.

2. RDDs, DataFrames, and Spark SQL

Resilient Distributed Datasets (RDDs) are the fundamental data structure in Spark – immutable, partitioned collections of objects that can be processed in parallel. RDDs support two types of operations: transformations (lazy operations that return new RDDs) and actions (operations that trigger computation and return results).

DataFrames are distributed collections of data organized into named columns, similar to tables in relational databases or data frames in R/Python. Built on top of RDDs, DataFrames provide a higher-level API with optimized execution through the Catalyst optimizer and Tungsten execution engine.

Spark SQL is a Spark module for structured data processing. It provides a programming abstraction called DataFrames and SQL interface, allowing users to query data using SQL expressions. Spark SQL integrates seamlessly with Hive metastore, enabling reading from Hive tables and writing data in various formats (Parquet, ORC, JSON, CSV).

Key Point: DataFrames are generally preferred over RDDs for structured data processing because they leverage Spark's Catalyst optimizer for query optimization and Tungsten for efficient memory management and code generation. This results in significant performance improvements.
2
Spark Data Abstractions
graph TD A[Spark Data Abstractions] --> B[RDD] A --> C[DataFrame] A --> D[Dataset] B --> B1[Low-level API] B --> B2[No Schema Enforcement] B --> B3[Functional Transformations] C --> C1[High-level API] C --> C2[Schema-on-Read] C --> C3[SQL-compatible Operations] D --> D1[Type-safe API] D --> D2[Compile-time Type Checking] D --> D3[Best of RDD + DataFrame] classDef decision fill:#6366f1,stroke:#818cf8,color:#fff,font-size:18px,font-weight:bold classDef process fill:#0ea5e9,stroke:#38bdf8,color:#fff,font-size:18px,font-weight:bold classDef success fill:#22c55e,stroke:#4ade80,color:#fff,font-size:18px,font-weight:bold classDef warning fill:#f59e0b,stroke:#fbbf24,color:#1e1b4b,font-size:18px,font-weight:bold classDef info fill:#8b5cf6,stroke:#a78bfa,color:#fff,font-size:18px,font-weight:bold class A decision class B,C,D process class B1,C1,D1 success class B2,C2,D2 warning class B3,C3,D3 info
RDDs offer low-level control but lack optimization. DataFrames provide high-level abstractions with automatic optimization. Datasets (available in Scala/Java) combine type safety with optimization benefits.

3. Spark Streaming

Spark Streaming enables scalable, high-throughput, fault-tolerant stream processing of live data streams. It receives live data, divides it into batches, and processes them using the Spark Core engine to generate results in batches.

The newer Structured Streaming API builds on Spark SQL and provides a continuous, incremental query engine that automatically incrementalizes batch computations. It treats the unbounded input data as a continuously appending table, enabling developers to query real-time data using the same DataFrame/SQL API used for batch processing.

Spark Streaming supports integration with Kafka, Flume, Kinesis, TCP sockets, and HDFS. It provides exactly-once fault-tolerance guarantees through checkpointing and write-ahead logs, making it suitable for mission-critical streaming applications.

Key Point: Structured Streaming is the recommended approach for new streaming applications in Spark 2.x+. It provides event-time processing with watermarking for handling late data, and supports output modes (Append, Complete, Update) for different query patterns.

4. MLlib – Machine Learning Library

MLlib is Spark's scalable machine learning library that provides numerous algorithms and utilities for common ML tasks including classification, regression, clustering, collaborative filtering, and feature extraction. MLlib is built on top of Spark Core and leverages its distributed computing capabilities.

Key algorithms include: Classification (Logistic Regression, Naive Bayes, Decision Trees, Random Forests, Gradient-Boosted Trees), Regression (Linear Regression, Decision Trees), Clustering (K-Means, Gaussian Mixture, LDA), and Collaborative Filtering (Alternating Least Squares for recommendation systems).

MLlib follows a standardized API called ML Pipelines that defines a set of DataFrames-based APIs for building, tuning, and evaluating ML workflows. The pipeline concept consists of Transformers (algorithms that transform DataFrames) and Estimators (algorithms that can be fit on a DataFrame to produce a Transformer).

Key Point: MLlib's Pipeline API simplifies ML workflow management by chaining feature transformers and model estimators into a single pipeline. CrossValidator and TrainValidationSplit handle hyperparameter tuning, while BinaryClassificationEvaluator and MulticlassClassificationEvaluator provide standardized model evaluation.
3
Spark vs MapReduce Processing Flow
graph LR A[Input Data] --> B{Processing Engine} B -->|MapReduce| C[Map Phase] C --> D[Disk Write - Intermediate] D --> E[Reduce Phase] E --> F[Disk Write - Output] F --> G[Final Result] B -->|Spark| H[Load into Memory] H --> I[Transformations Chain] I --> J[In-Memory Processing] J --> K[Action - Write Output] K --> L[Final Result] classDef decision fill:#6366f1,stroke:#818cf8,color:#fff,font-size:18px,font-weight:bold classDef process fill:#0ea5e9,stroke:#38bdf8,color:#fff,font-size:18px,font-weight:bold classDef success fill:#22c55e,stroke:#4ade80,color:#fff,font-size:18px,font-weight:bold classDef warning fill:#f59e0b,stroke:#fbbf24,color:#1e1b4b,font-size:18px,font-weight:bold classDef info fill:#8b5cf6,stroke:#a78bfa,color:#fff,font-size:18px,font-weight:bold class A,B decision class C,D,E,F process class H,I,J warning class K,L info class G success
MapReduce writes intermediate results to disk after each stage, causing significant I/O overhead. Spark keeps data in memory across operations, resulting in 10-100x performance improvements for iterative and interactive workloads.

5. Spark vs MapReduce

Processing Speed: Spark is 10-100x faster than MapReduce for most workloads due to in-memory computing. MapReduce writes intermediate results to HDFS, while Spark retains data in memory across operations, dramatically reducing I/O overhead.

Ease of Use: Spark provides high-level APIs in Python, Scala, Java, and R with interactive shells for exploration. MapReduce requires writing verbose Java/Python code for even simple operations, with no built-in interactive environment.

Flexibility: Spark supports batch processing, stream processing, machine learning, graph processing, and SQL queries within a single framework. MapReduce is primarily designed for batch processing and requires additional tools (Storm, Flink) for streaming.

Cost Efficiency: Spark's speed reduces cluster runtime and cloud computing costs. However, Spark requires more memory per node, making memory the primary bottleneck. MapReduce is more memory-efficient but trades off processing speed.

Use Cases: MapReduce remains suitable for simple ETL jobs, extremely large datasets that exceed available RAM, and legacy Hadoop ecosystems. Spark excels at iterative algorithms (ML), interactive analytics, real-time streaming, and complex data pipelines.

Key Point: While Spark has largely replaced MapReduce as the preferred processing engine, understanding both paradigms is essential. Spark can run on Hadoop YARN and access HDFS data, allowing organizations to adopt Spark incrementally within existing Hadoop clusters.