Skip to main content

Spark accessible through a REST API,
without a Spark client.

Livy receives code and applications over HTTP and runs them on the Spark cluster, with several users at the same time and Spark contexts that last from one request to the next. Follow a session, from its creation to the result of a code snippet.

A Livy server receives code snippets and whole applications for Apache Spark through a REST API and runs them on a cluster, returning the results. Apache Livy plays this role in TDP Datacenter, as part of the Spark3 service.

No Spark client

HTTP only

A web or mobile application, a notebook, or a script reaches Spark with REST requests, without a Spark client installed.

Interactive

One session, many snippets

A session keeps an active SparkContext on the cluster and receives statements in Scala, Python, R, or SQL.

Batch

A whole application

A batch takes a ready-made application to the cluster, with its main class and arguments, and Livy tracks the state and the log.

On this page

Watch a session happen​

A notebook opens a Scala session, computes 1 + 1, and closes the session. Press Play or move forward step by step. Underlined names lead to the Glossary.

ClientWho sends the code
NotebookZeppelin interpreter
Livy serverWho receives and tracks
Sessionstarting, idle, busy
Livy serverREST API
Statementrunning, available
YARN clusterWhere Spark computes
ResourceManagerAllocates the containers
DriverWith the session's SparkContext
ExecutorsCompute the tasks
Step 1 of 7

1 The client creates a session

The notebook sends a POST to /sessions, with the kind spark, for Scala code. The Livy server responds with the id of the new session, still in the starting state.

  1. The client creates a session. The notebook sends a POST to /sessions, with the kind spark, for Scala code. The Livy server responds with the id of the new session, still in the starting state.
  2. The session starts on the cluster. Livy submits the session to YARN, which allocates containers on the cluster. The session driver, with its SparkContext, runs there, and not on the Livy server, and obtains its executors.
  3. The session becomes idle. The driver connects back to the Livy server, and the session moves to the idle state: it is waiting for code. The notebook follows this state with GET /sessions/{id}/state.
  4. A statement comes in. The notebook sends the snippet 1 + 1 in a POST to /sessions/{id}/statements. When the execution takes a while, Livy responds before it finishes, with the address of the statement to query later.
  5. The driver executes the code. Livy hands the code to the session driver, which executes it with the executors. Meanwhile, the session is busy and the statement is running.
  6. The result becomes available. When it finishes, the statement moves to the available state, with the output in the output field, and the session goes back to idle. The notebook queries the statement and receives the result. The SparkContext stays active for the next statements.
  7. The client closes the session. With a DELETE on /sessions/{id}, the notebook closes the session, and Livy ends the session job on the cluster.
Figure 1 - A Livy session. The client talks only to the Livy server. The session driver, with the SparkContext, runs on the cluster and executes the code with the executors.

Architecture​

Clients, a Livy server, and, on the cluster, a Spark driver for each session and each batch.

Livy architecture: clients, Livy server, YARN, and the sessions and batches in the clusterClients, such as a notebook with the Zeppelin interpreter or a web or mobile application, talk to the Livy server through the REST API, on the sessions and batches paths. The Livy server submits the interactive sessions and the batches to YARN, whose ResourceManager allocates containers in the cluster. Each session and each batch has its own Spark driver, with the SparkContext, and its own executors, all in the cluster and not on the Livy server. The Livy server sends code to the session drivers and receives the results from them.ClientsNotebookZeppelin interpreterApplicationWeb or mobileLivy serverREST API/sessions · /batchesSessionsReceive statementsScala, Python,R and SQLBatchesWhole applicationScala, Javaor PythonYARNResourceManagerAllocates containersYARN-managed clusterspark sessionDriverwith the SparkContextExecutorIn a containerExecutorIn a containerpyspark sessionDriverwith the SparkContextExecutorIn a containerExecutorIn a containerBatchDriverof the submitted applicationExecutorIn a containerExecutorIn a containerREST requestsSubmission to YARN and containersSession code and resultsTasks to executors
Figure 2 - Livy architecture: clients, Livy server, YARN and, on the cluster, the sessions and the batch, each with its own driver and its own executors. Underlined terms lead to the Glossary.

What the figure shows:

  1. The clients, such as a notebook with the Zeppelin interpreter or a web or mobile application, talk to the Livy server through the REST API.
  2. The Livy server is the only point of contact for clients: it receives the REST requests on the /sessions and /batches paths, creates the sessions and the batches, and tracks each one. Sessions receive statements in Scala, Python, R and SQL, and batches are the entire application, in Scala, Java or Python. The server submits the interactive sessions and the batches to YARN.
  3. YARN, through the ResourceManager, allocates the containers on the cluster.
  4. Each session has its own driver, with the SparkContext, and its own executors, each in a cluster container. The Livy server sends the code to the session drivers and receives the results from them, and each driver sends tasks to its executors.
  5. The batch has its own driver, that of the submitted application, and its own executors, each in a cluster container.

Because the computation happens on the cluster and not on the Livy server, Livy manages several SparkContexts at the same time, with fault tolerance and concurrency.

With Spark submitting applications to YARN in cluster mode, the resources of each user session are accounted for in the YARN cluster.

One address, four parts​

Everything in Livy is a resource of the REST API. The result of a code snippet lives at an address like this one.

Click each part of the address.

Sessions

The collection of active interactive sessions. A POST here creates a new session. A GET lists the active ones.

See in the Glossary
sessions — Sessions
The collection of active interactive sessions. A POST here creates a new session. A GET lists the active ones.
0 — A session
The id that Livy returns when creating the session. With it, the client queries the state and the log of the session and, with a DELETE, closes it.
statements — Statements
The code snippets sent to that session. A POST here executes a new statement. A GET lists those already sent.
1 — A statement
A specific code snippet, with the code, the state, the progress, and the output of the execution. A statement in progress can be cancelled.

Batches follow the same logic, under /batches: each batch has an id, with its own state and its own log.

Session or batch​

Two ways to bring work to Spark through Livy.

Session

A shell on the cluster

An interactive Scala, Python, R, or SQL shell, with the SparkContext active between one statement and the next. The code kind can come with each statement, and cached data stays available for the following snippets.

Batch

A ready-made application

The application file, the main class, and the command-line arguments. The client tracks the state and the log until the end.

In both

On behalf of another user

Sessions and batches can run on behalf of a proxy user, and both accept JAR files, Python files, and Spark configuration properties.

Code kinds

spark (Scala), pyspark (Python), sparkr (R) and sql.

Session states

not_started and starting before it is ready; idle, waiting for code; busy, executing a statement; shutting_down when closing; and, at the end, success, killed, dead or error.

Statement states

waiting, in the queue; running, executing; available, with the response ready; error, if it failed; cancelling and cancelled, if it was cancelled.

Why Livy​

Applications

Spark behind an API

Web and mobile applications submit jobs to Spark programmatically, without a Spark client, which brings Spark to interactive applications.

Multi-user

Several users at the same time

Submission is multi-user and fault-tolerant: several users interact with the Spark cluster at the same time, reliably.

Long-lived contexts

One SparkContext, many jobs

A long-running SparkContext serves several jobs and several clients, and cached RDDs and DataFrames are shared among them.

On the cluster

The computation stays off the server

SparkContexts run on the cluster, and not on the Livy server, which favors fault tolerance and concurrency across several contexts.

Ways to submit

Code, JAR, or client API

Code snippets, precompiled JAR files, or the Java and Scala client API, with the result returned synchronously or asynchronously.

Security

Authenticated communication

Communication with Livy is secure and authenticated, and existing programs need no changes to run through it.

Where it comes from

Livy entered the Apache Incubator in 2017 and completed incubation in 2026, as a project of the Apache Software Foundation.

In the TDP editions​

Available in the TDP editions
EditionVersionDocumentation
TDP Datacenter0.8.0TDP Datacenter components

In TDP Datacenter​

In TDP Datacenter, Livy is a component of the stack's Spark3 service, installed and managed by Ambari. The service depends on HDFS, YARN, and Hive.

Service component

Livy for Spark3 Server, a slave and optional component of the Spark3 service, alongside the Spark3 History Server, the Spark3 Thrift Server, and the Spark3 Client. See Apache Spark and Component category.

Configurations

livy2-conf (server), livy2-env (environment), livy2-client-conf, livy2-spark-blacklist and livy2-log4j-properties (logs), all in the Spark3 service.

Spark and cluster manager

The Spark master for the sessions is set in livy.spark.master. See YARN. The Spark installation and the Spark and Hadoop configurations that Livy uses are set in livy2-env.

Users

The use of proxy users when submitting jobs can be enabled in livy.impersonation.enabled.

Protected properties

The Spark blacklist in livy2-spark-blacklist lists the Spark properties that users cannot override when starting a session.

Sessions and recovery

The wait time before an idle session is closed is set in livy.server.session.timeout. The recovery mode and the location where Livy stores state for recovery are set in livy.server.recovery.mode, livy.server.recovery.state-store and livy.server.recovery.state-store.url.

REST API

The server port is set in livy.server.port, and CSRF protection for the REST API can be enabled in livy.server.csrf_protection.enabled. HiveContext in the Livy interpreter can be enabled in livy.repl.enableHiveContext.

What calls Livy

Zeppelin, with the Livy interpreter (see Apache Zeppelin), and NiFi, with the nifi-livy-nar package (see Apache NiFi).

Next steps​

Sources​

Apache Livy

Apache Livy - Getting Started

Apache Livy - REST API

Apache Livy - Examples

Apache Incubator - Livy Project Status