为具有多种记录类型的数组创建avro架构?

时间:2018-05-03 20:27:23

标签: avro

我正在为JSON有效负载创建一个avro架构,它似乎有一个包含多个对象的数组。我不确定如何在架构中表示这一点。有问题的关键是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"
}

1 个答案:

答案 0 :(得分:3)

如果你问是否有可能创建一个包含不同类型记录的数组,那就是。 Avro通过工会支持这一点。看起来会像。

{
    "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"}
                    ]
                }
                ]
            }
        }
    ] 
}