sparklearning Documentation

repository·master·Indexed 20 days ago

https://github.com/ankurchavda/sparklearning

A comprehensive learning guide for Apache Spark covering core concepts including RDDs, transformations, actions, and the Spark ecosystem (SQL, MLlib, Streaming, GraphX). The guide details the Catalyst framework, SparkSession, cluster managers (Standalone, Mesos, YARN, Kubernetes), and performance optimization techniques such as Broadcast Variables, Accumulators, and Parquet storage.

Tokens
8.3K
Snippets
15
Records
53
Agent score
30%

What's inside sparklearning

  1. Overview of Apache Spark Ecosystem

    master

    Apache Spark is a fast, general-purpose cluster computing platform that acts as a computational engine for scheduling, distributing, and monitoring applications across a cluster.

    Core Components

    • Spark Core: The foundation containing task scheduling, memory management, fault recovery, and the RDD API.
    • Spark SQL: Enables executing SQL-like queries on Spark data using standard BI or visualization tools.
    • Spark MLlib: A machine learning library for algorithms like clustering, regression, and classification.
    • Spark Streaming: Used for processing real-time streaming data.
    • Spark GraphX: An API for graph parallel computations (e.g., joinVertices, subgraph, aggregateMessages).
  2. Understand the core features of Delta Lake

    master

    Delta Lake is an open-format storage layer designed for Apache Spark that brings data warehouse-like structure and governance to data lakes, forming the foundation of a 'Lakehouse'.

    Key features include:

    • ACID Transactions: Ensures atomicity; new data is only visible once a transaction is complete, and failed jobs can be discarded without leaving partial data.
    • Schema Management: Supports schema enforcement (validating data during writes) and schema evolution (making changes to table schemas).
    • Scalable Metadata: Processes metadata using distributed processing, similar to regular data.
    • Unified Batch and Streaming: Supports both processing modes; each micro-batch transaction creates a new table version.
    • Time Travel: Allows access to historical versions of data by scanning transaction logs.
    • Data Operations: Supports upserts, deletes, and structured streaming.
  3. What is Dynamic Partition Pruning (DPP)?

    master
    Dynamic Partition Pruning (DPP) is a Spark optimization technique that improves query performance by pruning partitions at runtime. It uses information from one side of a join (typically a dimension table) to filter the partitions of the other side (typically a large fact table) during the scan, significantly reducing the amount of data read from storage.
  4. What is shuffling and when does it occur?

    master

    Shuffling is the process of rearranging data within a cluster between different stages. It is triggered by wide transformations, which require data to be redistributed across the cluster.

    Common triggers for shuffling include:

    • repartition
    • Bykey operations (except counting)
    • Joins (especially cross joins)
    • Sorting
    • Distinct
    • GroupBy
  5. The Role of the Spark Driver

    master

    The Driver is the central process that runs your main() function. It sits on a node in the cluster and is responsible for:

    1. Maintaining information about the Spark Application.
    2. Responding to user input/programs.
    3. Analyzing, distributing, and scheduling work across Executors.

    Driver Responsibilities:

    • Prepares the SparkContext.
    • Declares RDD operations using Transformations and Actions.
    • Submits the serialized RDD graph to the master.
  6. When to use a Shuffle Sort Merge Join

    master

    A Shuffle Sort Merge Join is the standard strategy for joining two large tables (Big table-to-big table). It consists of a sort phase (sorting each dataset by the join key) followed by a merge phase (iterating through keys to find matches).

    Use this join type when:

    • Each key within the two large datasets can be sorted and hashed to the same partition by Spark.
    • You are performing equi-joins (joining based on matching keys).
    • You want to prevent unnecessary Exchange and Sort operations by pre-optimizing data (e.g., via bucketing).
  7. Compare DataFrames and RDDs

    master

    A DataFrame is a distributed collection of data organized into named columns.

    Why use DataFrames over RDDs? When using RDDs, Spark only sees operations as opaque lambda expressions (e.g., Iterator[T]). Because the internal structure of the data is not visible to the engine, Spark cannot perform automatic optimizations. DataFrames provide a structured schema that allows the Spark optimizer to improve query execution.

  8. Understand Spark Closures

    master
    A Closure consists of the variables and methods that must be visible to the executor to perform computations (e.g., inside a foreach() call). When a function is sent to an executor, the closure is serialized and sent along with it. Note that the variables within the closure sent to each executor are copies.
  9. Understand Spark Streaming concepts

    master

    Stream processing involves continuously incorporating unbounded data (data with no predetermined beginning or end) to calculate results. Common use cases include processing credit card transactions, IoT device data, and click streams.

    Key Principle: When designing your application, ensure that your business logic produces identical results whether it is applied to a streaming data source or a batch data source.

  10. Understanding Parquet Storage

    master

    Parquet is a columnar storage format used in Spark that offers several advantages over row-based storage:

    • Columnar Storage: Loads only the required columns for a query, reducing I/O.
    • Schema Storage: The schema is stored in the file footer.
    • Predicate Pushdown: Allows for efficient data filtering at the storage level.
    • Data Skipping: Enables skipping irrelevant data blocks.
    • Space Efficiency: Does not waste space storing missing values.
  11. Enable and use Adaptive Query Execution (AQE)

    master

    Adaptive Query Execution (AQE) in Spark 3 allows the engine to re-optimize and adjust query plans at runtime based on statistics collected during execution. This addresses limitations in rule-based optimizations that rely on pre-runtime estimates which may be inaccurate due to selective filters or complex operators.

    AQE provides three primary runtime optimizations:

    1. Switch Join Strategies: Dynamically switches join types (e.g., from Sort-Merge Join to Broadcast-Hash Join) if the actual size of a relation after filtering is smaller than the broadcast threshold.
    2. Coalesce Shuffle Partitions: Automatically combines small adjacent shuffle partitions into larger ones. This prevents the overhead of too many small tasks and inefficient I/O caused by having too many partitions, while also preventing disk spilling caused by too few partitions.
    3. Optimize Skew Joins: Automatically detects data skew (uneven data distribution) from shuffle statistics and splits skewed partitions into smaller sub-partitions to balance the workload across executors.
  12. Choose the Right Spark Join Type

    master

    Selecting the correct join type is critical for performance.

    1. Broadcast Hash Join (Map-side join):

    • When to use: When one dataset is much smaller than the other (default threshold is < 10MB) and you can afford the network bandwidth/memory to broadcast it to all executors.
    • Benefit: Avoids large shuffles.

    2. Shuffle Sort Merge Join:

    • When to use: When joining two large datasets on a common sortable key. It is used for equi-joins.
    • Optimization: Use Bucketing for high cardinality columns or Partitioning for common sorted keys to prevent expensive Exchange and Sort operations.

    General Join Tips:

    • Keep the largest DataFrame on the left; Spark tends to shuffle the right DataFrame first.
    • Filter data as early as possible before joining.
    • Use the same partitioner between DataFrames if possible.
    • Use Salting to handle data skew on joining keys (note: this increases memory usage).