avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Scott Carey <sc...@richrelevance.com>
Subject Re: Schema for array of records
Date Mon, 21 Mar 2011 19:47:22 GMT
If you want to generate Java source code based on the schema, you need to
use the Specific API and SpecificCompiler.
There are ant and maven plugins for integration in your build process.
http://avro.apache.org/docs/1.5.0/api/java/org/apache/avro/specific/package
-summary.html

Doug described how to use the Generic API, which uses GenericRecord
objects to represent all record schema types.  See also GenericData.
http://avro.apache.org/docs/1.5.0/api/java/org/apache/avro/generic/package-
summary.html

The Reflect API is a third option.
http://avro.apache.org/docs/1.5.0/api/java/org/apache/avro/reflect/package-
summary.html



On 3/21/11 10:47 AM, "Curtis Jensen" <curtis@the-jensens.org> wrote:

>In one of my iterations of trying to get this to work, I tried the
>straight array schema you suggested.  However, in Java, how do I
>create a "User" record?  After parsing the schema, the only schema
>object I can create from it is an Array object.  How do I populate the
>array?
>
>Thanks,
>Curtis
>
>
>On Mon, Mar 21, 2011 at 10:28 AM, Scott Carey <scott@richrelevance.com>
>wrote:
>>
>>
>> On 3/21/11 8:28 AM, "Curtis Jensen" <curtis@the-jensens.org> wrote:
>>
>>>How does one create a schema for an array of records?
>>>
>>>For example, I have a record schema for a user (name and id; see
>>>below).  I would like to serialize an array of users.  I've tried
>>>various combinations of things, and I'm not getting it to work.
>>>
>>>Thanks,
>>>Curtis
>>>
>>
>> The below Schema is a UNION of "User" and an array of "User". (I' am
>> ignoring the trailing unmatched "}")
>>
>>
>>>
>>> [
>>> {
>>> "name": "User",
>>> "type": "record",
>>> "fields": [
>>>   {"name": "name", "type": "string"},
>>>   {"name": "id", "type": "int"}]
>>> },
>>> {
>>> "type": "array", "items": "User"
>>> }
>>> ]}
>>
>> If you want only an array of Users, do:
>>
>> {
>>  "type": "array", "items": {
>>    "name": "User",
>>    "type": "record",
>>    "fields": [
>>      {"name": "name", "type": "string"},
>>      {"name": "id", "type": "int"}
>>    ]
>>  }
>> }
>>
>>
>>
>> If you want the array to be a field inside of a "Users" record, do:
>> {
>>  "type": "record", "name":"Users", "fields": [
>>  "name": "users", "type":
>>    {"type": "array", "items": {
>>      "name": "User",
>>      "type": "record",
>>      "fields": [
>>        {"name": "name", "type": "string"},
>>        {"name": "id", "type": "int"}
>>      ]
>>    }
>>  ]
>> }
>>
>>
>>
>> Note, that all fields have a name and type; and a fields type is either
>> declared by nesting in { } or with an already defined name.  Arrays do
>>not
>> have names, so they cannot be referenced with one.
>>
>>
>>


Mime
View raw message