Prompts Forms Resume Reports ★ Saved ★ Saved★ + Submit a tool
Tools and frameworks

Apache Spark

About 4204 characters Updated Nov 30, 2026 22 views AI wiki

Apache Spark is a distributed computing engine for large-scale data, centered on in-memory computing and a unified programming model. It places batch processing, stream processing, SQL queries and machine learning on the same set of abstractions, converging fragmented data engineering stacks into one platform, and it is the core execution layer of data lake and warehouse systems.

Core Principles

The basic abstraction in Spark is the distributed dataset. RDD is an immutable, partitioned collection of objects that can be operated on in parallel, and new RDDs can only be produced through transformations; DataFrame adds structural information and execution plan optimization on top of it and is the mainstream interface today. What the two share is lazy execution: transformations only record lineage and do not compute immediately, and a job is actually triggered when an action is encountered, which lets the engine optimize the execution path as a whole.

Once a job is triggered, the driver turns the logical plan into a directed acyclic graph, splits it into stages, and separates stages by shuffle. A shuffle must redistribute data by partition key, involving disk writes, network transfer and sorting, and it is the root of most performance problems. The engine tries to merge transformations with narrow dependencies into the same stage pipeline to reduce intermediate spills. For memory management, Spark uses a unified memory model in which execution memory and storage memory share one pool and can be evicted dynamically, spilling to disk when the cache is insufficient.

In terms of cluster shape, the driver breaks down the plan, schedules tasks and aggregates results, while executors run tasks on the CPU and memory allocated to them. Job parallelism is determined by the number of partitions, so partition settings directly affect cluster utilization efficiency.

Key Points

1. Partitions are the unit of parallelism. Too few partitions leave a small number of tasks carrying all the data, while too many raise scheduling overhead. Initial partitions are decided by block size when reading files; after joins and aggregations you should check the partition distribution and repartition or merge small files when necessary.

2. Avoid unnecessary shuffles. Prefer broadcast joins for relating a large table with a small one, distributing the small table to every executor; multiple operations on the same partition key can repartition once and then be reused; and filter early to trim data so that the shuffle volume becomes smaller.

3. Skew handling. When a few keys carry most of the data, individual tasks slow down the whole stage. You can salt the hot keys to spread them, split the hot ones out for separate handling, or enable adaptive query execution so that the engine merges small partitions at runtime and switches join strategies.

5. Distinguishing Spark from similar tools. Compared with Hadoop batch processing, Spark keeps intermediate results in memory instead of writing to and reading from disk at every step, so iterative algorithms benefit most. Compared with Flink, Flink centers on event time and low-latency stream processing, while Spark Structured Streaming simulates real time on top of micro-batches and wins by sharing one code base and optimizer with batch processing. Compared with single-machine analysis libraries, the advantage of Spark is that it can scale out once the data exceeds one machine memory, at the cost of startup and scheduling overhead, which actually makes it slower on small data.

Common Misconceptions

The first is calling actions at will for debugging, triggering full computation on the same DataFrame again and again; the right approach is to sample a small slice first or to look at the structure using a limited row count. The second is blindly enlarging executor memory while ignoring partition count and parallelism, leaving resources idle while stages remain slow. The third is treating a DataFrame as an ordinary collection and processing it row by row, giving up the gains from vectorization and code generation. The fourth is looking only at total elapsed time without opening the stage view, unable to tell whether reading, shuffle or skew is slowing the job.

There are two further engineering issues that are easy to overlook: writing a large number of small files into the same directory, where every file becomes a task at read time and scheduling overhead far exceeds the computation itself, so files should be merged periodically after writing; and failing to adjust the default partition count when the data volume changes, so the same code is smooth on small datasets but the stage count explodes once a large table arrives.

Application Scenarios

Offline warehouse ETL, log cleaning, feature engineering, large-scale joins and aggregations, structured stream processing, training data preparation for machine learning, data lake query acceleration, and user behavior analysis with precomputed reports.