Quick answer
Apache Spark is an open-source engine that processes large datasets in parallel across a cluster, keeping intermediate data in memory so it runs much faster than Hadoop MapReduce for iterative work. A Spark application has a driver that plans the work and executors that run it. You mostly work with the DataFrame API, and transformations run lazily until an action asks for a result. This Apache Spark tutorial explains how Spark works and walks you through your first PySpark program.
Apache Spark is the most widely used engine for processing large amounts of data across a cluster of machines. This tutorial explains what Spark is, why it replaced Hadoop MapReduce for most work, how a Spark application actually runs, and the core ideas you need before writing your first job. It ends with a short working example.
What is Apache Spark?
Apache Spark is an open-source, distributed engine for processing data. You write a program that describes what to do with the data, and Spark splits the work into many small tasks and runs them in parallel across a cluster, which can be anything from a single laptop to thousands of servers.
The same engine handles several kinds of work: batch jobs that transform large datasets, SQL queries, streaming data that arrives continuously, and machine learning. Before Spark, each of those usually needed a separate system. You can write Spark programs in Python (PySpark), Scala, Java, SQL and R.
Why Spark was built: the problem with MapReduce
Hadoop made it practical to store and process data spread across many machines. It has three main parts: HDFS for distributed storage, YARN for managing cluster resources, and MapReduce for processing.
MapReduce splits a job into two steps. The map step reads input and turns each record into key-value pairs. The reduce step groups the pairs by key and combines them. Counting words in a set of documents is the classic example: map emits (word, 1) for every word, and reduce adds up the ones for each word.
The model scales well, but it has a cost. Every MapReduce job writes its results to disk, and the next job reads them back. Many real tasks are a chain of steps, and machine learning algorithms repeat the same step dozens of times, so most of the time goes on disk reads and writes rather than computation. Interactive analysis, where you run a query and want an answer in seconds, was impractical.
Spark’s central idea is to keep intermediate data in memory between steps wherever possible, and to plan a whole chain of operations together rather than one job at a time. That makes iterative and interactive work dramatically faster.
A short history of Spark
- 2009: started by Matei Zaharia at UC Berkeley’s AMPLab as a research project.
- 2010: released as open source.
- 2013: donated to the Apache Software Foundation. The original creators founded Databricks the same year.
- 2014: became a top-level Apache project, and Spark 1.0 was released.
- 2016: Spark 2.0 made DataFrames and Datasets the main API and introduced Structured Streaming.
- 2020: Spark 3.0 added Adaptive Query Execution, which re-optimizes queries while they run.
- 2025: Spark 4.0 made ANSI SQL mode the default and expanded Spark Connect, which lets lightweight clients talk to a remote Spark cluster.
Apache Spark features
- Speed. In-memory processing and whole-job planning make Spark much faster than MapReduce, especially for iterative work. The often-quoted “up to 100 times faster” comes from iterative workloads held in memory. For a single pass over data on disk, the gain is smaller.
- One engine for many workloads. Batch processing, SQL, streaming and machine learning share one API and one cluster.
- Several languages. Python, Scala, Java, SQL and R, with a high-level DataFrame API that looks much the same in each.
- Many data sources. Spark reads and writes Parquet, ORC, JSON, CSV, Avro, Delta Lake and Apache Iceberg tables, JDBC databases, Kafka, Cassandra, Hive tables, and cloud object storage such as Amazon S3.
- Fault tolerance. If a machine fails mid-job, Spark recomputes only the lost pieces, using the record of how each piece was produced (its lineage).
- Runs where your data is. Spark can run on YARN alongside Hadoop, on Kubernetes, in its own standalone cluster, or as a managed service from cloud providers.
Apache Spark components
Spark is a core engine with several libraries built on top of it.

Spark Core
The foundation. It handles task scheduling, memory management, fault recovery and reading from storage systems, and it provides the original low-level API, the RDD.
Spark SQL and DataFrames
Spark SQL works with structured data: data with columns and types. You can query it with SQL or with the DataFrame API, and mix the two in one program. Behind both is the Catalyst optimizer, which rewrites your query into an efficient plan, much as a database does. For most work today, DataFrames are the API you will use.
Structured Streaming
Structured Streaming processes data that keeps arriving, from sources such as Kafka or files landing in a folder. You write the same DataFrame code you would write for a batch job, and Spark runs it incrementally as new data arrives, keeping the results up to date. It replaced the older Spark Streaming library, which was built on DStreams and is now legacy.
MLlib
Spark’s machine learning library includes algorithms for classification, regression, clustering and recommendation, plus tools for feature engineering, building pipelines, and evaluating models. The DataFrame-based API, in the pyspark.ml package, is the current one. The older RDD-based API is in maintenance mode.
GraphX
A library for graph processing, such as analyzing connections in a social network, with built-in algorithms like PageRank and connected components. It has a Scala API only. Python users usually turn to the separate GraphFrames package instead.
How a Spark application runs
Every Spark application has three parts working together.

The driver
The driver is the process running your main program. It creates the SparkSession, the entry point to Spark. When your code asks for a result, the driver turns your operations into a plan, splits it into stages and tasks, and sends those tasks to the executors. When the driver stops, the application ends.
Executors
Executors are worker processes that run on the machines in the cluster. Each runs the tasks it is given, keeps any data you have asked Spark to cache, and sends results back to the driver. By default they live for the whole application.
The cluster manager
The cluster manager provides the machines and memory that executors run on. Spark supports:
- Standalone: Spark’s own simple cluster manager.
- Hadoop YARN: the usual choice on an existing Hadoop cluster.
- Kubernetes: runs the driver and executors as containers, now common in cloud deployments.
Apache Mesos was supported in the past, but support was deprecated in Spark 3.2 and removed in Spark 4.0. Spark also offers dynamic allocation, which adds executors when there is work waiting and removes them when they sit idle.
Jobs, stages and tasks
Each time your code asks for a result, Spark starts a job. The job is split into stages wherever data has to be shuffled, or redistributed between machines, for example to group rows by a key. Each stage is made of tasks, one per partition of the data, which run in parallel.
RDDs, DataFrames and lazy evaluation

RDDs
The Resilient Distributed Dataset (RDD) was Spark’s original data structure. It is an immutable collection of objects split into partitions that are spread across the cluster.
- Resilient: each RDD remembers how it was produced from other data, so a lost partition can be recomputed.
- Distributed: the partitions live on different machines and are processed in parallel.
- Immutable: you never change an RDD. Each operation produces a new one.
You create an RDD from a collection in your program (with parallelize()), from a file (with textFile()), or by transforming another RDD.
DataFrames
A DataFrame is a distributed table with named, typed columns. It is built on the same engine as RDDs, but because Spark knows the structure of the data, the Catalyst optimizer can plan the work far more efficiently. That makes DataFrames both easier to write and usually faster. Use DataFrames by default, and drop down to RDDs only when you need fine-grained control. In Scala and Java, a Dataset is a typed version of a DataFrame.
Transformations and actions
Spark operations come in two kinds:
- Transformations describe a new dataset from an existing one:
filter,select,groupBy,join,map. - Actions ask for a result:
count,show,collect, or writing data out withwrite.
Transformations are lazy: Spark does not run them when you call them. It only records them, and builds a plan when an action finally asks for a result. That gives it the chance to optimize the whole chain, for example by reading only the columns you actually use.
Narrow and wide transformations
A narrow transformation, such as filter or select, needs only the data already in each partition. A wide transformation, such as groupBy or join, needs data from many partitions to be brought together, which causes a shuffle across the network. Shuffles are usually the most expensive part of a Spark job, so reducing them is the first step in most tuning.
Caching
If you reuse the same dataset several times, call cache() or persist() so Spark keeps it in memory after the first time it is computed, rather than recomputing it from the source for every action.
Your first Spark program
Here is word count, the “hello world” of big data, written with PySpark DataFrames. Install PySpark with pip install pyspark (Java must be installed too) and run this on your own machine. Spark runs in local mode, using your computer’s cores as the cluster.
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
spark = SparkSession.builder.appName("WordCount").getOrCreate()
lines = spark.read.text("sample.txt")
counts = (
lines
.select(F.explode(F.split(F.lower(F.col("value")), r"\W+")).alias("word"))
.filter(F.col("word") != "")
.groupBy("word")
.count()
.orderBy(F.desc("count"))
)
counts.show(10)
spark.stop()
Everything before show() is a transformation, so nothing runs until that line. Then Spark reads the file, splits each line into words, groups identical words (a wide transformation, so a shuffle happens), counts them, and prints the ten most common. While it runs, open http://localhost:4040 to see the Spark UI, which shows the job, its stages and its tasks.
Apache Spark limitations
- No storage of its own. Spark processes data but does not store it. It relies on HDFS, cloud object storage, or a database.
- Memory-hungry. In-memory processing needs plenty of RAM. Undersized executors spill to disk or fail with out-of-memory errors.
- Streaming latency. Structured Streaming normally processes data in small micro-batches, which typically means latency of a second or more. For per-event processing in milliseconds, engines such as Apache Flink are often a better fit.
- The small files problem. Thousands of tiny input files create thousands of tiny tasks, and overhead dominates. Compacting files or repartitioning helps.
- Tuning still matters. Adaptive Query Execution now handles a lot automatically, but partition counts, memory settings, data skew and shuffles still need attention on large jobs.
- Overkill for small data. If your data fits comfortably on one machine, tools such as pandas, Polars or DuckDB are simpler and often faster.
Apache Spark use cases
- ETL and data pipelines: cleaning, joining and reshaping raw data into tables ready for analysis. This is the most common use by far.
- Data lakes and lakehouses: processing data in open table formats such as Delta Lake and Apache Iceberg on cloud storage.
- Interactive analysis: running SQL or DataFrame queries over very large datasets from a notebook.
- Streaming: processing events from Kafka as they arrive, for live dashboards, alerting or fraud detection.
- Machine learning at scale: preparing features and training models on data too large for one machine.
Industries that rely on it include finance (fraud detection and risk), retail and e-commerce (recommendations and demand forecasting), telecommunications (network and customer analytics), healthcare (processing clinical and claims data) and media (personalization).
Where to go next
- Run the word count example, then try it on a larger file and watch the job in the Spark UI.
- Learn the common DataFrame operations:
select,filter,withColumn,groupBy,joinand window functions. - Read a Parquet file and compare how quickly Spark reads it against CSV. Columnar formats are a big part of Spark’s speed.
- Try a small Structured Streaming job that reads files as they land in a folder.
What to learn after this Apache Spark tutorial
Spark took the core idea of Hadoop, processing data in parallel across many machines, and made it fast enough for interactive work by keeping data in memory and planning whole chains of work at once. If you understand the driver and executors, lazy evaluation, and the cost of a shuffle, you understand most of what makes Spark jobs fast or slow. If you have questions about getting your first job running, ask in the comments.
Frequently asked questions
Is Apache Spark faster than Hadoop MapReduce?
Usually, yes, especially for iterative and interactive work, because Spark keeps intermediate data in memory and plans a whole chain of operations together. For a single pass over data stored on disk, the difference is smaller.
Does Spark replace Hadoop?
Spark replaces MapReduce, Hadoop’s processing engine, for most work, but not Hadoop as a whole. Spark has no storage of its own, so it often runs on HDFS and YARN, or on cloud object storage and Kubernetes instead.
Which language should I use for Spark?
Python (PySpark) is the most popular and the easiest place to start. Scala is Spark’s native language and has every API, including GraphX. With DataFrames, performance is similar in each language, because Spark optimizes the query plan the same way.
What is the difference between an RDD and a DataFrame?
An RDD is a distributed collection of objects with no schema. A DataFrame is a distributed table with named, typed columns. Because Spark knows a DataFrame’s structure, its Catalyst optimizer can plan the work far more efficiently, so DataFrames are the default choice.
Can I learn Spark on my laptop?
Yes. Install PySpark with pip install pyspark, and Spark runs in local mode, using your computer’s cores as the cluster. You also need Java installed.
What is lazy evaluation in Spark?
Transformations such as filter and groupBy are recorded but not run. Spark runs them only when an action, such as count, show or write, asks for a result, which lets it optimize the whole chain at once.
Sources and further reading
- Apache Spark documentation: the official Spark docs
- Spark SQL, DataFrames and Datasets guide: the DataFrame API used in this tutorial
- Structured Streaming programming guide: Spark’s current streaming engine
- Apache Spark news: release announcements

Leave a Reply