SparkSession

org.apache.spark.sql.SparkSession
See theSparkSession companion object
class SparkSession extends AutoCloseable

The entry point to programming Spark with the DataFrame API over Spark Connect.

 val spark = SparkSession.builder
   .remote("sc://localhost:15002")
   .appName("my-app")
   .getOrCreate()

 spark.range(10).filter(col("id") % 2 === 0).show()
 spark.stop()

A session owns the SparkConnectClient, a monotonic plan-id allocator (so every relation is uniquely identifiable to the server), an Arrow allocator for decoding results, and the RuntimeConfig facade.

Attributes

Companion
object
Graph
Supertypes
trait AutoCloseable
class Object
trait Matchable
class Any

Members list

Type members

Classlikes

object implicits extends SQLImplicits

Implicit conversions ($"col", Seq(...).toDF(...)); use import spark.implicits.*.

Implicit conversions ($"col", Seq(...).toDF(...)); use import spark.implicits.*.

Attributes

Supertypes
class SQLImplicits
class Object
trait Matchable
class Any
Self type
implicits.type

Value members

Concrete methods

def addTag(tag: String): Unit

Adds a tag to every operation started by the current thread (and threads it creates later), so they can be interrupted together with interruptTag. A tag must be non-empty and must not contain ,.

Adds a tag to every operation started by the current thread (and threads it creates later), so they can be interrupted together with interruptTag. A tag must be non-empty and must not contain ,.

Attributes

def clearTags(): Unit

Removes all of the current thread's operation tags.

Removes all of the current thread's operation tags.

Attributes

Clones this session on the server (Spark 4.1+): the new session starts with a copy of this session's SQL configurations, temporary views, registered functions, and catalog state, and is independent afterwards.

Clones this session on the server (Spark 4.1+): the new session starts with a copy of this session's SQL configurations, temporary views, registered functions, and catalog state, and is independent afterwards.

Attributes

def cloneSession(sessionId: String): SparkSession

Like cloneSession, with the given session id (a UUID) for the clone.

Like cloneSession, with the given session id (a UUID) for the clone.

Attributes

override def close(): Unit

Attributes

Definition Classes
AutoCloseable
def createDataFrame(rows: Seq[Row], schema: StructType): DataFrame

Creates a DataFrame from a local sequence of Rows using the given schema.

Creates a DataFrame from a local sequence of Rows using the given schema.

Attributes

def createDataFrame(rows: List[Row], schema: StructType): DataFrame

Creates a DataFrame from a Java list of Rows using the given schema.

Creates a DataFrame from a Java list of Rows using the given schema.

Attributes

def createDataFrame[A <: Product](data: Seq[A])(using enc: Encoder[A]): DataFrame

Creates a DataFrame from a local sequence of case classes or tuples.

Creates a DataFrame from a local sequence of case classes or tuples.

Attributes

def createDataset[T](data: Seq[T])(using enc: Encoder[T]): Dataset[T]

Creates a typed Dataset from a local sequence of T using its Encoder. The data is serialized via the encoder and shipped as a local relation; no server-side closure is involved.

Creates a typed Dataset from a local sequence of T using its Encoder. The data is serialized via the encoder and shipped as a local relation; no server-side closure is involved.

Attributes

def createDataset[T](data: List[T])(using enc: Encoder[T]): Dataset[T]

Creates a typed Dataset from a Java list of T using its Encoder.

Creates a typed Dataset from a Java list of T using its Encoder.

Attributes

Returns a DataFrame with no rows or columns.

Returns a DataFrame with no rows or columns.

Attributes

def emptyDataset[T](using enc: Encoder[T]): Dataset[T]

Returns an empty Dataset of type T.

Returns an empty Dataset of type T.

Attributes

def executeCommand(runner: String, command: String, options: Map[String, String]): DataFrame

Runs command in an external execution engine (runner, e.g. a JDBC data source) rather than Spark, returning the command's output, if any, as a DataFrame.

Runs command in an external execution engine (runner, e.g. a JDBC data source) rather than Spark, returning the command's output, if any, as a DataFrame.

Attributes

def getTags(): Set[String]

The tags the current thread attaches to the operations it starts.

The tags the current thread attaches to the operations it starts.

Attributes

def interruptAll(): Seq[String]

Interrupts all operations of this session; returns the ids of interrupted operations.

Interrupts all operations of this session; returns the ids of interrupted operations.

Attributes

def interruptOperation(operationId: String): Seq[String]

Interrupts the operation with the given id; returns the ids of interrupted operations.

Interrupts the operation with the given id; returns the ids of interrupted operations.

Attributes

def interruptTag(tag: String): Seq[String]

Interrupts the operations with the given tag; returns the ids of interrupted operations.

Interrupts the operations with the given tag; returns the ids of interrupted operations.

Attributes

def newDataFrame(relType: RelType): DataFrame

Creates a DataFrame from an arbitrary RelType, tagging it with a unique plan id.

Creates a DataFrame from an arbitrary RelType, tagging it with a unique plan id.

This is the primary extension point for Spark Connect plugins: build any relation the server understands -- including a custom extension relation -- and turn it into a DataFrame. For the common case of a packed plugin message, prefer the extension overload.

Attributes

def newDataFrame(extension: Any): DataFrame

Creates a DataFrame backed by an extension relation carrying an already packed protobuf message. This is the Scala counterpart of the PySpark client's

Creates a DataFrame backed by an extension relation carrying an already packed protobuf message. This is the Scala counterpart of the PySpark client's

plan.extension.Pack(message); DataFrame(plan, session)

and targets server-side org.apache.spark.sql.connect.plugin.RelationPlugin implementations.

 import com.google.protobuf.any.Any as ProtoAny

 // `myPluginMessage` is a ScalaPB message generated from the plugin's own .proto.
 // Existing DataFrames can be embedded via their `relation` (see Dataset.relation):
 //   val payload = MyPlugin(input = Some(existingDf.relation))
 val payload: ProtoAny = ProtoAny.pack(myPluginMessage)
 val df = spark.newDataFrame(payload)

Attributes

def newRelation(relType: RelType): Relation

Wraps a RelType into a fresh proto.Relation tagged with a unique plan id.

Wraps a RelType into a fresh proto.Relation tagged with a unique plan id.

This is a low-level building block intended for Spark Connect plugin authors who need to construct custom relations (for example, a relation handled by a server-side org.apache.spark.sql.connect.plugin.RelationPlugin). It is a stable extension point but not part of the day-to-day DataFrame API; ordinary code should use range, sql, createDataFrame, and friends instead.

Attributes

Starts a new independent session against the same endpoint: a fresh server-side session with its own configuration and temporary views.

Starts a new independent session against the same endpoint: a fresh server-side session with its own configuration and temporary views.

Attributes

def pipeline(defaultCatalog: Option[String], defaultDatabase: Option[String], sqlConf: Map[String, String]): Pipeline

Creates a new Spark Declarative Pipeline (a dataflow graph) in this session. Available on Spark 4.1 and later servers.

Creates a new Spark Declarative Pipeline (a dataflow graph) in this session. Available on Spark 4.1 and later servers.

Attributes

def range(end: Long): Dataset[Long]

Creates a Dataset with a single id column of [0, end).

Creates a Dataset with a single id column of [0, end).

Attributes

def range(start: Long, end: Long): Dataset[Long]

Creates a Dataset with a single id column of [start, end).

Creates a Dataset with a single id column of [start, end).

Attributes

def range(start: Long, end: Long, step: Long): Dataset[Long]

Creates a Dataset with a single id column of [start, end) stepping by step.

Creates a Dataset with a single id column of [start, end) stepping by step.

Attributes

def range(start: Long, end: Long, step: Long, numPartitions: Int): Dataset[Long]

Creates a Dataset with a single id column of [start, end) in numPartitions.

Creates a Dataset with a single id column of [start, end) in numPartitions.

Attributes

Returns a DataFrameReader that can be used to read non-streaming data as a DataFrame.

Returns a DataFrameReader that can be used to read non-streaming data as a DataFrame.

Attributes

Returns a org.apache.spark.sql.streaming.DataStreamReader for reading streaming data.

Returns a org.apache.spark.sql.streaming.DataStreamReader for reading streaming data.

Attributes

def removeTag(tag: String): Unit

Removes a tag previously added by addTag on the current thread.

Removes a tag previously added by addTag on the current thread.

Attributes

def sessionId: String

The client session id (a UUID).

The client session id (a UUID).

Attributes

Make this the active session for the current thread.

Make this the active session for the current thread.

Attributes

def sql(sqlText: String): DataFrame

Executes a SQL statement, returning its result as a DataFrame. Like Apache Spark, the statement is sent to the server right away: commands (DDL / DML such as CREATE TABLE or INSERT) run immediately, while queries stay lazy until an action.

Executes a SQL statement, returning its result as a DataFrame. Like Apache Spark, the statement is sent to the server right away: commands (DDL / DML such as CREATE TABLE or INSERT) run immediately, while queries stay lazy until an action.

Attributes

def sql(sqlText: String, args: Array[_]): DataFrame

Executes a SQL statement with positional parameters bound into the query.

Executes a SQL statement with positional parameters bound into the query.

Attributes

def sql(sqlText: String, args: Map[String, Any]): DataFrame

Executes a SQL statement with named parameters bound into the query.

Executes a SQL statement with named parameters bound into the query.

Attributes

def sql(sqlText: String, args: Map[String, Any]): DataFrame

(Java-specific) Executes a SQL statement with named parameters bound into the query.

(Java-specific) Executes a SQL statement with named parameters bound into the query.

Attributes

def stop(): Unit

Releases the server-side session resources and closes the channel.

Releases the server-side session resources and closes the channel.

Attributes

def table(tableName: String): DataFrame

Returns the named table or view as a DataFrame.

Returns the named table or view as a DataFrame.

Attributes

def time[T](f: => T): T

Runs f, printing the time it took to the console, and returns its result.

Runs f, printing the time it took to the console, and returns its result.

Attributes

Returns the table-valued functions, e.g. spark.tvf.explode(...).

Returns the table-valued functions, e.g. spark.tvf.explode(...).

Attributes

def version: String

The version of Spark on which the connected server is running.

The version of Spark on which the connected server is running.

Attributes

def withActive[T](block: => T): T

Runs block with this session as the active session, restoring the previous one after.

Runs block with this session as the active session, restoring the previous one after.

Attributes

Concrete fields

lazy val catalog: Catalog

Returns the org.apache.spark.sql.catalog.Catalog interface for this session.

Returns the org.apache.spark.sql.catalog.Catalog interface for this session.

Attributes

lazy val conf: RuntimeConfig

Runtime configuration for Spark.

Runtime configuration for Spark.

Attributes