flybonzai
flybonzai

Reputation: 3931

Creating an avro schema for an array with multiple record types?

I am creating an avro schema for a JSON payload that appear to have an array of multiple objects. I'm not sure exactly how to represent this in the schema. The key in question is content:

{
  "id": "channel-id",
  "name": "My Channel with a New Title",
  "description": "Herpy me derpy merpus herpsum ner berp berps derp ter tee",
  "privacyLevel": "<private|org>",
  "planId": "some-plan-id",
  "owner": "a-user-handle",
  "curators": [
    "user-handle-1",
    "user-handle-2"
  ],
  "members": 5,
  "content": [
    {
      "id": "docker",
      "slug": "docker",
      "index": 1,
      "type": "path"
    },
    {
      "id": "such-linkage",
      "slug": "such-linkage",
      "index": 2,
      "type": "external-link",
      "details": {
        "url": "http://some-dank-link.com",
        "title": "My Dank Link",
        "contentType": "External Link",
        "level": "Beginner",
        "duration": "PT34293H33M9S"
      }
    },
    {
      "id": "21f1e812-b10a-40df-8b52-3a1d05fc215c",
      "slug": "windows-azure-storage-in-depth",
      "index": 3,
      "type": "course"
    },
    {
      "id": "7c346c05-6416-42dd-80b2-d5e758de7926",
      "slug": "7c346c05-6416-42dd-80b2-d5e758de7926",
      "index": 4,
      "type": "project"
    }
  ],
  "imageUrls": ["https://url/to/an/image", "https://url/to/another/image"],
  "analyticsEnabled": true,
  "orgDiscoverable": false,
  "createdDate": "2015-12-31T01:23:45+00:00",
  "archiveDate": "2015-12-31T01:23:45+00:00",
  "messagePublishedAt": "2015-12-31T01:23:45+00:00"
}

Upvotes: 3

Views: 7794

Answers (2)

jcompetence
jcompetence

Reputation: 8383

If you already have the records defined previously, then it could look like this:

          {
            "name": "mulitplePossibleTypes",
            "type": [
              "null",
              {
                "type": "array",
                "items": [
                  "com.xyz.kola.cloud.events.itemmanager.Part",
                  "com.xyz.kola.cloud.events.itemmanager.Document",
                  "com.xyz.kola.cloud.events.itemmanager.DigitalModel",
                  "com.xyz.kola.cloud.events.itemmanager.Interface"
                ]
              }
            ]
          },

Upvotes: 1

hlagos
hlagos

Reputation: 7947

If you are asking if it is possible create an array with different kind of records, it is. Avro support this through union. it would looks like .

{
    "name": "myRecord",
    "type":"record",
    "fields":[
        {
            "name":"myArrayWithMultiplesTypes",
            "type":{
                "type": "array",  
                "items":[
                {
                    "name":"typeOne",
                    "type":"record",
                    "fields":[
                        {"name":"name", "type":"string"}
                    ]
                },
                {
                    "name":"typeTwo",
                    "type":"record",
                    "fields":[
                        {"name":"id", "type":"int"}
                    ]
                }
                ]
            }
        }
    ] 
}

Upvotes: 10

Related Questions