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 17:28:10 GMT


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