<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Encoder_(Spark)</id>
	<title>Encoder (Spark) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Encoder_(Spark)"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Encoder_(Spark)&amp;action=history"/>
	<updated>2026-12-06T00:07:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Encoder_(Spark)&amp;diff=144&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Encoder_(Spark)&amp;diff=144&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:40Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Encoder (Spark)&lt;br /&gt;
| also_known_as   = &amp;lt;code&amp;gt;Encoder[T]&amp;lt;/code&amp;gt;, Dataset encoder&lt;br /&gt;
| field           = Serialization in distributed data processing&lt;br /&gt;
| introduced      = Spark 1.6.0&lt;br /&gt;
| key_parts       = A schema (&amp;lt;code&amp;gt;StructType&amp;lt;/code&amp;gt;), a class tag, generated serializer and deserializer code&lt;br /&gt;
| implementations = [[Apache Spark]] (Scala and Java APIs)&lt;br /&gt;
| related         = [[Dataset (Spark)]], [[DataFrame]], [[Project Tungsten]], [[Spark Connect]], [[Apache Arrow]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the v4.3.0 tag of apache/spark (Encoder.scala, Encoders.scala, AgnosticEncoder.scala, ExpressionEncoder.scala, Dataset.scala, sql/connect/common/.../client/arrow/ArrowSerializer.scala and ArrowDeserializer.scala, docs/sql-getting-started.md, docs/sql-programming-guide.md)}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]], an &amp;#039;&amp;#039;&amp;#039;encoder&amp;#039;&amp;#039;&amp;#039; is the object that converts a JVM object of type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; to and from Spark SQL&amp;#039;s internal row representation; every typed [[Dataset (Spark)|Dataset]] needs one for its element type.&amp;lt;ref name=&amp;quot;encoder&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/Encoder.scala |title=Encoder.scala (tag v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The encoder tells Spark the schema of &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; and lets it generate code at runtime that serializes objects into a compact binary format.&amp;lt;ref name=&amp;quot;dataset&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/Dataset.scala |title=Dataset.scala (tag v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Encoders are part of the Scala and Java APIs; PySpark has no Dataset API and so no encoders.&amp;lt;ref name=&amp;quot;guide&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-programming-guide.html |title=Spark SQL, DataFrames and Datasets Guide (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What does an encoder do? ==&lt;br /&gt;
An encoder maps the type &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; to Spark&amp;#039;s internal type system. For a class &amp;lt;code&amp;gt;Person&amp;lt;/code&amp;gt; with a string field &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and an integer field &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;, the encoder tells Spark to generate code that writes each &amp;lt;code&amp;gt;Person&amp;lt;/code&amp;gt; into a binary structure, which usually takes less memory than the objects and is laid out for efficient processing.&amp;lt;ref name=&amp;quot;dataset&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The public interface is small. &amp;lt;code&amp;gt;trait Encoder[T]&amp;lt;/code&amp;gt;, available since Spark 1.6.0, has two members: &amp;lt;code&amp;gt;schema&amp;lt;/code&amp;gt;, the &amp;lt;code&amp;gt;StructType&amp;lt;/code&amp;gt; that a &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt; becomes as a row, and &amp;lt;code&amp;gt;clsTag&amp;lt;/code&amp;gt;, a &amp;lt;code&amp;gt;ClassTag&amp;lt;/code&amp;gt; used to build arrays of &amp;lt;code&amp;gt;T&amp;lt;/code&amp;gt;. Encoders must be thread-safe.&amp;lt;ref name=&amp;quot;encoder&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How is an encoder different from Java or Kryo serialization? ==&lt;br /&gt;
[[RDD|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.&amp;lt;ref name=&amp;quot;started&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-getting-started.html |title=Getting Started (Spark SQL, Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Because the encoder also gives Spark a schema, a Dataset of case classes shows up with ordinary named columns that Spark SQL can query.&amp;lt;ref name=&amp;quot;encoder&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do you get an encoder? ==&lt;br /&gt;
In Scala, encoders are usually provided implicitly by &amp;lt;code&amp;gt;import spark.implicits._&amp;lt;/code&amp;gt;, which covers primitive types such as &amp;lt;code&amp;gt;Int&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; and product types such as case classes. If no encoder is found, compilation fails with the message &amp;quot;Unable to find encoder for type T&amp;quot;, which points to that import.&amp;lt;ref name=&amp;quot;encoder&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;scala&amp;quot;&amp;gt;&lt;br /&gt;
import org.apache.spark.sql.{Encoder, Encoders}&lt;br /&gt;
&lt;br /&gt;
case class Order(id: Long, customer: String, amount: Double)&lt;br /&gt;
&lt;br /&gt;
import spark.implicits._&lt;br /&gt;
val orders = Seq(Order(1L, &amp;quot;acme&amp;quot;, 120.0), Order(2L, &amp;quot;globex&amp;quot;, 75.5)).toDS()&lt;br /&gt;
orders.printSchema()     // id: long, customer: string, amount: double&lt;br /&gt;
&lt;br /&gt;
val enc: Encoder[Order] = Encoders.product[Order]&lt;br /&gt;
println(enc.schema.simpleString)   // struct&amp;lt;id:bigint,customer:string,amount:double&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Java, encoders are passed explicitly, taken from static methods on &amp;lt;code&amp;gt;Encoders&amp;lt;/code&amp;gt;, for example &amp;lt;code&amp;gt;spark.createDataset(data, Encoders.STRING())&amp;lt;/code&amp;gt;. Encoders can be combined into tuples with &amp;lt;code&amp;gt;Encoders.tuple&amp;lt;/code&amp;gt; or derived from a Java Bean with &amp;lt;code&amp;gt;Encoders.bean(MyClass.class)&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;encoder&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Which encoders does the Encoders object provide? ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Method !! Encodes !! Since&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;BOOLEAN&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;INT&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;LONG&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DOUBLE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;STRING&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DECIMAL&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;DATE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;TIMESTAMP&amp;lt;/code&amp;gt; and other primitives || Boxed Java types and &amp;lt;code&amp;gt;java.sql&amp;lt;/code&amp;gt; date and time types, one column each || 1.6.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;BINARY&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;Array[Byte]&amp;lt;/code&amp;gt; || 1.6.1&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;LOCALDATE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;INSTANT&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;java.time.LocalDate&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;java.time.Instant&amp;lt;/code&amp;gt; || 3.0.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;DURATION&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;PERIOD&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;java.time.Duration&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;java.time.Period&amp;lt;/code&amp;gt; (interval types) || 3.2.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;LOCALDATETIME&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;java.time.LocalDateTime&amp;lt;/code&amp;gt; || 3.4.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;row(schema)&amp;lt;/code&amp;gt; || A generic &amp;lt;code&amp;gt;Row&amp;lt;/code&amp;gt; with the given schema || 3.5.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;CHAR(n)&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;VARCHAR(n)&amp;lt;/code&amp;gt; || Strings with a length limit || 4.0.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;LOCALTIME&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GEOMETRY&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GEOGRAPHY&amp;lt;/code&amp;gt; || &amp;lt;code&amp;gt;java.time.LocalTime&amp;lt;/code&amp;gt; and geospatial types || 4.1.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;bean(Class)&amp;lt;/code&amp;gt; || A Java Bean, field by field || 1.6.0&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;kryo&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;javaSerialization&amp;lt;/code&amp;gt; || Any public type, as a single binary column || 1.6.0&lt;br /&gt;
|}&lt;br /&gt;
The methods and versions are taken from the &amp;lt;code&amp;gt;@since&amp;lt;/code&amp;gt; tags in &amp;lt;code&amp;gt;Encoders.scala&amp;lt;/code&amp;gt; at v4.3.0.&amp;lt;ref name=&amp;quot;encoders&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/Encoders.scala |title=Encoders.scala (tag v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;kryo&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;javaSerialization&amp;lt;/code&amp;gt; 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.&amp;lt;ref name=&amp;quot;encoders&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How do encoders work inside Spark? ==&lt;br /&gt;
At v4.3.0 the encoders returned by &amp;lt;code&amp;gt;Encoders&amp;lt;/code&amp;gt;, such as &amp;lt;code&amp;gt;StringEncoder&amp;lt;/code&amp;gt; behind &amp;lt;code&amp;gt;Encoders.STRING&amp;lt;/code&amp;gt;, are &amp;lt;code&amp;gt;AgnosticEncoder&amp;lt;/code&amp;gt; instances.&amp;lt;ref name=&amp;quot;encoders&amp;quot; /&amp;gt; An &amp;lt;code&amp;gt;AgnosticEncoder&amp;lt;/code&amp;gt; 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.&amp;lt;ref name=&amp;quot;agnostic&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/api/src/main/scala/org/apache/spark/sql/catalyst/encoders/AgnosticEncoder.scala |title=AgnosticEncoder.scala (tag v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In classic Spark, the engine builds an &amp;lt;code&amp;gt;ExpressionEncoder&amp;lt;/code&amp;gt; from it: a serializer and a deserializer written as Catalyst expressions, which convert an object to an &amp;lt;code&amp;gt;InternalRow&amp;lt;/code&amp;gt; and back and take part in code generation.&amp;lt;ref name=&amp;quot;expr&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/encoders/ExpressionEncoder.scala |title=ExpressionEncoder.scala (tag v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In the [[Spark Connect]] Scala client, the same &amp;lt;code&amp;gt;AgnosticEncoder&amp;lt;/code&amp;gt; drives an &amp;lt;code&amp;gt;ArrowSerializer&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ArrowDeserializer&amp;lt;/code&amp;gt; that convert user objects to and from [[Apache Arrow]] batches.&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/connect/common/src/main/scala/org/apache/spark/sql/connect/client/arrow/ArrowSerializer.scala |title=ArrowSerializer.scala (tag v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/connect/common/src/main/scala/org/apache/spark/sql/connect/client/arrow/ArrowDeserializer.scala |title=ArrowDeserializer.scala (tag v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Why does PySpark have no encoders? ==&lt;br /&gt;
PySpark has no typed Dataset, so it has nothing for an encoder to describe. Spark&amp;#039;s documentation explains that compile-time type safety is not a feature of Python, and that the DataFrame, a Dataset of &amp;lt;code&amp;gt;Row&amp;lt;/code&amp;gt;, is the main abstraction there.&amp;lt;ref name=&amp;quot;guide&amp;quot; /&amp;gt;&amp;lt;ref&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/sql-migration-guide.html |title=Migration Guide: SQL, Datasets and DataFrame (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; A PySpark [[DataFrame]] carries a schema of named, typed columns instead; Python logic over rows runs in [[User-defined function (Spark)|user-defined functions]].&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Dataset (Spark)]]&lt;br /&gt;
* [[DataFrame]]&lt;br /&gt;
* [[RDD]]&lt;br /&gt;
* [[Spark Connect]]&lt;br /&gt;
* [[Project Tungsten]]&lt;br /&gt;
* [[User-defined function (Spark)]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>