Dataset (Spark)
| Also known as | Typed Dataset, Dataset[T]
|
|---|---|
| Field | Data processing APIs |
| Introduced | Spark 1.6.0 |
| Key parts | A JVM type T, an encoder for that type, a logical plan
|
| Open source implementations | Apache Spark (Scala and Java APIs) |
| Related | DataFrame, Encoder (Spark), RDD, Spark Connect |
In Apache Spark, a Dataset is a strongly typed, distributed collection of objects of one JVM type T that can be transformed in parallel with functional operations such as map and filter or with relational operations such as select and groupBy.[1] It combines the typed lambdas of the RDD API with the optimized execution engine of Spark SQL.[2] The Dataset API exists only in Scala and Java; a DataFrame is the untyped Dataset of Row objects, and it is the only form available in Python and R.[2]
How is a Dataset different from a DataFrame?
A DataFrame is a Dataset whose element type is the generic Row. In Scala the alias is declared in the org.apache.spark.sql package object as type DataFrame = Dataset[Row]; in Java the type is written Dataset<Row>.[3][2]
Both forms share one class, so both kinds of operation are available on any Dataset. The Spark documentation calls column-based operations such as select and groupBy "untyped transformations", and operations that take a lambda over T, such as map, filter and groupByKey, "typed transformations".[4][5] A DataFrame becomes a typed Dataset with as[T], which maps columns to fields by name.[1]
Like a DataFrame, a Dataset is lazy. It represents a logical plan, transformations return new Datasets, and an action such as count, show or a write runs the optimized physical plan on the executors.[1]
What does a Dataset need an encoder for?
Every Dataset needs an encoder for its type. The encoder maps T to Spark's internal type system, and Spark uses it to generate code that serializes objects into a compact binary form.[1] Spark can filter, sort and hash data in that form without turning the bytes back into objects, which is the difference from Java or Kryo serialization of RDD elements.[4]
In Scala, import spark.implicits._ supplies encoders for primitive types and case classes. In Java, encoders come from static methods on Encoders, such as Encoders.bean(Person.class).[6]
Example
This Scala example is adapted from the Spark SQL examples at v4.3.0. It reads a JSON file into a DataFrame, converts it to a typed Dataset of a case class, and applies a typed and an untyped transformation.[7]
import org.apache.spark.sql.{Dataset, SparkSession}
case class Person(name: String, age: Option[Long])
val spark = SparkSession.builder().getOrCreate()
import spark.implicits._
val people: Dataset[Person] =
spark.read.json("examples/src/main/resources/people.json").as[Person]
val adultNames: Dataset[String] =
people.filter(p => p.age.exists(_ >= 21)).map(_.name) // typed
val byAge = people.groupBy("age").count() // untyped, returns a DataFrame
byAge.show()
adultNames.show()
The same query in PySpark uses a DataFrame and column expressions, because Python has no typed Dataset:
from pyspark.sql import functions as F
people = spark.read.json("examples/src/main/resources/people.json")
adult_names = people.filter(F.col("age") >= 21).select("name")
adult_names.show()
Why does PySpark have no Dataset API?
The Dataset's value is compile-time type safety: the compiler checks that a lambda receives a Person and returns a String. When Spark 2.0 unified the two APIs, the migration guide noted that compile-time type safety is not a language feature of Python and R, so the Dataset concept does not apply to them and the DataFrame stays their main abstraction.[5] The programming guide adds that, because Python is dynamic, many of the Dataset's benefits are already available there; for example, a field of a Row can be read by name as row.columnName.[2]
PySpark code that needs per-row Python logic uses a user-defined function or mapInPandas, which maps an iterator of pandas DataFrames to another, rather than a typed map.[8]
Does Spark Connect support Datasets?
Yes. The Spark Connect Scala client supports most Scala APIs since Spark 3.5, including Dataset and KeyValueGroupedDataset. User-defined functions, which typed transformations rely on, work by default in the shell and need extra set-up in standalone applications.[9] See Spark Connect.
Is the Dataset API deprecated?
No. The Dataset class is annotated @Stable at the v4.3.0 tag, and the Spark 4.3.0 documentation presents it as a current API for Scala and Java.[1][2]
History
Spark 1.6.0 added the Dataset API (SPARK-9999) as a new API, similar to RDDs, for working with custom objects and lambda functions while using the Spark SQL execution engine.[10] Spark 2.0.0 unified DataFrame and Dataset in Scala and Java, so that DataFrame became a type alias for Dataset[Row], and introduced a typed Aggregator API for Datasets.[11]
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 Dataset.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 2.2 2.3 2.4 Spark SQL, DataFrames and Datasets Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ package.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 4.0 4.1 Getting Started (Spark SQL, Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 5.0 5.1 Migration Guide: SQL, Datasets and DataFrame (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Encoder.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SparkSQLExample.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Apache Arrow in PySpark (arrow_pandas.rst at v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Connect Overview (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 1.6.0. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Spark Release 2.0.0. Apache Software Foundation.Retrieved 2026-11-23.