{"id":356,"date":"2016-03-04T17:37:39","date_gmt":"2016-03-04T23:37:39","guid":{"rendered":"http:\/\/sha.nnoncarey.com\/blog\/?p=356"},"modified":"2016-03-04T17:42:39","modified_gmt":"2016-03-04T23:42:39","slug":"avro-schemas-in-multiple-files","status":"publish","type":"post","link":"https:\/\/sha.nnoncarey.com\/blog\/archives\/356","title":{"rendered":"Avro Schemas in Multiple Files"},"content":{"rendered":"<p>Please don&#8217;t follow the advice given in <a href=\"http:\/\/www.infoq.com\/articles\/ApacheAvro\">the InfoQ article on building an Avro schema up from multiple files<\/a>. The article recommends doing string replacement to mutate the schemas in order to combine them. The article was written in 2011, and clearly there have been some improvements to Avro since then.<\/p>\n<p>A better (I&#8217;m not sure if it&#8217;s the best) way to do this, assuming you don&#8217;t want to or can&#8217;t use .avdl files, is to parse your various files into the same Schema.Parser object. It will give you a map of type name to Schema object:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n        List&lt;String&gt; schemaResourceNames = Arrays.asList(&quot;avro\/foo.avsc&quot;, &quot;avro\/bar.avsc&quot;);\r\n\r\n        Schema.Parser parser = new Schema.Parser();\r\n        for (String schemaResourceName : schemaResourceNames) {\r\n            try (InputStream schemaInputStream = classLoader.getResourceAsStream(schemaResourceName)) {\r\n                if (schemaInputStream == null) {\r\n                    throw new RuntimeException(&quot;Resource not found &quot; + schemaResourceName);\r\n                }\r\n                parser.parse(schemaInputStream);\r\n            }\r\n        }\r\n        return parser.getTypes();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Please don&#8217;t follow the advice given in the InfoQ article on building an Avro schema up from multiple files. The article recommends doing string replacement to mutate the schemas in order to combine them. The article was written in 2011, and clearly there have been some improvements to Avro since then. A better (I&#8217;m not &hellip; <a href=\"https:\/\/sha.nnoncarey.com\/blog\/archives\/356\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Avro Schemas in Multiple Files&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-356","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/comments?post=356"}],"version-history":[{"count":4,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/356\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/posts\/356\/revisions\/361"}],"wp:attachment":[{"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/media?parent=356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/categories?post=356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sha.nnoncarey.com\/blog\/wp-json\/wp\/v2\/tags?post=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}