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
- Architecture
- One address, four parts
- Session or batch
- Why Livy
- In the TDP editions
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.
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.
In the Glossary: Notebook · Session · Livy server
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Architecture
Clients, a Livy server, and, on the cluster, a Spark driver for each session and each batch.
What the figure shows:
- 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.
- The Livy server is the only point of contact for clients: it receives the REST requests on the
/sessionsand/batchespaths, 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. - YARN, through the ResourceManager, allocates the containers on the cluster.
- 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.
- 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 Glossarysessions— 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.
spark (Scala), pyspark (Python), sparkr (R) and sql.
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.
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.
Livy entered the Apache Incubator in 2017 and completed incubation in 2026, as a project of the Apache Software Foundation.
In the TDP editions
| Edition | Version | Documentation |
|---|---|---|
| TDP Datacenter | 0.8.0 | TDP 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.
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.
livy2-conf (server), livy2-env (environment), livy2-client-conf, livy2-spark-blacklist and livy2-log4j-properties (logs), all in the Spark3 service.
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.
The use of proxy users when submitting jobs can be enabled in livy.impersonation.enabled.
The Spark blacklist in livy2-spark-blacklist lists the Spark properties that users cannot override when starting a session.
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.
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.
Zeppelin, with the Livy interpreter (see Apache Zeppelin), and NiFi, with the nifi-livy-nar package (see Apache NiFi).
Next steps
TDP Datacenter
TDP Datacenter Architecture
Where Livy, Spark, and YARN sit in the cluster managed by Ambari.