Skip to main content

Batch, streaming, SQL, and machine learning
on the same engine.

Spark processes large volumes of data in parallel, splits the work among the cluster's processes, and keeps in memory whatever will be reused. Batch, streaming, SQL, and machine learning share the same engine. Follow a job, from the program that describes it to the result.

Distributed processing splits a large volume of data into parts that several nodes of a cluster process in parallel. Apache Spark does this work in both TDP editions, with programs written in SQL, Python, Scala, Java, or R.

Only when needed

Nothing runs before the action

Transformations are only noted down. Computation starts when an action asks for a result, and Spark already knows the whole path to it.

In parallel

One task per partition

Data is split into partitions, and each task processes one of them on an executor, at the same time as the others.

One engine

Many languages, the same engine

SQL or DataFrames in Python, Scala, Java, or R: whichever the API, the computation runs on the same execution engine.

On this page

See a job happen​

A program reads order files, filters the month's orders, and sums the total per store. Press Play or step through. Underlined names lead to the Glossary.

Cluster managerWho grants resources
Cluster managerStandalone, YARN, or Kubernetes
DriverWho coordinates
DriverRuns the program's main()
SparkContextCoordinates the application on the cluster
ExecutorsWho computes
ExecutorOn a worker node
ExecutorOn a worker node
ExecutorOn a worker node
StorageWhere the files are
Order filesHDFS or S3-compatible storage
Step 1 of 8

1 The driver connects to the cluster

The program starts in the driver, the process that runs the main() function and creates the SparkContext. To run on a cluster, the SparkContext connects to a cluster manager: Spark's own standalone cluster manager, YARN, or Kubernetes.

  1. The driver connects to the cluster. The program starts in the driver, the process that runs the main() function and creates the SparkContext. To run on a cluster, the SparkContext connects to a cluster manager: Spark's own standalone cluster manager, YARN, or Kubernetes.
  2. Executors on the worker nodes. The cluster manager allocates resources, and Spark obtains executors on the worker nodes: processes that run the computations and store data for the application. Each application has its own executors, which stay up for as long as it lasts.
  3. The code reaches the executors. The SparkContext sends the application code to the executors, in the JAR or Python files passed to it. From here on, the driver talks directly to the executors.
  4. Transformations: Spark only takes note. The program asks to read the files, filter the month's orders, and group them by store. These are transformations, and transformations are lazy: Spark merely records what was requested. No file has been read so far.
  5. An action triggers the job. The program asks for the total per store, and that request is an action. Only now does the computation begin: the action spawns a job, which Spark divides into stages that depend on each other, and each stage into tasks, one per data partition. The SparkContext sends the tasks to the executors.
  6. Each task, one partition. Each task works on a single partition. The executors read their part of the files and run the tasks in multiple threads, all at the same time.
  7. Shuffle: data changes places. To sum by store, the values for each store must be together, but they are scattered across several partitions. The shuffle redistributes the data among the executors: map tasks organize the data, and reduce tasks aggregate it. It involves disk, serialization, and network, which is why it is a costly operation.
  8. The result returns to the driver. The action returns the total per store to the driver, and the program carries on with the result in hand. The executors stay up for the next jobs of the same application.
Figure 1 - A Spark job. The driver obtains executors through the cluster manager and, from then on, talks to them directly: it sends the code and the tasks and receives the results.

Architecture​

A driver, a cluster manager, and executors on the worker nodes make up a Spark application.

Spark architecture: driver, cluster manager and worker nodes with executorsThe driver contains the SparkContext, which connects to the cluster manager. The cluster manager allocates executors on the worker nodes. Each worker node has an executor, which runs tasks and caches data. The SparkContext sends the code and tasks directly to the executors and receives the results from them.DriverSparkContextCluster ManagerWorker NodeTask 1Task nCacheExecutorWorker NodeTask 1Task nCacheExecutorWorker NodeTask 1Task nCacheExecutorConnection and executor allocationCode, tasks and results
Figure 2 - Spark architecture: the driver with the SparkContext, the cluster manager, and the worker nodes, each with its own executor. Underlined terms lead to the Glossary.

What the figure shows:

  1. The driver coordinates the application through the SparkContext: it requests resources from the cluster manager, sends code and tasks to the executors, and receives the results from them.
  2. The cluster manager receives the resource request from the driver and allocates executors on the worker nodes.
  3. The worker nodes each have an executor, which runs tasks and keeps data in cache.

Each application has its own executors, which run tasks in multiple threads and keep data in memory or on disk across them. This isolates applications from one another, so an application shares data with another only by writing it to an external storage system.

Because the driver distributes tasks and receives connections from the executors throughout the execution, it must be reachable from the worker nodes and is preferably run close to them, on the same local area network. Each driver has a web interface that shows the running tasks, the executors, and storage usage.

From application to task​

What Spark does with the program you wrote: four levels, each inside the other.

Level 1 of 4

Application

The program built on Spark: a driver and its executors on the cluster. Each application has its own SparkContext and executors, which stay up for as long as it lasts.

  • Deploy modeTells where the driver runs: in cluster mode, inside the cluster. In client mode, outside it, on the side of whoever submitted the application.
  1. Application. The program built on Spark: a driver and its executors on the cluster. Each application has its own SparkContext and executors, which stay up for as long as it lasts.
    • Deploy mode. Tells where the driver runs: in cluster mode, inside the cluster. In client mode, outside it, on the side of whoever submitted the application.
  2. Job. A parallel computation, made of several tasks, that is spawned in response to an action, such as save or collect. It is the term that appears in the driver's logs.
    • Transformation. Creates a dataset from another one, such as map or filter. It is lazy: Spark only records it and computes it when an action needs the result.
    • Action. Computes on the dataset and returns a value to the driver, such as reduce. It is the action that triggers the job.
  3. Stage. A smaller set of tasks into which the job is divided. Stages depend on each other, like the map and reduce phases of MapReduce.
    • Shuffle. Redistributes the data to group it differently across partitions, copying it across executors and machines. Map tasks organize the data, and reduce tasks aggregate it.
  4. Task. The unit of work sent to an executor. Each task operates on a single partition of the data.
    • Partition. A part of the dataset. Partitions are processed in parallel by the cluster nodes, and Spark runs one task for each of them.
Figure 3 - Each level contains the next: an application runs jobs, a job is divided into stages, and a stage into tasks. Transformation, action, shuffle, and partition accompany the levels.

The data that crosses these levels is described by an RDD, a collection of elements partitioned across the nodes of the cluster, or by a DataFrame, organized into named columns, like a table.

With DataFrames and SQL, Spark knows the structure of both the data and the computation, and uses that information to optimize execution.

Where Spark runs​

Spark does not depend on a specific cluster manager: it only needs to obtain executors that communicate with each other.

Standalone

The simple cluster manager that comes with Spark: a Master and Workers that connect to it through the Master's spark:// address. This is the TDP Kubernetes form, with the Master and the Workers in Pods.

Hadoop YARN

The Hadoop resource manager. The ResourceManager's address comes from the Hadoop configuration, and the application only indicates yarn as the master. This is the TDP Datacenter form. See YARN.

Kubernetes

The open-source system for automating deployment, scaling, and management of containerized applications also serves Spark as a cluster manager.

With any of them, the deploy mode decides where the driver sits. In client mode, the driver runs in the process of whoever submits the application. In cluster mode, it runs inside the cluster, in a Worker process under standalone or in an application master managed by YARN, and whoever submitted the application can disconnect.

Why Spark​

Batch and streaming

One engine for both

Batch processing and real-time streaming processing are unified, in the language of your choice: Python, SQL, Scala, Java, or R.

SQL

Distributed ANSI SQL queries

Fast, distributed queries for dashboards and ad hoc reports.

Data science

Exploratory analysis at scale

Exploratory analysis runs on petabyte-scale data, without resorting to downsampling.

Machine learning

From laptop to cluster

The same code that trains machine learning algorithms on a laptop scales to fault-tolerant clusters of thousands of machines.

Libraries

On the same engine

Spark SQL for structured data, pandas API on Spark, MLlib for machine learning, GraphX for graphs, and Structured Streaming for stream processing.

In memory

Data reused across operations

A dataset kept in memory with persist or cache is reused by the following actions, which speeds up iterative algorithms and interactive use. RDDs recover on their own from node failures.

Where it comes from

Spark started in 2009 as a research project at the AMPLab of the University of California, Berkeley, was open sourced in early 2010, and moved to the Apache Software Foundation in 2013. Today it is developed by a community of hundreds of developers from hundreds of organizations.

In the TDP editions​

Available in the TDP editions
EditionVersionDocumentation
TDP Datacenter3.5.3TDP Datacenter components
TDP Kubernetes4.0.2 · chart tdp-spark 3.0.3Configuration in TDP Kubernetes

In TDP Datacenter​

In TDP Datacenter, Spark is the Spark3 service of the stack, installed and managed by Ambari.

Service components

LIVY2_SERVER (slave, optional), SPARK3_CLIENT (client, one or more), SPARK3_JOBHISTORYSERVER (master, one or more) and SPARK3_THRIFTSERVER (slave, optional). The LIVY2_SERVER requires SPARK3_CLIENT, HDFS_CLIENT (HDFS) and YARN_CLIENT (YARN). The SPARK3_CLIENT requires HDFS_CLIENT (HDFS), MAPREDUCE2_CLIENT (MapReduce) and YARN_CLIENT (YARN). The SPARK3_JOBHISTORYSERVER requires HDFS_CLIENT (HDFS), MAPREDUCE2_CLIENT (MapReduce), YARN_CLIENT (YARN) and HIVE_CLIENT (Hive). The SPARK3_THRIFTSERVER requires HDFS_CLIENT (HDFS), MAPREDUCE2_CLIENT (MapReduce), YARN_CLIENT (YARN) and HIVE_METASTORE (Hive). Component category.

Cluster manager

Applications run as YARN applications, with the cluster manager in spark.master and the YARN queue in spark.yarn.queue, in the spark3-defaults configuration.

Completed applications

The History Server rebuilds the applications' interface from the event logs, with the directory configurable in spark.eventLog.dir and spark.history.fs.logDirectory.

SQL over JDBC and ODBC

The Spark3 Thrift Server exposes Spark SQL to users and applications over JDBC/ODBC, like HiveServer2, with its own configuration in spark3-thrift-sparkconf.

Tables and formats

Access to the Hive catalog is configurable in spark3-hive-site-override. See Hive Metastore. The Iceberg catalogs spark_catalog and tdp_catalog and the Iceberg extensions are in spark3-defaults. See Apache Iceberg.

Dependencies

Ambari requires in the cluster HDFS, YARN and Hive.

Required by

The JUPYTERLAB (JupyterLab) requires SPARK3_CLIENT.

What triggers Spark

Livy, which is part of the service (see Apache Livy); Airflow, with the spark-submit of the Spark3 Client; JupyterLab, which points to Spark in jupyterlab-env; and Zeppelin, with the Spark interpreters.

Authentication

Kerberos, which can be enabled in Ambari. See Security in TDP Datacenter.

In TDP Kubernetes​

In TDP Kubernetes, Spark is installed by the tdp-spark chart, which packages the Apache Spark chart and adds the TDP configurations and integrations. The chart brings up a standalone cluster, with the Master and the Workers in Pods.

Workers

The number of Workers is set in spark.worker.replicaCount. The HPA for the Workers, off by default in spark.worker.autoscaling.enabled, requires the Metrics Server in the cluster.

Storage

S3-compatible, such as Apache Ozone, through the S3A connector, in tdp-spark.hadoopConfig (fs.s3a.*) and tdp-spark.customSparkConfig.properties (spark.hadoop.fs.s3a.*). The values arrive through Secrets or external tooling.

Web interface

The chart enables Spark's native reverse proxy, so that the Master interface reaches the Workers and the running applications. Ingress or Gateway API take the interface outside the cluster.

Completed applications

The History Server is optional, off by default in spark.historyServer.enabled. It reads the event logs from S3-compatible storage or from a persistent volume, in the same directory where the drivers write them.

Configurable integrations

JupyterHub (tdp-spark.integration.jupyter.enabled): in notebooks, the driver address is the Pod IP, so that the Workers can always reach it.

Table formats

Delta Lake and Iceberg are optional blocks. The Iceberg block describes a catalog in the Hive Metastore and S3-compatible storage. Both can be on at the same time. The Iceberg, Delta Lake, and S3A jars already come in the Spark image, so jobs do not download anything at run time. See Spark Configuration.

Python libraries in the image

Besides PySpark and delta-spark, the Spark image includes Great Expectations, Polars, DuckDB, and PyIceberg. The versions are in TDP Kubernetes Components.

Table maintenance

The tdp-iceberg and tdp-deltalake charts use Spark as the runtime for table maintenance jobs.

In both editions, the path of a job is the one in Figure 1. What changes is the cluster manager: YARN in TDP Datacenter, the standalone Master in TDP Kubernetes.

Next steps​

Sources​

Apache Spark

Apache Spark - Overview

Apache Spark - Cluster Mode Overview

Apache Spark - RDD Programming Guide

Apache Spark - Spark SQL, DataFrames and Datasets Guide

Apache Spark - Spark Standalone Mode

Apache Spark - Running Spark on YARN

Apache Spark - Distributed SQL Engine

Apache Spark - Monitoring and Instrumentation

Apache Spark - Project History