Added: avro/site/publish/docs/1.11.3/index.xml
URL: 
http://svn.apache.org/viewvc/avro/site/publish/docs/1.11.3/index.xml?rev=1919887&view=auto
==============================================================================
--- avro/site/publish/docs/1.11.3/index.xml (added)
+++ avro/site/publish/docs/1.11.3/index.xml Wed Aug 14 13:30:06 2024
@@ -0,0 +1,1848 @@
+<rss version="2.0" 
xmlns:atom="http://www.w3.org/2005/Atom";><channel><title>Apache Avro – Apache 
Avro™ 1.11.3 
Documentation</title><link>/docs/1.11.3/</link><description>Recent content in 
Apache Avro™ 1.11.3 Documentation on Apache Avro</description><generator>Hugo 
-- gohugo.io</generator><language>en</language><lastBuildDate>Mon, 25 Oct 2021 
00:00:00 +0000</lastBuildDate><atom:link href="/docs/1.11.3/index.xml" 
rel="self" type="application/rss+xml"/><item><title>Docs: Getting Started 
(Java)</title><link>/docs/1.11.3/getting-started-java/</link><pubDate>Mon, 01 
Jan 0001 00:00:00 
+0000</pubDate><guid>/docs/1.11.3/getting-started-java/</guid><description>
+&lt;!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+https://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+&lt;p>This is a short guide for getting started with Apache Avro™ using 
Java. This guide only covers using Avro for data serialization; see Patrick 
Hunt&amp;rsquo;s &lt;a href="https://github.com/phunt/avro-rpc-quickstart";>Avro 
RPC Quick Start&lt;/a> for a good introduction to using Avro for RPC.&lt;/p>
+&lt;h2 id="download">Download&lt;/h2>
+&lt;p>Avro implementations for C, C++, C#, Java, PHP, Python, and Ruby can be 
downloaded from the &lt;a href="/project/download/">Apache Avro™ 
Download&lt;/a> page. This guide uses Avro 1.11.3, the latest version at the 
time of writing. For the examples in this guide, download avro-1.11.3.jar and 
avro-tools-1.11.3.jar.&lt;/p>
+&lt;p>Alternatively, if you are using Maven, add the following dependency to 
your POM:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-xml" data-lang="xml">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;dependency&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;groupId&amp;gt;&lt;/span>org.apache.avro&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/groupId&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;artifactId&amp;gt;&lt;/span>avro&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/artifactId&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;version&amp;gt;&lt;/span>1.11.3&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/version&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/dependency&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>As well as the Avro 
Maven plugin (for performing code generation):&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-xml" data-lang="xml">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;plugin&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;groupId&amp;gt;&lt;/span>org.apache.avro&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/groupId&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;artifactId&amp;gt;&lt;/span>avro-maven-plugin&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/artifactId&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;version&amp;gt;&lt;/span>1.11.3&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/version&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;executions&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;execution&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;phase&amp;gt;&lt;/span>generate-sources&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/phase&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;goals&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;goal&amp;gt;&lt;/span>schema&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/goal&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/goals&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;configuration&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;sourceDirectory&amp;gt;&lt;/span>${project.basedir}/src/main/avro/&lt;span
 
style="color:#204a87;font-weight:bold">&amp;lt;/sourceDirectory&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;outputDirectory&amp;gt;&lt;/span>${project.basedir}/src/main/java/&lt;span
 
style="color:#204a87;font-weight:bold">&amp;lt;/outputDirectory&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/configuration&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/execution&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/executions&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/plugin&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;plugin&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;groupId&amp;gt;&lt;/span>org.apache.maven.plugins&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/groupId&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;artifactId&amp;gt;&lt;/span>maven-compiler-plugin&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/artifactId&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;configuration&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;source&amp;gt;&lt;/span>1.8&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/source&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;target&amp;gt;&lt;/span>1.8&lt;span
 style="color:#204a87;font-weight:bold">&amp;lt;/target&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/configuration&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;lt;/plugin&amp;gt;&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You may also build the 
required Avro jars from source. Building Avro is beyond the scope of this 
guide; see the Build Documentation page in the wiki for more information.&lt;/p>
+&lt;h2 id="defining-a-schema">Defining a schema&lt;/h2>
+&lt;p>Avro schemas are defined using JSON. Schemas are composed of primitive 
types (null, boolean, int, long, float, double, bytes, and string) and complex 
types (record, enum, array, map, union, and fixed). You can learn more about 
Avro schemas and types from the specification, but for now let&amp;rsquo;s 
start with a simple schema example, user.avsc:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-json" data-lang="json">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;namespace&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;example.avro&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;record&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;User&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;fields&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#000;font-weight:bold">[&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;string&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">},&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#000;font-weight:bold">[&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;int&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;null&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">]},&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#000;font-weight:bold">[&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;string&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;null&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">]}&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">]&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This schema defines a 
record representing a hypothetical user. (Note that a schema file can only 
contain a single schema definition.) At minimum, a record definition must 
include its type (&amp;ldquo;type&amp;rdquo;: &amp;ldquo;record&amp;rdquo;), a 
name (&amp;ldquo;name&amp;rdquo;: &amp;ldquo;User&amp;rdquo;), and fields, in 
this case name, favorite_number, and favorite_color. We also define a namespace 
(&amp;ldquo;namespace&amp;rdquo;: &amp;ldquo;example.avro&amp;rdquo;), which 
together with the name attribute defines the &amp;ldquo;full name&amp;rdquo; of 
the schema (example.avro.User in this case).&lt;/p>
+&lt;p>Fields are defined via an array of objects, each of which defines a name 
and type (other attributes are optional, see the record specification for more 
details). The type attribute of a field is another schema object, which can be 
either a primitive or complex type. For example, the name field of our User 
schema is the primitive type string, whereas the favorite_number and 
favorite_color fields are both unions, represented by JSON arrays. unions are a 
complex type that can be any of the types listed in the array; e.g., 
favorite_number can either be an int or null, essentially making it an optional 
field.&lt;/p>
+&lt;h2 id="serializing-and-deserializing-with-code-generation">Serializing and 
deserializing with code generation&lt;/h2>
+&lt;h3 id="compiling-the-schema">Compiling the schema&lt;/h3>
+&lt;p>Code generation allows us to automatically create classes based on our 
previously-defined schema. Once we have defined the relevant classes, there is 
no need to use the schema directly in our programs. We use the avro-tools jar 
to generate code as follows:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-shell" data-lang="shell">&lt;span 
style="display:flex;">&lt;span>java -jar /path/to/avro-tools-1.11.3.jar compile 
schema &amp;lt;schema file&amp;gt; &amp;lt;destination&amp;gt;
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This will generate the 
appropriate source files in a package based on the schema&amp;rsquo;s namespace 
in the provided destination folder. For instance, to generate a User class in 
package example.avro from the schema defined above, run&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-shell" data-lang="shell">&lt;span 
style="display:flex;">&lt;span>java -jar /path/to/avro-tools-1.11.3.jar compile 
schema user.avsc .
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Note that if you using 
the Avro Maven plugin, there is no need to manually invoke the schema compiler; 
the plugin automatically performs code generation on any .avsc files present in 
the configured source directory.&lt;/p>
+&lt;h3 id="creating-users">Creating Users&lt;/h3>
+&lt;p>Now that we&amp;rsquo;ve completed the code generation, let&amp;rsquo;s 
create some Users, serialize them to a data file on disk, and then read back 
the file and deserialize the User objects.&lt;/p>
+&lt;p>First let&amp;rsquo;s create some Users and set their fields.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-java" data-lang="java">&lt;span 
style="display:flex;">&lt;span>&lt;span style="color:#000">User&lt;/span> 
&lt;span style="color:#000">user1&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">();&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">user1&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">setName&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;Alyssa&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">user1&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">setFavoriteNumber&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#0000cf;font-weight:bold">256&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Leave favorite color null
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Alternate constructor
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">User&lt;/span> &lt;span style="color:#000">user2&lt;/span> 
&lt;span style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;Ben&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">7&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;red&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Construct via builder
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">User&lt;/span> &lt;span style="color:#000">user3&lt;/span> 
&lt;span style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">newBuilder&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">()&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">setName&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;Charlie&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">)&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">setFavoriteColor&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;blue&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">)&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">setFavoriteNumber&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">null&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">)&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">build&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">();&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>As shown in this 
example, Avro objects can be created either by invoking a constructor directly 
or by using a builder. Unlike constructors, builders will automatically set any 
default values specified in the schema. Additionally, builders validate the 
data as it set, whereas objects constructed directly will not cause an error 
until the object is serialized. However, using constructors directly generally 
offers better performance, as builders create a copy of the datastructure 
before it is written.&lt;/p>
+&lt;p>Note that we do not set user1&amp;rsquo;s favorite color. Since that 
record is of type [&amp;ldquo;string&amp;rdquo;, &amp;ldquo;null&amp;rdquo;], 
we can either set it to a string or leave it null; it is essentially optional. 
Similarly, we set user3&amp;rsquo;s favorite number to null (using a builder 
requires setting all fields, even if they are null).&lt;/p>
+&lt;h3 id="serializing">Serializing&lt;/h3>
+&lt;p>Now let&amp;rsquo;s serialize our Users to disk.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-java" data-lang="java">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Serialize user1, user2 and user3 to 
disk
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">DatumWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">userDatumWriter&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">SpecificDatumWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">class&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">DataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">dataFileWriter&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">DataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">userDatumWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">create&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user1&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">getSchema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(),&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">File&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;users.avro&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">));&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">append&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user1&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">append&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user2&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">append&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user3&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">close&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">();&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We create a DatumWriter, 
which converts Java objects into an in-memory serialized format. The 
SpecificDatumWriter class is used with generated classes and extracts the 
schema from the specified generated type.&lt;/p>
+&lt;p>Next we create a DataFileWriter, which writes the serialized records, as 
well as the schema, to the file specified in the dataFileWriter.create call. We 
write our users to the file via calls to the dataFileWriter.append method. When 
we are done writing, we close the data file.&lt;/p>
+&lt;h3 id="deserializing">Deserializing&lt;/h3>
+&lt;p>Finally, let&amp;rsquo;s deserialize the data file we just 
created.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-java" data-lang="java">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Deserialize Users from disk
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">DatumReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">userDatumReader&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">SpecificDatumReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">class&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">DataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">dataFileReader&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">DataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">User&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">file&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#000">userDatumReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">User&lt;/span> &lt;span style="color:#000">user&lt;/span> 
&lt;span style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">null&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">while&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">dataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">hasNext&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">())&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">{&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Reuse user object by passing it to 
next(). This saves us from
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// allocating and garbage collecting 
many objects for files with
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// many items.
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">user&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">dataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">next&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">System&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">out&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">println&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#ce5c00;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This snippet will 
output:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-json" data-lang="json">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Alyssa&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">256&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">null&lt;/sp
 an>&lt;span style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Ben&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">7&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;red&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Charlie&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">null&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;blue&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Deserializing is very 
similar to serializing. We create a SpecificDatumReader, analogous to the 
SpecificDatumWriter we used in serialization, which converts in-memory 
serialized items into instances of our generated class, in this case User. We 
pass the DatumReader and the previously created File to a DataFileReader, 
analogous to the DataFileWriter, which reads both the schema used by the writer 
as well as the data from the file on disk. The data will be read using the 
writer&amp;rsquo;s schema included in the file and the schema provided by the 
reader, in this case the User class. The writer&amp;rsquo;s schema is needed to 
know the order in which fields were written, while the reader&amp;rsquo;s 
schema is needed to know what fields are expected and how to fill in default 
values for fields added since the file was written. If there are differences 
between the two schemas, they are resolved according to the Schema Resolution 
speci
 fication.&lt;/p>
+&lt;p>Next we use the DataFileReader to iterate through the serialized Users 
and print the deserialized object to stdout. Note how we perform the iteration: 
we create a single User object which we store the current deserialized user in, 
and pass this record object to every call of dataFileReader.next. This is a 
performance optimization that allows the DataFileReader to reuse the same User 
object rather than allocating a new User for every iteration, which can be very 
expensive in terms of object allocation and garbage collection if we 
deserialize a large data file. While this technique is the standard way to 
iterate through a data file, it&amp;rsquo;s also possible to use for (User user 
: dataFileReader) if performance is not a concern.&lt;/p>
+&lt;h3 id="compiling-and-running-the-example-code">Compiling and running the 
example code&lt;/h3>
+&lt;p>This example code is included as a Maven project in the 
examples/java-example directory in the Avro docs. From this directory, execute 
the following commands to build and run the example:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-shell" data-lang="shell">&lt;span 
style="display:flex;">&lt;span>$ mvn compile &lt;span 
style="color:#8f5902;font-style:italic"># includes code generation via Avro 
Maven plugin&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>$ mvn -q exec:java 
-Dexec.mainClass&lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span>example.SpecificMain
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h3 
id="beta-feature-generating-faster-code">Beta feature: Generating faster 
code&lt;/h3>
+&lt;p>In release 1.9.0, we introduced a new approach to generating code that 
speeds up decoding of objects by more than 10% and encoding by more than 30% 
(future performance enhancements are underway). To ensure a smooth introduction 
of this change into production systems, this feature is controlled by a feature 
flag, the system property org.apache.avro.specific.use_custom_coders. In this 
first release, this feature is off by default. To turn it on, set the system 
flag to true at runtime. In the sample above, for example, you could enable the 
fater coders as follows:&lt;/p>
+&lt;p>$ mvn -q exec:java -Dexec.mainClass=example.SpecificMain &lt;br>
+-Dorg.apache.avro.specific.use_custom_coders=true&lt;/p>
+&lt;p>Note that you do not have to recompile your Avro schema to have access 
to this feature. The feature is compiled and built into your code, and you turn 
it on and off at runtime using the feature flag. As a result, you can turn it 
on during testing, for example, and then off in production. Or you can turn it 
on in production, and quickly turn it off if something breaks.&lt;/p>
+&lt;p>We encourage the Avro community to exercise this new feature early to 
help build confidence. (For those paying one-demand for compute resources in 
the cloud, it can lead to meaningful cost savings.) As confidence builds, we 
will turn this feature on by default, and eventually eliminate the feature flag 
(and the old code).&lt;/p>
+&lt;h2 id="serializing-and-deserializing-without-code-generation">Serializing 
and deserializing without code generation&lt;/h2>
+&lt;p>Data in Avro is always stored with its corresponding schema, meaning we 
can always read a serialized item regardless of whether we know the schema 
ahead of time. This allows us to perform serialization and deserialization 
without code generation.&lt;/p>
+&lt;p>Let&amp;rsquo;s go over the same example as in the previous section, but 
without using code generation: we&amp;rsquo;ll create some users, serialize 
them to a data file on disk, and then read back the file and deserialize the 
users objects.&lt;/p>
+&lt;h3 id="creating-users-1">Creating users&lt;/h3>
+&lt;p>First, we use a Parser to read our schema definition and create a Schema 
object.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-java" data-lang="java">&lt;span 
style="display:flex;">&lt;span>&lt;span style="color:#000">Schema&lt;/span> 
&lt;span style="color:#000">schema&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">Schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">Parser&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">().&lt;/span>&lt;span 
style="color:#c4a000">parse&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">File&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;user.avsc&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c0
 0;font-weight:bold">));&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Using this schema, 
let&amp;rsquo;s create some users.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-java" data-lang="java">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000">GenericRecord&lt;/span> &lt;span 
style="color:#000">user1&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">GenericData&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">Record&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">user1&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">put&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Alyssa&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">user1&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">put&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">256&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Leave favorite color null
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">GenericRecord&lt;/span> &lt;span 
style="color:#000">user2&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">GenericData&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">Record&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">user2&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">put&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Ben&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">user2&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">put&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">7&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">user2&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">put&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;red&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Since we&amp;rsquo;re 
not using code generation, we use GenericRecords to represent users. 
GenericRecord uses the schema to verify that we only specify valid fields. If 
we try to set a non-existent field (e.g., 
user1.put(&amp;ldquo;favorite_animal&amp;rdquo;, &amp;ldquo;cat&amp;rdquo;)), 
we&amp;rsquo;ll get an AvroRuntimeException when we run the program.&lt;/p>
+&lt;p>Note that we do not set user1&amp;rsquo;s favorite color. Since that 
record is of type [&amp;ldquo;string&amp;rdquo;, &amp;ldquo;null&amp;rdquo;], 
we can either set it to a string or leave it null; it is essentially 
optional.&lt;/p>
+&lt;h3 id="serializing-1">Serializing&lt;/h3>
+&lt;p>Now that we&amp;rsquo;ve created our user objects, serializing and 
deserializing them is almost identical to the example above which uses code 
generation. The main difference is that we use generic instead of specific 
readers and writers.&lt;/p>
+&lt;p>First we&amp;rsquo;ll serialize our users to a data file on disk.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-java" data-lang="java">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Serialize user1 and user2 to disk
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">File&lt;/span> &lt;span style="color:#000">file&lt;/span> 
&lt;span style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">File&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;users.avro&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">DatumWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">datumWriter&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">GenericDatumWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">DataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">dataFileWriter&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">DataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">datumWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">create&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#000">file&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">append&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user1&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">append&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user2&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">dataFileWriter&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">close&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">();&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We create a DatumWriter, 
which converts Java objects into an in-memory serialized format. Since we are 
not using code generation, we create a GenericDatumWriter. It requires the 
schema both to determine how to write the GenericRecords and to verify that all 
non-nullable fields are present.&lt;/p>
+&lt;p>As in the code generation example, we also create a DataFileWriter, 
which writes the serialized records, as well as the schema, to the file 
specified in the dataFileWriter.create call. We write our users to the file via 
calls to the dataFileWriter.append method. When we are done writing, we close 
the data file.&lt;/p>
+&lt;h3 id="deserializing-1">Deserializing&lt;/h3>
+&lt;p>Finally, we&amp;rsquo;ll deserialize the data file we just 
created.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-java" data-lang="java">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Deserialize users from disk
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">DatumReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">datumReader&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">GenericDatumReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">DataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;&lt;/span> &lt;span 
style="color:#000">dataFileReader&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">new&lt;/span> &lt;span 
style="color:#000">DataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;lt;&lt;/span>&lt;span 
style="color:#000">GenericRecord&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">&amp;gt;(&lt;/span>&lt;span 
style="color:#000">file&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">,&lt;/span> &lt;span 
style="color:#000">datumReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">GenericRecord&lt;/span> &lt;span 
style="color:#000">user&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">null&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">;&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">while&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">dataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">hasNext&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">())&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">{&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// Reuse user object by passing it to 
next(). This saves us from
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// allocating and garbage collecting 
many objects for files with
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">// many items.
+&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#8f5902;font-style:italic">&lt;/span>&lt;span 
style="color:#000">user&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">dataFileReader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">next&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">System&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">out&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#c4a000">println&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">);&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This outputs:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-json" data-lang="json">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Alyssa&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">256&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">null&lt;/sp
 an>&lt;span style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Ben&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">7&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;red&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Deserializing is very 
similar to serializing. We create a GenericDatumReader, analogous to the 
GenericDatumWriter we used in serialization, which converts in-memory 
serialized items into GenericRecords. We pass the DatumReader and the 
previously created File to a DataFileReader, analogous to the DataFileWriter, 
which reads both the schema used by the writer as well as the data from the 
file on disk. The data will be read using the writer&amp;rsquo;s schema 
included in the file, and the reader&amp;rsquo;s schema provided to the 
GenericDatumReader. The writer&amp;rsquo;s schema is needed to know the order 
in which fields were written, while the reader&amp;rsquo;s schema is needed to 
know what fields are expected and how to fill in default values for fields 
added since the file was written. If there are differences between the two 
schemas, they are resolved according to the Schema Resolution 
specification.&lt;/p>
+&lt;p>Next, we use the DataFileReader to iterate through the serialized users 
and print the deserialized object to stdout. Note how we perform the iteration: 
we create a single GenericRecord object which we store the current deserialized 
user in, and pass this record object to every call of dataFileReader.next. This 
is a performance optimization that allows the DataFileReader to reuse the same 
record object rather than allocating a new GenericRecord for every iteration, 
which can be very expensive in terms of object allocation and garbage 
collection if we deserialize a large data file. While this technique is the 
standard way to iterate through a data file, it&amp;rsquo;s also possible to 
use for (GenericRecord user : dataFileReader) if performance is not a 
concern.&lt;/p>
+&lt;h3 id="compiling-and-running-the-example-code-1">Compiling and running the 
example code&lt;/h3>
+&lt;p>This example code is included as a Maven project in the 
examples/java-example directory in the Avro docs. From this directory, execute 
the following commands to build and run the example:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-shell" data-lang="shell">&lt;span 
style="display:flex;">&lt;span>$ mvn compile
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>$ mvn -q exec:java 
-Dexec.mainClass&lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span>example.GenericMain
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Docs:
 Getting Started 
(Python)</title><link>/docs/1.11.3/getting-started-python/</link><pubDate>Mon, 
01 Jan 0001 00:00:00 
+0000</pubDate><guid>/docs/1.11.3/getting-started-python/</guid><description>
+&lt;!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+https://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+&lt;p>This is a short guide for getting started with Apache Avro™ using 
Python. This guide only covers using Avro for data serialization; see Patrick 
Hunt&amp;rsquo;s Avro RPC Quick Start for a good introduction to using Avro for 
RPC.&lt;/p>
+&lt;h2 id="notice-for-python-3-users">Notice for Python 3 users&lt;/h2>
+&lt;p>A package called &amp;ldquo;avro-python3&amp;rdquo; had been provided to 
support Python 3 previously, but the codebase was consolidated into the 
&amp;ldquo;avro&amp;rdquo; package and that supports both Python 2 and 3 now. 
The avro-python3 package will be removed in the near future, so users should 
use the &amp;ldquo;avro&amp;rdquo; package instead. They are mostly API 
compatible, but there&amp;rsquo;s a few minor difference (e.g., function name 
capitalization, such as avro.schema.Parse vs avro.schema.parse).&lt;/p>
+&lt;h2 id="download">Download&lt;/h2>
+&lt;p>For Python, the easiest way to get started is to install it from PyPI. 
Python&amp;rsquo;s Avro API is available over PyPi.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-shell" data-lang="shell">&lt;span 
style="display:flex;">&lt;span>$ python3 -m pip install avro
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The official releases of 
the Avro implementations for C, C++, C#, Java, PHP, Python, and Ruby can be 
downloaded from the Apache Avro™ Releases page. This guide uses Avro 1.11.3, 
the latest version at the time of writing. Download and unzip 
avro-1.11.3.tar.gz, and install via python setup.py (this will probably require 
root privileges). Ensure that you can import avro from a Python prompt.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-shell" data-lang="shell">&lt;span 
style="display:flex;">&lt;span>$ tar xvf avro-1.11.3.tar.gz
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>$ &lt;span 
style="color:#204a87">cd&lt;/span> avro-1.11.3
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>$ python setup.py 
install
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>$ python
+&lt;/span>&lt;/span>&lt;span 
style="display:flex;">&lt;span>&amp;gt;&amp;gt;&amp;gt; import avro &lt;span 
style="color:#8f5902;font-style:italic"># should not raise ImportError&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Alternatively, you may 
build the Avro Python library from source. From your the root Avro directory, 
run the commands&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-shell" data-lang="shell">&lt;span 
style="display:flex;">&lt;span>$ &lt;span style="color:#204a87">cd&lt;/span> 
lang/py/
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>$ python3 -m pip 
install -e .
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>$ python
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 
id="defining-a-schema">Defining a schema&lt;/h2>
+&lt;p>Avro schemas are defined using JSON. Schemas are composed of primitive 
types (null, boolean, int, long, float, double, bytes, and string) and complex 
types (record, enum, array, map, union, and fixed). You can learn more about 
Avro schemas and types from the specification, but for now let&amp;rsquo;s 
start with a simple schema example, user.avsc:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-json" data-lang="json">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;namespace&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;example.avro&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;record&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;User&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;fields&amp;#34;&lt;/span>&lt;span
 style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#000;font-weight:bold">[&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;string&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">},&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#000;font-weight:bold">[&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;int&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;null&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">]},&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#000;font-weight:bold">[&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;string&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;null&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">]}&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#000;font-weight:bold">]&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This schema defines a 
record representing a hypothetical user. (Note that a schema file can only 
contain a single schema definition.) At minimum, a record definition must 
include its type (&amp;ldquo;type&amp;rdquo;: &amp;ldquo;record&amp;rdquo;), a 
name (&amp;ldquo;name&amp;rdquo;: &amp;ldquo;User&amp;rdquo;), and fields, in 
this case name, favorite_number, and favorite_color. We also define a namespace 
(&amp;ldquo;namespace&amp;rdquo;: &amp;ldquo;example.avro&amp;rdquo;), which 
together with the name attribute defines the &amp;ldquo;full name&amp;rdquo; of 
the schema (example.avro.User in this case).&lt;/p>
+&lt;p>Fields are defined via an array of objects, each of which defines a name 
and type (other attributes are optional, see the record specification for more 
details). The type attribute of a field is another schema object, which can be 
either a primitive or complex type. For example, the name field of our User 
schema is the primitive type string, whereas the favorite_number and 
favorite_color fields are both unions, represented by JSON arrays. unions are a 
complex type that can be any of the types listed in the array; e.g., 
favorite_number can either be an int or null, essentially making it an optional 
field.&lt;/p>
+&lt;h2 id="serializing-and-deserializing-without-code-generation">Serializing 
and deserializing without code generation&lt;/h2>
+&lt;p>Data in Avro is always stored with its corresponding schema, meaning we 
can always read a serialized item, regardless of whether we know the schema 
ahead of time. This allows us to perform serialization and deserialization 
without code generation. Note that the Avro Python library does not support 
code generation.&lt;/p>
+&lt;p>Try running the following code snippet, which serializes two users to a 
data file on disk, and then reads back and deserializes the data file:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-python" data-lang="python">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">import&lt;/span> &lt;span 
style="color:#000">avro.schema&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">from&lt;/span> &lt;span 
style="color:#000">avro.datafile&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">import&lt;/span> &lt;span 
style="color:#000">DataFileReader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#000">DataFileWriter&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">from&lt;/span> &lt;span 
style="color:#000">avro.io&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">import&lt;/span> &lt;span 
style="color:#000">DatumReader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#000">DatumWriter&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">schema&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">avro&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">parse&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87">open&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;user.avsc&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;rb&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">)&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">read&lt;/span>&lt;span 
style="color:#000;font-weight:bold">())&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">writer&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">DataFileWriter&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87">open&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;users.avro&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;wb&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">),&lt;/span> &lt;span 
style="color:#000">DatumWriter&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(),&lt;/span> &lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#000;font-weight:bold">)&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">writer&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">append&lt;/span>&lt;span 
style="color:#000;font-weight:bold">({&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Alyssa&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">256&lt;/span>&lt;span 
style="color:#000;font-weight:bold">})&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">writer&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">append&lt;/span>&lt;span 
style="color:#000;font-weight:bold">({&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Ben&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">7&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;red&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">})&lt;/spa
 n>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">writer&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">close&lt;/span>&lt;span 
style="color:#000;font-weight:bold">()&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">reader&lt;/span> &lt;span 
style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">DataFileReader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87">open&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;users.avro&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;rb&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">),&lt;/span> &lt;span 
style="color:#000">DatumReader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">())&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">for&lt;/span> &lt;span 
style="color:#000">user&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">in&lt;/span> &lt;span 
style="color:#000">reader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87">print&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user&lt;/span>&lt;span 
style="color:#000;font-weight:bold">)&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">reader&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">close&lt;/span>&lt;span 
style="color:#000;font-weight:bold">()&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This outputs:&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-json" data-lang="json">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#a40000">u&amp;#39;favorite_color&amp;#39;:&lt;/span> &lt;span 
style="color:#a40000">None,&lt;/span> &lt;span 
style="color:#a40000">u&amp;#39;favorite_number&amp;#39;:&lt;/span> &lt;span 
style="color:#a40000">256,&lt;/span> &lt;span 
style="color:#a40000">u&amp;#39;name&amp;#39;:&lt;/span> &lt;span 
style="color:#a40000">u&amp;#39;Alyssa&amp;#39;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#a40000">u&amp;#39;favorite_color&amp;#39;:&lt;/span> &lt;span 
style="color:#a40000">u&amp;#39;red&amp;#39;,&lt;/span> &lt;span 
style="color:#a40000">u&amp;#39;favorite_number&amp;#39;:&lt;/span> &lt;span 
style="color:#a40000">7,&lt;/span> &lt;span 
style="color:#a40000">u&amp;#39;name&amp;#39;:&lt;/span> &lt;span 
style="color:#a40000">u&amp;#39;Ben&amp;#39;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">}&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Do make sure that you 
open your files in binary mode (i.e. using the modes wb or rb respectively). 
Otherwise you might generate corrupt files due to automatic replacement of 
newline characters with the platform-specific representations.&lt;/p>
+&lt;p>Let&amp;rsquo;s take a closer look at what&amp;rsquo;s going on 
here.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-python" data-lang="python">&lt;span 
style="display:flex;">&lt;span>&lt;span style="color:#000">schema&lt;/span> 
&lt;span style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">avro&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">parse&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87">open&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;user.avsc&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;rb&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">)&lt;/span>&lt;span style="color:#ce5c00;fo
 nt-weight:bold">.&lt;/span>&lt;span style="color:#000">read&lt;/span>&lt;span 
style="color:#000;font-weight:bold">())&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>avro.schema.parse takes 
a string containing a JSON schema definition as input and outputs a 
avro.schema.Schema object (specifically a subclass of Schema, in this case 
RecordSchema). We&amp;rsquo;re passing in the contents of our user.avsc schema 
file here.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-python" data-lang="python">&lt;span 
style="display:flex;">&lt;span>&lt;span style="color:#000">writer&lt;/span> 
&lt;span style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">DataFileWriter&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87">open&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;users.avro&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;wb&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">),&lt;/span> &lt;span 
style="color:#000">DatumWriter&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(),&lt;/span> &lt;span 
style="color:#000">schema&lt;/span>&lt;span 
style="color:#000;font-weight:bold">)&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We create a 
DataFileWriter, which we&amp;rsquo;ll use to write serialized items to a data 
file on disk. The DataFileWriter constructor takes three arguments:&lt;/p>
+&lt;ul>
+&lt;li>The file we&amp;rsquo;ll serialize to&lt;/li>
+&lt;li>A DatumWriter, which is responsible for actually serializing the items 
to Avro&amp;rsquo;s binary format (DatumWriters can be used separately from 
DataFileWriters, e.g., to perform IPC with Avro).&lt;/li>
+&lt;li>The schema we&amp;rsquo;re using. The DataFileWriter needs the schema 
both to write the schema to the data file, and to verify that the items we 
write are valid items and write the appropriate fields.&lt;/li>
+&lt;/ul>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-python" data-lang="python">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000">writer&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">append&lt;/span>&lt;span 
style="color:#000;font-weight:bold">({&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Alyssa&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">256&lt;/span>&lt;span 
style="color:#000;font-weight:bold">})&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span 
style="color:#000">writer&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">.&lt;/span>&lt;span 
style="color:#000">append&lt;/span>&lt;span 
style="color:#000;font-weight:bold">({&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;name&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;Ben&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_number&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#0000cf;font-weight:bold">7&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;favorite_color&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;red&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">})&lt;/spa
 n>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We use 
DataFileWriter.append to add items to our data file. Avro records are 
represented as Python dicts. Since the field favorite_color has type 
[&amp;ldquo;string&amp;rdquo;, &amp;ldquo;null&amp;rdquo;], we are not required 
to specify this field, as shown in the first append. Were we to omit the 
required name field, an exception would be raised. Any extra entries not 
corresponding to a field are present in the dict are ignored.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-python" data-lang="python">&lt;span 
style="display:flex;">&lt;span>&lt;span style="color:#000">reader&lt;/span> 
&lt;span style="color:#ce5c00;font-weight:bold">=&lt;/span> &lt;span 
style="color:#000">DataFileReader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#204a87">open&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;users.avro&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;rb&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">),&lt;/span> &lt;span 
style="color:#000">DatumReader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">())&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>We open the file again, 
this time for reading back from disk. We use a DataFileReader and DatumReader 
analagous to the DataFileWriter and DatumWriter above.&lt;/p>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-python" data-lang="python">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#204a87;font-weight:bold">for&lt;/span> &lt;span 
style="color:#000">user&lt;/span> &lt;span 
style="color:#204a87;font-weight:bold">in&lt;/span> &lt;span 
style="color:#000">reader&lt;/span>&lt;span 
style="color:#000;font-weight:bold">:&lt;/span>
+&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span 
style="color:#204a87">print&lt;/span>&lt;span 
style="color:#000;font-weight:bold">(&lt;/span>&lt;span 
style="color:#000">user&lt;/span>&lt;span 
style="color:#000;font-weight:bold">)&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The DataFileReader is an 
iterator that returns dicts corresponding to the serialized 
items.&lt;/p></description></item><item><title>Docs: 
Specification</title><link>/docs/1.11.3/specification/</link><pubDate>Mon, 25 
Oct 2021 00:00:00 
+0000</pubDate><guid>/docs/1.11.3/specification/</guid><description>
+&lt;!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+https://www.apache.org/licenses/LICENSE-2.0
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+&lt;h2 id="introduction">Introduction&lt;/h2>
+&lt;p>This document defines Apache Avro. It is intended to be the 
authoritative specification. Implementations of Avro must adhere to this 
document.&lt;/p>
+&lt;h2 id="schema-declaration">Schema Declaration&lt;/h2>
+&lt;p>A Schema is represented in &lt;a 
href="https://www.json.org/";>JSON&lt;/a> by one of:&lt;/p>
+&lt;ul>
+&lt;li>A JSON string, naming a defined type.&lt;/li>
+&lt;li>A JSON object, of the form:&lt;/li>
+&lt;/ul>
+&lt;div class="highlight">&lt;pre tabindex="0" 
style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code
 class="language-js" data-lang="js">&lt;span 
style="display:flex;">&lt;span>&lt;span 
style="color:#000;font-weight:bold">{&lt;/span>&lt;span 
style="color:#4e9a06">&amp;#34;type&amp;#34;&lt;/span>&lt;span 
style="color:#ce5c00;font-weight:bold">:&lt;/span> &lt;span 
style="color:#4e9a06">&amp;#34;typeName&amp;#34;&lt;/span>&lt;span 
style="color:#000;font-weight:bold">,&lt;/span> &lt;span 
style="color:#000;font-weight:bold">...&lt;/span>&lt;span 
style="color:#000">attributes&lt;/span>&lt;span 
style="color:#000;font-weight:bold">...}&lt;/span>
+&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>where 
&lt;em>typeName&lt;/em> is either a primitive or derived type name, as defined 
below. Attributes not defined in this document are permitted as metadata, but 
must not affect the format of serialized data.&lt;/p>
+&lt;ul>
+&lt;li>A JSON array, representing a union of embedded types.&lt;/li>
+&lt;/ul>
+&lt;h2 id="primitive-types">Primitive Types&lt;/h2>
+&lt;p>The set of primitive type names is:&lt;/p>
+&lt;ul>
+&lt;li>&lt;em>null&lt;/em>: no value&lt;/li>
+&lt;li>&lt;em>boolean&lt;/em>: a binary value&lt;/li>
+&lt;li>&lt;em>int&lt;/em>: 32-bit signed integer&lt;/li>
+&lt;li>&lt;em>long&lt;/em>: 64-bit signed integer&lt;/li>
+&lt;li>&lt;em>float&lt;/em>: single precision (32-bit) IEEE 754 floating-point 
number&lt;/li>
+&lt;li>&lt;em>double&lt;/em>: double precision (64-bit) IEEE 754 
floating-point number&lt;/li>
+&lt;li>&lt;em>bytes&lt;/em>: sequence of 8-bit unsigned bytes&lt;/li>
+&lt;li>&lt;em>string&lt;/em>: unicode character sequence&lt;/li>
+&lt;/ul>
+&lt;p>Primitive types have no specified attributes.&lt;/p>
+&lt;p>Primitive type names are also defined type names. Thus, for example, the 
schema &amp;ldquo;string&amp;rdquo; is equivalent to:&lt;/p>

[... 1536 lines stripped ...]


Reply via email to