blob: 17eb109c36b7007a1bb53bf99efa911656894c2b [file] [log] [blame]
<!doctype html><html lang=en class=no-js><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name=generator content="Hugo 0.111.3"><link rel=alternate type=text/html href=/update-site/docs/++version++/getting-started-java/_print/><link rel=alternate type=application/rss+xml href=/update-site/docs/++version++/getting-started-java/index.xml><meta name=robots content="index, follow"><link rel=apple-touch-icon sizes=57x57 href=https://apache.org/favicons/apple-touch-icon-57x57.png><link rel=apple-touch-icon sizes=60x60 href=https://apache.org/favicons/apple-touch-icon-60x60.png><link rel=apple-touch-icon sizes=72x72 href=https://apache.org/favicons/apple-touch-icon-72x72.png><link rel=apple-touch-icon sizes=76x76 href=https://apache.org/favicons/apple-touch-icon-76x76.png><link rel=apple-touch-icon sizes=114x114 href=https://apache.org/favicons/apple-touch-icon-114x114.png><link rel=apple-touch-icon sizes=120x120 href=https://apache.org/favicons/apple-touch-icon-120x120.png><link rel=apple-touch-icon sizes=144x144 href=https://apache.org/favicons/apple-touch-icon-144x144.png><link rel=apple-touch-icon sizes=152x152 href=https://apache.org/favicons/apple-touch-icon-152x152.png><link rel=apple-touch-icon sizes=180x180 href=https://apache.org/favicons/apple-touch-icon-180x180.png><link rel=icon type=image/png href=https://apache.org/favicons/favicon-32x32.png sizes=32x32><link rel=icon type=image/png href=https://apache.org/favicons/favicon-194x194.png sizes=194x194><link rel=icon type=image/png href=https://apache.org/favicons/favicon-96x96.png sizes=96x96><link rel=icon type=image/png href=https://apache.org/favicons/android-chrome-192x192.png sizes=192x192><link rel=icon type=image/png href=https://apache.org/favicons/favicon-16x16.png sizes=16x16><link rel=manifest href=https://apache.org/favicons/manifest.json><link rel="shortcut icon" href=https://apache.org/favicons/favicon.ico><title>Getting Started (Java) | Apache Avro</title><meta name=description content><meta property="og:title" content="Getting Started (Java)"><meta property="og:description" content><meta property="og:type" content="website"><meta property="og:url" content="/update-site/docs/++version++/getting-started-java/"><meta property="og:site_name" content="Apache Avro"><meta itemprop=name content="Getting Started (Java)"><meta itemprop=description content><meta name=twitter:card content="summary"><meta name=twitter:title content="Getting Started (Java)"><meta name=twitter:description content><link rel=preload href=/update-site/scss/main.min.6deb8a211453721a965671b611280fb11af8ef2def6b7a2b0a34f6a94939360f.css as=style><link href=/update-site/scss/main.min.6deb8a211453721a965671b611280fb11af8ef2def6b7a2b0a34f6a94939360f.css rel=stylesheet integrity><script src=https://code.jquery.com/jquery-3.5.1.min.js integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin=anonymous></script>
<link rel=stylesheet href=/css/prism.css></head><body class=td-section><header><nav class="js-navbar-scroll navbar navbar-expand navbar-dark flex-column flex-md-row td-navbar"><a class=navbar-brand href=/update-site/><span class=navbar-logo><img src=/docs/++version++/logo.svg width=100 height=30 style="margin:0 10px"></span><span class="text-uppercase font-weight-bold">Apache Avro</span></a><div class="td-navbar-nav-scroll ml-md-auto" id=main_navbar><ul class="navbar-nav mt-2 mt-lg-0"><li class="nav-item mr-4 mb-2 mb-lg-0"><a class=nav-link href=/update-site/project/><span>Project</span></a></li><li class="nav-item mr-4 mb-2 mb-lg-0"><a class=nav-link href=/update-site/blog/><span>Blog</span></a></li><li class="nav-item mr-4 mb-2 mb-lg-0"><a class=nav-link href=/update-site/community/><span>Community</span></a></li><li class="nav-item dropdown mr-4 d-none d-lg-block"><a class="nav-link dropdown-toggle" href=# id=navbarDropdown role=button data-toggle=dropdown aria-haspopup=true aria-expanded=false>Documentation</a><div class=dropdown-menu aria-labelledby=navbarDropdownMenuLink><a class=dropdown-item href=./docs/++version++/>++version++ (Current)</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.11.0/>1.11.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.10.2/>1.10.2</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.10.1/>1.10.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.10.0/>1.10.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.9.2/>1.9.2</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.9.1/>1.9.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.9.0/>1.9.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.8.2/>1.8.2</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.8.1/>1.8.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.8.0/>1.8.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.7/>1.7.7</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.6/>1.7.6</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.5/>1.7.5</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.4/>1.7.4</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.3/>1.7.3</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.2/>1.7.2</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.1/>1.7.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.7.0/>1.7.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.6.3/>1.6.3</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.6.2/>1.6.2</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.6.1/>1.6.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.6.0/>1.6.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.5.4/>1.5.4</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.5.3/>1.5.3</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.5.2/>1.5.2</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.5.1/>1.5.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.5.0/>1.5.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.4.1/>1.4.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.4.0/>1.4.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.3.3/>1.3.3</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.3.2/>1.3.2</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.3.1/>1.3.1</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.3.0/>1.3.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.2.0/>1.2.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.1.0/>1.1.0</a>
<a class=dropdown-item href=https://avro.apache.org/docs/1.0.0/>1.0.0</a></div></li><li class="nav-item dropdown mr-4 d-none d-lg-block"><a class="nav-link dropdown-toggle" href=# id=navbarDropdown role=button data-toggle=dropdown aria-haspopup=true aria-expanded=false>ASF links</a><div class=dropdown-menu aria-labelledby=navbarDropdownMenuLink><a class=dropdown-item href=http://www.apache.org/ target=_blank>ASF Web Site</a>
<a class=dropdown-item href=http://www.apache.org/licenses/ target=_blank>License</a>
<a class=dropdown-item href=http://www.apache.org/foundation/sponsorship.html target=_blank>Donate</a>
<a class=dropdown-item href=http://www.apache.org/foundation/thanks.html target=_blank>Thanks</a>
<a class=dropdown-item href=http://www.apache.org/security/ target=_blank>Security</a></div></li></ul></div><div class="navbar-nav d-none d-lg-block"></div></nav></header><div class="container-fluid td-outer"><div class=td-main><div class="row flex-xl-nowrap"><aside class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none"><div id=td-sidebar-menu class=td-sidebar__inner><div id=content-mobile><form class="td-sidebar__search d-flex align-items-center"><button class="btn btn-link td-sidebar__toggle d-md-none p-0 ml-3 fas fa-bars" type=button data-toggle=collapse data-target=#td-section-nav aria-controls=td-docs-nav aria-expanded=false aria-label="Toggle section navigation"></button></form></div><div id=content-desktop></div><nav class="collapse td-sidebar-nav foldable-nav" id=td-section-nav><ul class="td-sidebar-nav__section pr-md-3 ul-0"><li class="td-sidebar-nav__section-title td-sidebar-nav__section with-child active-path" id=m-update-sitedocs-li><a href=/update-site/docs/ class="align-left pl-0 td-sidebar-link td-sidebar-link__section tree-root" id=m-update-sitedocs><span>Documentation</span></a><ul class=ul-1><li class="td-sidebar-nav__section-title td-sidebar-nav__section with-child active-path" id=m-update-sitedocsversion-li><input type=checkbox id=m-update-sitedocsversion-check checked>
<label for=m-update-sitedocsversion-check><a href=/update-site/docs/++version++/ title="Apache Avro™ ++version++ Documentation" class="align-left pl-0 td-sidebar-link td-sidebar-link__section" id=m-update-sitedocsversion><span>++version++</span></a></label><ul class="ul-2 foldable"><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversiongetting-started-java-li><input type=checkbox id=m-update-sitedocsversiongetting-started-java-check>
<label for=m-update-sitedocsversiongetting-started-java-check><a href=/update-site/docs/++version++/getting-started-java/ class="align-left pl-0 active td-sidebar-link td-sidebar-link__section" id=m-update-sitedocsversiongetting-started-java><span class=td-sidebar-nav-active-item>Getting Started (Java)</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversiongetting-started-python-li><input type=checkbox id=m-update-sitedocsversiongetting-started-python-check>
<label for=m-update-sitedocsversiongetting-started-python-check><a href=/update-site/docs/++version++/getting-started-python/ class="align-left pl-0 td-sidebar-link td-sidebar-link__section" id=m-update-sitedocsversiongetting-started-python><span>Getting Started (Python)</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionspecification-li><input type=checkbox id=m-update-sitedocsversionspecification-check>
<label for=m-update-sitedocsversionspecification-check><a href=/update-site/docs/++version++/specification/ class="align-left pl-0 td-sidebar-link td-sidebar-link__section" id=m-update-sitedocsversionspecification><span>Specification</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionapi-java-li><input type=checkbox id=m-update-sitedocsversionapi-java-check>
<label for=m-update-sitedocsversionapi-java-check><a href=./api/java/ class="align-left pl-0 td-sidebar-link td-sidebar-link__page" id=m-update-sitedocsversionapi-java><span>Java API</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionapi-c-li><input type=checkbox id=m-update-sitedocsversionapi-c-check>
<label for=m-update-sitedocsversionapi-c-check><a href=./api/c/ class="align-left pl-0 td-sidebar-link td-sidebar-link__page" id=m-update-sitedocsversionapi-c><span>C API</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionapi-c-li><input type=checkbox id=m-update-sitedocsversionapi-c-check>
<label for=m-update-sitedocsversionapi-c-check><a href=./api/cpp/html/ class="align-left pl-0 td-sidebar-link td-sidebar-link__page" id=m-update-sitedocsversionapi-c><span>C++ API</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionapi-csharp-li><input type=checkbox id=m-update-sitedocsversionapi-csharp-check>
<label for=m-update-sitedocsversionapi-csharp-check><a href=./api/csharp/html/ class="align-left pl-0 td-sidebar-link td-sidebar-link__page" id=m-update-sitedocsversionapi-csharp><span>C# API</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionmapreduce-guide-li><input type=checkbox id=m-update-sitedocsversionmapreduce-guide-check>
<label for=m-update-sitedocsversionmapreduce-guide-check><a href=/update-site/docs/++version++/mapreduce-guide/ class="align-left pl-0 td-sidebar-link td-sidebar-link__section" id=m-update-sitedocsversionmapreduce-guide><span>MapReduce guide</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionidl-language-li><input type=checkbox id=m-update-sitedocsversionidl-language-check>
<label for=m-update-sitedocsversionidl-language-check><a href=/update-site/docs/++version++/idl-language/ class="align-left pl-0 td-sidebar-link td-sidebar-link__section" id=m-update-sitedocsversionidl-language><span>IDL Language</span></a></label></li><li class="td-sidebar-nav__section-title td-sidebar-nav__section without-child" id=m-update-sitedocsversionsasl-profile-li><input type=checkbox id=m-update-sitedocsversionsasl-profile-check>
<label for=m-update-sitedocsversionsasl-profile-check><a href=/update-site/docs/++version++/sasl-profile/ class="align-left pl-0 td-sidebar-link td-sidebar-link__section" id=m-update-sitedocsversionsasl-profile><span>SASL profile</span></a></label></li></ul></li></ul></li></ul></nav></div></aside><aside class="d-none d-xl-block col-xl-2 td-sidebar-toc d-print-none"><div class="td-page-meta ml-2 pb-1 pt-2 mb-0"><a href=https://github.com/apache/avro/tree/master/doc/content/en/docs/++version++/Getting%20started%20%28Java%29/_index.md class=td-page-meta--view target=_blank rel=noopener><i class="fa fa-file-alt fa-fw"></i> View page source</a>
<a href=https://github.com/apache/avro/edit/master/doc/content/en/docs/++version++/Getting%20started%20%28Java%29/_index.md class=td-page-meta--edit target=_blank rel=noopener><i class="fa fa-edit fa-fw"></i> Edit this page</a>
<a href="https://github.com/apache/avro/new/master/doc/content/en/docs/++version++/Getting%20started%20%28Java%29/_index.md?filename=change-me.md&amp;value=---%0Atitle%3A+%22Long+Page+Title%22%0AlinkTitle%3A+%22Short+Nav+Title%22%0Aweight%3A+100%0Adescription%3A+%3E-%0A+++++Page+description+for+heading+and+indexes.%0A---%0A%0A%23%23+Heading%0A%0AEdit+this+template+to+create+your+new+page.%0A%0A%2A+Give+it+a+good+name%2C+ending+in+%60.md%60+-+e.g.+%60getting-started.md%60%0A%2A+Edit+the+%22front+matter%22+section+at+the+top+of+the+page+%28weight+controls+how+its+ordered+amongst+other+pages+in+the+same+directory%3B+lowest+number+first%29.%0A%2A+Add+a+good+commit+message+at+the+bottom+of+the+page+%28%3C80+characters%3B+use+the+extended+description+field+for+more+detail%29.%0A%2A+Create+a+new+branch+so+you+can+preview+your+new+file+and+request+a+review+via+Pull+Request.%0A" class=td-page-meta--child target=_blank rel=noopener><i class="fa fa-edit fa-fw"></i> Create child page</a>
<a href="https://github.com/apache/avro/issues/new?title=Getting%20Started%20%28Java%29" class=td-page-meta--issue target=_blank rel=noopener><i class="fab fa-github fa-fw"></i> Create documentation issue</a>
<a href=https://github.com/apache/avro/issues/new class=td-page-meta--project-issue target=_blank rel=noopener><i class="fas fa-tasks fa-fw"></i> Create project issue</a>
<a id=print href=/update-site/docs/++version++/getting-started-java/_print/><i class="fa fa-print fa-fw"></i> Print entire section</a></div><div class=td-toc><nav id=TableOfContents><ul><li><a href=#download>Download</a></li><li><a href=#defining-a-schema>Defining a schema</a></li><li><a href=#serializing-and-deserializing-with-code-generation>Serializing and deserializing with code generation</a><ul><li><a href=#compiling-the-schema>Compiling the schema</a></li><li><a href=#creating-users>Creating Users</a></li><li><a href=#serializing>Serializing</a></li><li><a href=#deserializing>Deserializing</a></li><li><a href=#compiling-and-running-the-example-code>Compiling and running the example code</a></li><li><a href=#beta-feature-generating-faster-code>Beta feature: Generating faster code</a></li></ul></li><li><a href=#serializing-and-deserializing-without-code-generation>Serializing and deserializing without code generation</a><ul><li><a href=#creating-users-1>Creating users</a></li><li><a href=#serializing-1>Serializing</a></li><li><a href=#deserializing-1>Deserializing</a></li><li><a href=#compiling-and-running-the-example-code-1>Compiling and running the example code</a></li></ul></li></ul></nav></div><div class="taxonomy taxonomy-terms-cloud taxo-tags"><h5 class=taxonomy-title>Tag Cloud</h5><ul class=taxonomy-terms><li><a class=taxonomy-term href=/update-site/tags/java/ data-taxonomy-term=java><span class=taxonomy-label>java</span><span class=taxonomy-count>1</span></a></li><li><a class=taxonomy-term href=/update-site/tags/python/ data-taxonomy-term=python><span class=taxonomy-label>python</span><span class=taxonomy-count>1</span></a></li></ul></div></aside><main class="col-12 col-md-9 col-xl-8 pl-md-5" role=main><nav aria-label=breadcrumb class=td-breadcrumbs><ol class=breadcrumb><li class=breadcrumb-item><a href=/update-site/docs/>Documentation</a></li><li class=breadcrumb-item><a href=/update-site/docs/++version++/>++version++</a></li><li class="breadcrumb-item active" aria-current=page><a href=/update-site/docs/++version++/getting-started-java/>Getting Started (Java)</a></li></ol></nav><div class=td-content><h1>Getting Started (Java)</h1><header class=article-meta><div class="taxonomy taxonomy-terms-article taxo-tags"><h5 class=taxonomy-title>Tags:</h5><ul class=taxonomy-terms><li><a class=taxonomy-term href=/update-site/tags/java/ data-taxonomy-term=java><span class=taxonomy-label>java</span></a></li></ul></div><p class=reading-time><i class="fa fa-clock" aria-hidden=true></i>&nbsp; 11 minute read &nbsp;</p></header><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=/update-site/project/download/>Apache Avro™ Download</a> page. This guide uses Avro ++version++, the latest version at the time of writing. For the examples in this guide, download avro-++version++.jar and avro-tools-++version++.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:700>&lt;dependency&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;groupId&gt;</span>org.apache.avro<span style=color:#204a87;font-weight:700>&lt;/groupId&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;artifactId&gt;</span>avro<span style=color:#204a87;font-weight:700>&lt;/artifactId&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;version&gt;</span>&#43;&#43;version&#43;&#43;<span style=color:#204a87;font-weight:700>&lt;/version&gt;</span>
</span></span><span style=display:flex><span><span style=color:#204a87;font-weight:700>&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:700>&lt;plugin&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;groupId&gt;</span>org.apache.avro<span style=color:#204a87;font-weight:700>&lt;/groupId&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;artifactId&gt;</span>avro-maven-plugin<span style=color:#204a87;font-weight:700>&lt;/artifactId&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;version&gt;</span>&#43;&#43;version&#43;&#43;<span style=color:#204a87;font-weight:700>&lt;/version&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;executions&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;execution&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;phase&gt;</span>generate-sources<span style=color:#204a87;font-weight:700>&lt;/phase&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;goals&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;goal&gt;</span>schema<span style=color:#204a87;font-weight:700>&lt;/goal&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;/goals&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;configuration&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;sourceDirectory&gt;</span>${project.basedir}/src/main/avro/<span style=color:#204a87;font-weight:700>&lt;/sourceDirectory&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;outputDirectory&gt;</span>${project.basedir}/src/main/java/<span style=color:#204a87;font-weight:700>&lt;/outputDirectory&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;/configuration&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;/execution&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;/executions&gt;</span>
</span></span><span style=display:flex><span><span style=color:#204a87;font-weight:700>&lt;/plugin&gt;</span>
</span></span><span style=display:flex><span><span style=color:#204a87;font-weight:700>&lt;plugin&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;groupId&gt;</span>org.apache.maven.plugins<span style=color:#204a87;font-weight:700>&lt;/groupId&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;artifactId&gt;</span>maven-compiler-plugin<span style=color:#204a87;font-weight:700>&lt;/artifactId&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;configuration&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;source&gt;</span>1.8<span style=color:#204a87;font-weight:700>&lt;/source&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;target&gt;</span>1.8<span style=color:#204a87;font-weight:700>&lt;/target&gt;</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&lt;/configuration&gt;</span>
</span></span><span style=display:flex><span><span style=color:#204a87;font-weight:700>&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:700>{</span><span style=color:#204a87;font-weight:700>&#34;namespace&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;example.avro&#34;</span><span style=color:#000;font-weight:700>,</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&#34;type&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;record&#34;</span><span style=color:#000;font-weight:700>,</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;User&#34;</span><span style=color:#000;font-weight:700>,</span>
</span></span><span style=display:flex><span> <span style=color:#204a87;font-weight:700>&#34;fields&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#000;font-weight:700>[</span>
</span></span><span style=display:flex><span> <span style=color:#000;font-weight:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;name&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;type&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;string&#34;</span><span style=color:#000;font-weight:700>},</span>
</span></span><span style=display:flex><span> <span style=color:#000;font-weight:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;favorite_number&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;type&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#000;font-weight:700>[</span><span style=color:#4e9a06>&#34;int&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#4e9a06>&#34;null&#34;</span><span style=color:#000;font-weight:700>]},</span>
</span></span><span style=display:flex><span> <span style=color:#000;font-weight:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;favorite_color&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;type&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#000;font-weight:700>[</span><span style=color:#4e9a06>&#34;string&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#4e9a06>&#34;null&#34;</span><span style=color:#000;font-weight:700>]}</span>
</span></span><span style=display:flex><span> <span style=color:#000;font-weight:700>]</span>
</span></span><span style=display:flex><span><span style=color:#000;font-weight:700>}</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-&#43;&#43;version&#43;&#43;.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-&#43;&#43;version&#43;&#43;.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:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>();</span>
</span></span><span style=display:flex><span><span style=color:#000>user1</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>setName</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;Alyssa&#34;</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>user1</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>setFavoriteNumber</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#0000cf;font-weight:700>256</span><span style=color:#ce5c00;font-weight:700>);</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:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;Ben&#34;</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#0000cf;font-weight:700>7</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#4e9a06>&#34;red&#34;</span><span style=color:#ce5c00;font-weight:700>);</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:700>=</span> <span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>newBuilder</span><span style=color:#ce5c00;font-weight:700>()</span>
</span></span><span style=display:flex><span> <span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>setName</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;Charlie&#34;</span><span style=color:#ce5c00;font-weight:700>)</span>
</span></span><span style=display:flex><span> <span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>setFavoriteColor</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;blue&#34;</span><span style=color:#ce5c00;font-weight:700>)</span>
</span></span><span style=display:flex><span> <span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>setFavoriteNumber</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#204a87;font-weight:700>null</span><span style=color:#ce5c00;font-weight:700>)</span>
</span></span><span style=display:flex><span> <span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>build</span><span style=color:#ce5c00;font-weight:700>();</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:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>userDatumWriter</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>SpecificDatumWriter</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>class</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>DataFileWriter</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>dataFileWriter</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>DataFileWriter</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>userDatumWriter</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>create</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user1</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>getSchema</span><span style=color:#ce5c00;font-weight:700>(),</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>File</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;users.avro&#34;</span><span style=color:#ce5c00;font-weight:700>));</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>append</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user1</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>append</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user2</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>append</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user3</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>close</span><span style=color:#ce5c00;font-weight:700>();</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:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>userDatumReader</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>SpecificDatumReader</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>class</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>DataFileReader</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>dataFileReader</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>DataFileReader</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>User</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>file</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#000>userDatumReader</span><span style=color:#ce5c00;font-weight:700>);</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:700>=</span> <span style=color:#204a87;font-weight:700>null</span><span style=color:#ce5c00;font-weight:700>;</span>
</span></span><span style=display:flex><span><span style=color:#204a87;font-weight:700>while</span> <span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>dataFileReader</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>hasNext</span><span style=color:#ce5c00;font-weight:700>())</span> <span style=color:#ce5c00;font-weight:700>{</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:700>=</span> <span style=color:#000>dataFileReader</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>next</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>System</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>out</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>println</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#ce5c00;font-weight:700>}</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:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;Alyssa&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_number&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#0000cf;font-weight:700>256</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_color&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#204a87;font-weight:700>null</span><span style=color:#000;font-weight:700>}</span>
</span></span><span style=display:flex><span><span style=color:#000;font-weight:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;Ben&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_number&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#0000cf;font-weight:700>7</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_color&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;red&#34;</span><span style=color:#000;font-weight:700>}</span>
</span></span><span style=display:flex><span><span style=color:#000;font-weight:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;Charlie&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_number&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#204a87;font-weight:700>null</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_color&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;blue&#34;</span><span style=color:#000;font-weight:700>}</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 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 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:700>=</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:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>Schema</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>Parser</span><span style=color:#ce5c00;font-weight:700>().</span><span style=color:#c4a000>parse</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>File</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;user.avsc&#34;</span><span style=color:#ce5c00;font-weight:700>));</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:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>GenericData</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>Record</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>schema</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>user1</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>put</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;name&#34;</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#4e9a06>&#34;Alyssa&#34;</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>user1</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>put</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;favorite_number&#34;</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#0000cf;font-weight:700>256</span><span style=color:#ce5c00;font-weight:700>);</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:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>GenericData</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>Record</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>schema</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>user2</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>put</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;name&#34;</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#4e9a06>&#34;Ben&#34;</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>user2</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>put</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;favorite_number&#34;</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#0000cf;font-weight:700>7</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>user2</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>put</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;favorite_color&#34;</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#4e9a06>&#34;red&#34;</span><span style=color:#ce5c00;font-weight:700>);</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:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>File</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#4e9a06>&#34;users.avro&#34;</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>DatumWriter</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>datumWriter</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>GenericDatumWriter</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>schema</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>DataFileWriter</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>dataFileWriter</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>DataFileWriter</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>datumWriter</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>create</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>schema</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#000>file</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>append</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user1</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>append</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user2</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>dataFileWriter</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>close</span><span style=color:#ce5c00;font-weight:700>();</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:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>datumReader</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>GenericDatumReader</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>schema</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>DataFileReader</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;</span> <span style=color:#000>dataFileReader</span> <span style=color:#ce5c00;font-weight:700>=</span> <span style=color:#204a87;font-weight:700>new</span> <span style=color:#000>DataFileReader</span><span style=color:#ce5c00;font-weight:700>&lt;</span><span style=color:#000>GenericRecord</span><span style=color:#ce5c00;font-weight:700>&gt;(</span><span style=color:#000>file</span><span style=color:#ce5c00;font-weight:700>,</span> <span style=color:#000>datumReader</span><span style=color:#ce5c00;font-weight:700>);</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:700>=</span> <span style=color:#204a87;font-weight:700>null</span><span style=color:#ce5c00;font-weight:700>;</span>
</span></span><span style=display:flex><span><span style=color:#204a87;font-weight:700>while</span> <span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>dataFileReader</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>hasNext</span><span style=color:#ce5c00;font-weight:700>())</span> <span style=color:#ce5c00;font-weight:700>{</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:700>=</span> <span style=color:#000>dataFileReader</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>next</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user</span><span style=color:#ce5c00;font-weight:700>);</span>
</span></span><span style=display:flex><span><span style=color:#000>System</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>out</span><span style=color:#ce5c00;font-weight:700>.</span><span style=color:#c4a000>println</span><span style=color:#ce5c00;font-weight:700>(</span><span style=color:#000>user</span><span style=color:#ce5c00;font-weight:700>);</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:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;Alyssa&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_number&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#0000cf;font-weight:700>256</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_color&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#204a87;font-weight:700>null</span><span style=color:#000;font-weight:700>}</span>
</span></span><span style=display:flex><span><span style=color:#000;font-weight:700>{</span><span style=color:#204a87;font-weight:700>&#34;name&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;Ben&#34;</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_number&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#0000cf;font-weight:700>7</span><span style=color:#000;font-weight:700>,</span> <span style=color:#204a87;font-weight:700>&#34;favorite_color&#34;</span><span style=color:#000;font-weight:700>:</span> <span style=color:#4e9a06>&#34;red&#34;</span><span style=color:#000;font-weight:700>}</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:700>=</span>example.GenericMain
</span></span></code></pre></div><div class=section-index></div><div class="text-muted mt-5 pt-3 border-top">Last modified April 13, 2023: <a href=https://github.com/apache/avro/commit/8b181dcaa392cfc9bf9ed903deb586de9878f938>Fix the path for Java javadoc HTMLs (8b181dc)</a></div></div></main></div></div><footer class="bg-dark py-5 row d-print-none"><div class="container-fluid mx-sm-5"><div class=row><div class="col-4 col-sm-3 text-xs-center order-sm-2"><ul class="list-inline mb-0"><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title="User mailing list" aria-label="User mailing list"><a class=text-white target=_blank rel=noopener href=mailto:user@avro.apache.org aria-label="User mailing list"><i class="fa fa-envelope"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=Twitter aria-label=Twitter><a class=text-white target=_blank rel=noopener href=https://twitter.com/ApacheAvro aria-label=Twitter><i class="fab fa-twitter"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title="Stack Overflow" aria-label="Stack Overflow"><a class=text-white target=_blank rel=noopener href=https://stackoverflow.com/questions/tagged/avro aria-label="Stack Overflow"><i class="fab fa-stack-overflow"></i></a></li></ul></div><div class="col-4 col-sm-3 text-right text-xs-center order-sm-3"><ul class="list-inline mb-0"><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=GitHub aria-label=GitHub><a class=text-white target=_blank rel=noopener href=https://github.com/apache/avro aria-label=GitHub><i class="fab fa-github"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=Issues aria-label=Issues><a class=text-white target=_blank rel=noopener href=https://issues.apache.org/jira/projects/AVRO/issues aria-label=Issues><i class="fab fa-jira"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title="Chat with other project developers at Slack" aria-label="Chat with other project developers at Slack"><a class=text-white target=_blank rel=noopener href=https://the-asf.slack.com/ aria-label="Chat with other project developers at Slack"><i class="fab fa-slack"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title="Developer mailing list" aria-label="Developer mailing list"><a class=text-white target=_blank rel=noopener href=mailto:dev@avro.apache.org aria-label="Developer mailing list"><i class="fa fa-envelope"></i></a></li></ul></div><div class="col-10 col-sm-3 text-center py-2 order-sm-2"><a href=https://www.apache.org/><small class=text-white>&copy; 2023 The Apache Software Foundation </small></a><small class=text-white>All Rights Reserved</small><p><small class=text-white>Apache Avro, Avro&trade;, Apache&reg;, and the Apache feather logo are either registered trademarks or trademarks of The Apache Software Foundation.</small></p></div><div class="col-5 col-sm-3 order-sm-2"><a href=https://www.apache.org/events/current-event.html><img src=https://www.apache.org/events/current-event-234x60.png></a></div></div></div></footer></div><script src=https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js integrity=sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN crossorigin=anonymous></script>
<script src=https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.min.js integrity="sha512-UR25UO94eTnCVwjbXozyeVd6ZqpaAE9naiEUBK/A+QDbfSTQFhPGj5lOR6d8tsgbBk84Ggb5A3EkjsOgPRPcKA==" crossorigin=anonymous></script>
<script src=/js/tabpane-persist.js></script>
<script src=/update-site/js/main.min.b0910468256f44515fad3d1c8b5cf64a439da3abc1acef42ad39b9ceac3ae705.js integrity="sha256-sJEEaCVvRFFfrT0ci1z2SkOdo6vBrO9CrTm5zqw65wU=" crossorigin=anonymous></script>
<script src=/js/prism.js></script></body></html>