Encoder (Spark)
| Also known as | Encoder[T], Dataset encoder
|
|---|---|
| Field | Serialization in distributed data processing |
| Introduced | Spark 1.6.0 |
| Key parts | A schema (StructType), a class tag, generated serializer and deserializer code
|
| Open source implementations | Apache Spark (Scala and Java APIs) |
| Related | Dataset (Spark), DataFrame, Project Tungsten, Spark Connect, Apache Arrow |
In Apache Spark, an encoder is the object that converts a JVM object of type T to and from Spark SQL's internal row representation; every typed Dataset needs one for its element type.[1] The encoder tells Spark the schema of T and lets it generate code at runtime that serializes objects into a compact binary format.[2] Encoders are part of the Scala and Java APIs; PySpark has no Dataset API and so no encoders.[3]
What does an encoder do?
An encoder maps the type T to Spark's internal type system. For a class Person with a string field name and an integer field age, the encoder tells Spark to generate code that writes each Person into a binary structure, which usually takes less memory than the objects and is laid out for efficient processing.[2]
The public interface is small. trait Encoder[T], available since Spark 1.6.0, has two members: schema, the StructType that a T becomes as a row, and clsTag, a ClassTag used to build arrays of T. Encoders must be thread-safe.[1]
How is an encoder different from Java or Kryo serialization?
RDDs serialize their elements with Java serialization or Kryo. Both encoders and those serializers turn objects into bytes, but encoders are generated dynamically and use a format that lets Spark filter, sort and hash the data without deserializing it back into objects.[4] Because the encoder also gives Spark a schema, a Dataset of case classes shows up with ordinary named columns that Spark SQL can query.[1]
How do you get an encoder?
In Scala, encoders are usually provided implicitly by import spark.implicits._, which covers primitive types such as Int and String and product types such as case classes. If no encoder is found, compilation fails with the message "Unable to find encoder for type T", which points to that import.[1]
import org.apache.spark.sql.{Encoder, Encoders}
case class Order(id: Long, customer: String, amount: Double)
import spark.implicits._
val orders = Seq(Order(1L, "acme", 120.0), Order(2L, "globex", 75.5)).toDS()
orders.printSchema() // id: long, customer: string, amount: double
val enc: Encoder[Order] = Encoders.product[Order]
println(enc.schema.simpleString) // struct<id:bigint,customer:string,amount:double>
In Java, encoders are passed explicitly, taken from static methods on Encoders, for example spark.createDataset(data, Encoders.STRING()). Encoders can be combined into tuples with Encoders.tuple or derived from a Java Bean with Encoders.bean(MyClass.class).[1]
Which encoders does the Encoders object provide?
| Method | Encodes | Since |
|---|---|---|
BOOLEAN, INT, LONG, DOUBLE, STRING, DECIMAL, DATE, TIMESTAMP and other primitives |
Boxed Java types and java.sql date and time types, one column each |
1.6.0 |
BINARY |
Array[Byte] |
1.6.1 |
LOCALDATE, INSTANT |
java.time.LocalDate, java.time.Instant |
3.0.0 |
DURATION, PERIOD |
java.time.Duration, java.time.Period (interval types) |
3.2.0 |
LOCALDATETIME |
java.time.LocalDateTime |
3.4.0 |
row(schema) |
A generic Row with the given schema |
3.5.0 |
CHAR(n), VARCHAR(n) |
Strings with a length limit | 4.0.0 |
LOCALTIME, GEOMETRY, GEOGRAPHY |
java.time.LocalTime and geospatial types |
4.1.0 |
bean(Class) |
A Java Bean, field by field | 1.6.0 |
kryo, javaSerialization |
Any public type, as a single binary column | 1.6.0 |
The methods and versions are taken from the @since tags in Encoders.scala at v4.3.0.[5]
The kryo and javaSerialization encoders store the whole object as one binary field, so Spark cannot see or prune its fields. The source describes Java serialization as extremely inefficient and to be used only as a last resort.[5]
How do encoders work inside Spark?
At v4.3.0 the encoders returned by Encoders, such as StringEncoder behind Encoders.STRING, are AgnosticEncoder instances.[5] An AgnosticEncoder holds all the information about a type, such as its fields, options, arrays and maps, without fixing how the conversion is done, so different implementations can be generated from it.[6]
In classic Spark, the engine builds an ExpressionEncoder from it: a serializer and a deserializer written as Catalyst expressions, which convert an object to an InternalRow and back and take part in code generation.[7] In the Spark Connect Scala client, the same AgnosticEncoder drives an ArrowSerializer and ArrowDeserializer that convert user objects to and from Apache Arrow batches.[8][9]
Why does PySpark have no encoders?
PySpark has no typed Dataset, so it has nothing for an encoder to describe. Spark's documentation explains that compile-time type safety is not a feature of Python, and that the DataFrame, a Dataset of Row, is the main abstraction there.[3][10] A PySpark DataFrame carries a schema of named, typed columns instead; Python logic over rows runs in user-defined functions.
See also
References
- ↑ 1.0 1.1 1.2 1.3 1.4 Encoder.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 Dataset.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 3.0 3.1 Spark SQL, DataFrames and Datasets Guide (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Getting Started (Spark SQL, Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 5.0 5.1 5.2 Encoders.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ AgnosticEncoder.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ ExpressionEncoder.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ ArrowSerializer.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ ArrowDeserializer.scala (tag v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Migration Guide: SQL, Datasets and DataFrame (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.