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> +<!-- +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. +--> +<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&rsquo;s <a href="https://github.com/phunt/avro-rpc-quickstart">Avro RPC Quick Start</a> for a good introduction to using Avro for RPC.</p> +<h2 id="download">Download</h2> +<p>Avro implementations for C, C++, C#, Java, PHP, Python, and Ruby can be downloaded from the <a href="/project/download/">Apache Avro⢠Download</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.</p> +<p>Alternatively, if you are using Maven, add the following dependency to your POM:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">&lt;dependency&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;groupId&gt;</span>org.apache.avro<span style="color:#204a87;font-weight:bold">&lt;/groupId&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;artifactId&gt;</span>avro<span style="color:#204a87;font-weight:bold">&lt;/artifactId&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;version&gt;</span>1.11.3<span style="color:#204a87;font-weight:bold">&lt;/version&gt;</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">&lt;/dependency&gt;</span> +</span></span></code></pre></div><p>As well as the Avro Maven plugin (for performing code generation):</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">&lt;plugin&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;groupId&gt;</span>org.apache.avro<span style="color:#204a87;font-weight:bold">&lt;/groupId&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;artifactId&gt;</span>avro-maven-plugin<span style="color:#204a87;font-weight:bold">&lt;/artifactId&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;version&gt;</span>1.11.3<span style="color:#204a87;font-weight:bold">&lt;/version&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;executions&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;execution&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;phase&gt;</span>generate-sources<span style="color:#204a87;font-weight:bold">&lt;/phase&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;goals&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;goal&gt;</span>schema<span style="color:#204a87;font-weight:bold">&lt;/goal&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;/goals&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;configuration&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;sourceDirectory&gt;</span>${project.basedir}/src/main/avro/<span style="color:#204a87;font-weight:bold">&lt;/sourceDirectory&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;outputDirectory&gt;</span>${project.basedir}/src/main/java/<span style="color:#204a87;font-weight:bold">&lt;/outputDirectory&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;/configuration&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;/execution&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;/executions&gt;</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">&lt;/plugin&gt;</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">&lt;plugin&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;groupId&gt;</span>org.apache.maven.plugins<span style="color:#204a87;font-weight:bold">&lt;/groupId&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;artifactId&gt;</span>maven-compiler-plugin<span style="color:#204a87;font-weight:bold">&lt;/artifactId&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;configuration&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;source&gt;</span>1.8<span style="color:#204a87;font-weight:bold">&lt;/source&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;target&gt;</span>1.8<span style="color:#204a87;font-weight:bold">&lt;/target&gt;</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&lt;/configuration&gt;</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">&lt;/plugin&gt;</span> +</span></span></code></pre></div><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.</p> +<h2 id="defining-a-schema">Defining a schema</h2> +<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&rsquo;s start with a simple schema example, user.avsc:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;namespace&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;example.avro&#34;</span><span style="color:#000;font-weight:bold">,</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;record&#34;</span><span style="color:#000;font-weight:bold">,</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;User&#34;</span><span style="color:#000;font-weight:bold">,</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;fields&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">[</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;string&#34;</span><span style="color:#000;font-weight:bold">},</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">[</span><span style="color:#4e9a06">&#34;int&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;null&#34;</span><span style="color:#000;font-weight:bold">]},</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">[</span><span style="color:#4e9a06">&#34;string&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;null&#34;</span><span style="color:#000;font-weight:bold">]}</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">]</span> +</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> +</span></span></code></pre></div><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 (&ldquo;type&rdquo;: &ldquo;record&rdquo;), a name (&ldquo;name&rdquo;: &ldquo;User&rdquo;), and fields, in this case name, favorite_number, and favorite_color. We also define a namespace (&ldquo;namespace&rdquo;: &ldquo;example.avro&rdquo;), which together with the name attribute defines the &ldquo;full name&rdquo; of the schema (example.avro.User in this case).</p> +<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.</p> +<h2 id="serializing-and-deserializing-with-code-generation">Serializing and deserializing with code generation</h2> +<h3 id="compiling-the-schema">Compiling the schema</h3> +<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:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>java -jar /path/to/avro-tools-1.11.3.jar compile schema &lt;schema file&gt; &lt;destination&gt; +</span></span></code></pre></div><p>This will generate the appropriate source files in a package based on the schema&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</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>java -jar /path/to/avro-tools-1.11.3.jar compile schema user.avsc . +</span></span></code></pre></div><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.</p> +<h3 id="creating-users">Creating Users</h3> +<p>Now that we&rsquo;ve completed the code generation, let&rsquo;s create some Users, serialize them to a data file on disk, and then read back the file and deserialize the User objects.</p> +<p>First let&rsquo;s create some Users and set their fields.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#000">User</span> <span style="color:#000">user1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">();</span> +</span></span><span style="display:flex;"><span><span style="color:#000">user1</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">setName</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Alyssa&#34;</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">user1</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">setFavoriteNumber</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">256</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Leave favorite color null +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span> +</span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Alternate constructor +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">User</span> <span style="color:#000">user2</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Ben&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">7</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;red&#34;</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span> +</span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Construct via builder +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">User</span> <span style="color:#000">user3</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">newBuilder</span><span style="color:#ce5c00;font-weight:bold">()</span> +</span></span><span style="display:flex;"><span> <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">setName</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;Charlie&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span> +</span></span><span style="display:flex;"><span> <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">setFavoriteColor</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;blue&#34;</span><span style="color:#ce5c00;font-weight:bold">)</span> +</span></span><span style="display:flex;"><span> <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">setFavoriteNumber</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">)</span> +</span></span><span style="display:flex;"><span> <span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">build</span><span style="color:#ce5c00;font-weight:bold">();</span> +</span></span></code></pre></div><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.</p> +<p>Note that we do not set user1&rsquo;s favorite color. Since that record is of type [&ldquo;string&rdquo;, &ldquo;null&rdquo;], we can either set it to a string or leave it null; it is essentially optional. Similarly, we set user3&rsquo;s favorite number to null (using a builder requires setting all fields, even if they are null).</p> +<h3 id="serializing">Serializing</h3> +<p>Now let&rsquo;s serialize our Users to disk.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Serialize user1, user2 and user3 to disk +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">DatumWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">userDatumWriter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">SpecificDatumWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">DataFileWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">dataFileWriter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">DataFileWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">userDatumWriter</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">create</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user1</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">getSchema</span><span style="color:#ce5c00;font-weight:bold">(),</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">File</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;users.avro&#34;</span><span style="color:#ce5c00;font-weight:bold">));</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">append</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user1</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">append</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user2</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">append</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user3</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">close</span><span style="color:#ce5c00;font-weight:bold">();</span> +</span></span></code></pre></div><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.</p> +<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.</p> +<h3 id="deserializing">Deserializing</h3> +<p>Finally, let&rsquo;s deserialize the data file we just created.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Deserialize Users from disk +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">DatumReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">userDatumReader</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">SpecificDatumReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">class</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">DataFileReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">dataFileReader</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">DataFileReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">User</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">file</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">userDatumReader</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">User</span> <span style="color:#000">user</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">;</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">while</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">dataFileReader</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">hasNext</span><span style="color:#ce5c00;font-weight:bold">())</span> <span style="color:#ce5c00;font-weight:bold">{</span> +</span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Reuse user object by passing it to next(). This saves us from +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// allocating and garbage collecting many objects for files with +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// many items. +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">user</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dataFileReader</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">next</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#ce5c00;font-weight:bold">}</span> +</span></span></code></pre></div><p>This snippet will output:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Alyssa&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">256</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">null</sp an><span style="color:#000;font-weight:bold">}</span> +</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Ben&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">7</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;red&#34;</span><span style="color:#000;font-weight:bold">}</span> +</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Charlie&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;blue&#34;</span><span style="color:#000;font-weight:bold">}</span> +</span></span></code></pre></div><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&rsquo;s schema included in the file and the schema provided by the reader, in this case the User class. The writer&rsquo;s schema is needed to know the order in which fields were written, while the reader&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.</p> +<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&rsquo;s also possible to use for (User user : dataFileReader) if performance is not a concern.</p> +<h3 id="compiling-and-running-the-example-code">Compiling and running the example code</h3> +<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:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ mvn compile <span style="color:#8f5902;font-style:italic"># includes code generation via Avro Maven plugin</span> +</span></span><span style="display:flex;"><span>$ mvn -q exec:java -Dexec.mainClass<span style="color:#ce5c00;font-weight:bold">=</span>example.SpecificMain +</span></span></code></pre></div><h3 id="beta-feature-generating-faster-code">Beta feature: Generating faster code</h3> +<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:</p> +<p>$ mvn -q exec:java -Dexec.mainClass=example.SpecificMain <br> +-Dorg.apache.avro.specific.use_custom_coders=true</p> +<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.</p> +<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).</p> +<h2 id="serializing-and-deserializing-without-code-generation">Serializing and deserializing without code generation</h2> +<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.</p> +<p>Let&rsquo;s go over the same example as in the previous section, but without using code generation: we&rsquo;ll create some users, serialize them to a data file on disk, and then read back the file and deserialize the users objects.</p> +<h3 id="creating-users-1">Creating users</h3> +<p>First, we use a Parser to read our schema definition and create a Schema object.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#000">Schema</span> <span style="color:#000">schema</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">Schema</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">Parser</span><span style="color:#ce5c00;font-weight:bold">().</span><span style="color:#c4a000">parse</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">File</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;user.avsc&#34;</span><span style="color:#ce5c0 0;font-weight:bold">));</span> +</span></span></code></pre></div><p>Using this schema, let&rsquo;s create some users.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#000">GenericRecord</span> <span style="color:#000">user1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">GenericData</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">Record</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">schema</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">user1</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;Alyssa&#34;</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">user1</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">256</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Leave favorite color null +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span> +</span></span><span style="display:flex;"><span><span style="color:#000">GenericRecord</span> <span style="color:#000">user2</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">GenericData</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">Record</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">schema</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">user2</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;Ben&#34;</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">user2</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">7</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">user2</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">put</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;favorite_color&#34;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;red&#34;</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span></code></pre></div><p>Since we&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(&ldquo;favorite_animal&rdquo;, &ldquo;cat&rdquo;)), we&rsquo;ll get an AvroRuntimeException when we run the program.</p> +<p>Note that we do not set user1&rsquo;s favorite color. Since that record is of type [&ldquo;string&rdquo;, &ldquo;null&rdquo;], we can either set it to a string or leave it null; it is essentially optional.</p> +<h3 id="serializing-1">Serializing</h3> +<p>Now that we&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.</p> +<p>First we&rsquo;ll serialize our users to a data file on disk.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Serialize user1 and user2 to disk +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">File</span> <span style="color:#000">file</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">File</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#4e9a06">&#34;users.avro&#34;</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">DatumWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">datumWriter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">GenericDatumWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">schema</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">DataFileWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">dataFileWriter</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">DataFileWriter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">datumWriter</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">create</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">schema</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">file</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">append</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user1</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">append</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user2</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">dataFileWriter</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">close</span><span style="color:#ce5c00;font-weight:bold">();</span> +</span></span></code></pre></div><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.</p> +<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.</p> +<h3 id="deserializing-1">Deserializing</h3> +<p>Finally, we&rsquo;ll deserialize the data file we just created.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Deserialize users from disk +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">DatumReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">datumReader</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">GenericDatumReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">schema</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">DataFileReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">dataFileReader</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">DataFileReader</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">GenericRecord</span><span style="color:#ce5c00;font-weight:bold">&gt;(</span><span style="color:#000">file</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000">datumReader</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">GenericRecord</span> <span style="color:#000">user</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">null</span><span style="color:#ce5c00;font-weight:bold">;</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">while</span> <span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">dataFileReader</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">hasNext</span><span style="color:#ce5c00;font-weight:bold">())</span> <span style="color:#ce5c00;font-weight:bold">{</span> +</span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// Reuse user object by passing it to next(). This saves us from +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// allocating and garbage collecting many objects for files with +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// many items. +</span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">user</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">dataFileReader</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">next</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span><span style="display:flex;"><span><span style="color:#000">System</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">out</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#c4a000">println</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#000">user</span><span style="color:#ce5c00;font-weight:bold">);</span> +</span></span></code></pre></div><p>This outputs:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Alyssa&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">256</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">null</sp an><span style="color:#000;font-weight:bold">}</span> +</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Ben&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">7</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;red&#34;</span><span style="color:#000;font-weight:bold">}</span> +</span></span></code></pre></div><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&rsquo;s schema included in the file, and the reader&rsquo;s schema provided to the GenericDatumReader. The writer&rsquo;s schema is needed to know the order in which fields were written, while the reader&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.</p> +<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&rsquo;s also possible to use for (GenericRecord user : dataFileReader) if performance is not a concern.</p> +<h3 id="compiling-and-running-the-example-code-1">Compiling and running the example code</h3> +<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:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ mvn compile +</span></span><span style="display:flex;"><span>$ mvn -q exec:java -Dexec.mainClass<span style="color:#ce5c00;font-weight:bold">=</span>example.GenericMain +</span></span></code></pre></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> +<!-- +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. +--> +<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&rsquo;s Avro RPC Quick Start for a good introduction to using Avro for RPC.</p> +<h2 id="notice-for-python-3-users">Notice for Python 3 users</h2> +<p>A package called &ldquo;avro-python3&rdquo; had been provided to support Python 3 previously, but the codebase was consolidated into the &ldquo;avro&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 &ldquo;avro&rdquo; package instead. They are mostly API compatible, but there&rsquo;s a few minor difference (e.g., function name capitalization, such as avro.schema.Parse vs avro.schema.parse).</p> +<h2 id="download">Download</h2> +<p>For Python, the easiest way to get started is to install it from PyPI. Python&rsquo;s Avro API is available over PyPi.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ python3 -m pip install avro +</span></span></code></pre></div><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.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ tar xvf avro-1.11.3.tar.gz +</span></span><span style="display:flex;"><span>$ <span style="color:#204a87">cd</span> avro-1.11.3 +</span></span><span style="display:flex;"><span>$ python setup.py install +</span></span><span style="display:flex;"><span>$ python +</span></span><span style="display:flex;"><span>&gt;&gt;&gt; import avro <span style="color:#8f5902;font-style:italic"># should not raise ImportError</span> +</span></span></code></pre></div><p>Alternatively, you may build the Avro Python library from source. From your the root Avro directory, run the commands</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>$ <span style="color:#204a87">cd</span> lang/py/ +</span></span><span style="display:flex;"><span>$ python3 -m pip install -e . +</span></span><span style="display:flex;"><span>$ python +</span></span></code></pre></div><h2 id="defining-a-schema">Defining a schema</h2> +<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&rsquo;s start with a simple schema example, user.avsc:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;namespace&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;example.avro&#34;</span><span style="color:#000;font-weight:bold">,</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;record&#34;</span><span style="color:#000;font-weight:bold">,</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;User&#34;</span><span style="color:#000;font-weight:bold">,</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;fields&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">[</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;string&#34;</span><span style="color:#000;font-weight:bold">},</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">[</span><span style="color:#4e9a06">&#34;int&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;null&#34;</span><span style="color:#000;font-weight:bold">]},</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">&#34;type&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">[</span><span style="color:#4e9a06">&#34;string&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;null&#34;</span><span style="color:#000;font-weight:bold">]}</span> +</span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">]</span> +</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> +</span></span></code></pre></div><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 (&ldquo;type&rdquo;: &ldquo;record&rdquo;), a name (&ldquo;name&rdquo;: &ldquo;User&rdquo;), and fields, in this case name, favorite_number, and favorite_color. We also define a namespace (&ldquo;namespace&rdquo;: &ldquo;example.avro&rdquo;), which together with the name attribute defines the &ldquo;full name&rdquo; of the schema (example.avro.User in this case).</p> +<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.</p> +<h2 id="serializing-and-deserializing-without-code-generation">Serializing and deserializing without code generation</h2> +<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.</p> +<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:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">avro.schema</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">from</span> <span style="color:#000">avro.datafile</span> <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">DataFileReader</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">DataFileWriter</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">from</span> <span style="color:#000">avro.io</span> <span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">DatumReader</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">DatumWriter</span> +</span></span><span style="display:flex;"><span> +</span></span><span style="display:flex;"><span><span style="color:#000">schema</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">avro</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">schema</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">parse</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;user.avsc&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;rb&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">())</span> +</span></span><span style="display:flex;"><span> +</span></span><span style="display:flex;"><span><span style="color:#000">writer</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">DataFileWriter</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;users.avro&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;wb&#34;</span><span style="color:#000;font-weight:bold">),</span> <span style="color:#000">DatumWriter</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">schema</span><span style="color:#000;font-weight:bold">)</span> +</span></span><span style="display:flex;"><span><span style="color:#000">writer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">append</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Alyssa&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">256</span><span style="color:#000;font-weight:bold">})</span> +</span></span><span style="display:flex;"><span><span style="color:#000">writer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">append</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Ben&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">7</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;red&#34;</span><span style="color:#000;font-weight:bold">})</spa n> +</span></span><span style="display:flex;"><span><span style="color:#000">writer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">close</span><span style="color:#000;font-weight:bold">()</span> +</span></span><span style="display:flex;"><span> +</span></span><span style="display:flex;"><span><span style="color:#000">reader</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">DataFileReader</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;users.avro&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;rb&#34;</span><span style="color:#000;font-weight:bold">),</span> <span style="color:#000">DatumReader</span><span style="color:#000;font-weight:bold">())</span> +</span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000">user</span> <span style="color:#204a87;font-weight:bold">in</span> <span style="color:#000">reader</span><span style="color:#000;font-weight:bold">:</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">user</span><span style="color:#000;font-weight:bold">)</span> +</span></span><span style="display:flex;"><span><span style="color:#000">reader</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">close</span><span style="color:#000;font-weight:bold">()</span> +</span></span></code></pre></div><p>This outputs:</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#a40000">u&#39;favorite_color&#39;:</span> <span style="color:#a40000">None,</span> <span style="color:#a40000">u&#39;favorite_number&#39;:</span> <span style="color:#a40000">256,</span> <span style="color:#a40000">u&#39;name&#39;:</span> <span style="color:#a40000">u&#39;Alyssa&#39;</span><span style="color:#000;font-weight:bold">}</span> +</span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#a40000">u&#39;favorite_color&#39;:</span> <span style="color:#a40000">u&#39;red&#39;,</span> <span style="color:#a40000">u&#39;favorite_number&#39;:</span> <span style="color:#a40000">7,</span> <span style="color:#a40000">u&#39;name&#39;:</span> <span style="color:#a40000">u&#39;Ben&#39;</span><span style="color:#000;font-weight:bold">}</span> +</span></span></code></pre></div><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.</p> +<p>Let&rsquo;s take a closer look at what&rsquo;s going on here.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#000">schema</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">avro</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">schema</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">parse</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;user.avsc&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;rb&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;fo nt-weight:bold">.</span><span style="color:#000">read</span><span style="color:#000;font-weight:bold">())</span> +</span></span></code></pre></div><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&rsquo;re passing in the contents of our user.avsc schema file here.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#000">writer</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">DataFileWriter</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;users.avro&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;wb&#34;</span><span style="color:#000;font-weight:bold">),</span> <span style="color:#000">DatumWriter</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#000">schema</span><span style="color:#000;font-weight:bold">)</span> +</span></span></code></pre></div><p>We create a DataFileWriter, which we&rsquo;ll use to write serialized items to a data file on disk. The DataFileWriter constructor takes three arguments:</p> +<ul> +<li>The file we&rsquo;ll serialize to</li> +<li>A DatumWriter, which is responsible for actually serializing the items to Avro&rsquo;s binary format (DatumWriters can be used separately from DataFileWriters, e.g., to perform IPC with Avro).</li> +<li>The schema we&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.</li> +</ul> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#000">writer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">append</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Alyssa&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">256</span><span style="color:#000;font-weight:bold">})</span> +</span></span><span style="display:flex;"><span><span style="color:#000">writer</span><span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#000">append</span><span style="color:#000;font-weight:bold">({</span><span style="color:#4e9a06">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;Ben&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;favorite_number&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">7</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;favorite_color&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;red&#34;</span><span style="color:#000;font-weight:bold">})</spa n> +</span></span></code></pre></div><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 [&ldquo;string&rdquo;, &ldquo;null&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.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#000">reader</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">DataFileReader</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;users.avro&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;rb&#34;</span><span style="color:#000;font-weight:bold">),</span> <span style="color:#000">DatumReader</span><span style="color:#000;font-weight:bold">())</span> +</span></span></code></pre></div><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.</p> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">for</span> <span style="color:#000">user</span> <span style="color:#204a87;font-weight:bold">in</span> <span style="color:#000">reader</span><span style="color:#000;font-weight:bold">:</span> +</span></span><span style="display:flex;"><span> <span style="color:#204a87">print</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">user</span><span style="color:#000;font-weight:bold">)</span> +</span></span></code></pre></div><p>The DataFileReader is an iterator that returns dicts corresponding to the serialized items.</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> +<!-- +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. +--> +<h2 id="introduction">Introduction</h2> +<p>This document defines Apache Avro. It is intended to be the authoritative specification. Implementations of Avro must adhere to this document.</p> +<h2 id="schema-declaration">Schema Declaration</h2> +<p>A Schema is represented in <a href="https://www.json.org/">JSON</a> by one of:</p> +<ul> +<li>A JSON string, naming a defined type.</li> +<li>A JSON object, of the form:</li> +</ul> +<div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-js" data-lang="js"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#4e9a06">&#34;type&#34;</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;typeName&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">...</span><span style="color:#000">attributes</span><span style="color:#000;font-weight:bold">...}</span> +</span></span></code></pre></div><p>where <em>typeName</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.</p> +<ul> +<li>A JSON array, representing a union of embedded types.</li> +</ul> +<h2 id="primitive-types">Primitive Types</h2> +<p>The set of primitive type names is:</p> +<ul> +<li><em>null</em>: no value</li> +<li><em>boolean</em>: a binary value</li> +<li><em>int</em>: 32-bit signed integer</li> +<li><em>long</em>: 64-bit signed integer</li> +<li><em>float</em>: single precision (32-bit) IEEE 754 floating-point number</li> +<li><em>double</em>: double precision (64-bit) IEEE 754 floating-point number</li> +<li><em>bytes</em>: sequence of 8-bit unsigned bytes</li> +<li><em>string</em>: unicode character sequence</li> +</ul> +<p>Primitive types have no specified attributes.</p> +<p>Primitive type names are also defined type names. Thus, for example, the schema &ldquo;string&rdquo; is equivalent to:</p>
[... 1536 lines stripped ...]
