【问题标题】:how to create avro schema for nested collections?如何为嵌套集合创建 avro 模式?
【发布时间】:2018-03-13 08:25:15
【问题描述】:

我确实有这样的身体 -

{
    {
        "name":"abc",
        "eventName": "abc",
        "pl": {
             "name": "xyz",
             "age": 21,
             "address": {
                 "line1" : "h.no - ",
                 "line2": "state"
             }
         }
    }
}

谁能帮我为这种身体创建一个有效的 Avro 架构。 我找到了一个创建这样的嵌套模式的示例 -

[
{
"type": "record",
"name": "Address",
"fields": [
  {
    "name": "streetaddress",
    "type": "string"
  },
  {
    "name": "city",
    "type": "string"
  }
]
 },


 {
"type": "record",
"name": "person",
"fields": [
  {
    "name": "firstname",
    "type": "string"
  },
  {
    "name": "lastname",
    "type": "string"
  },
  {
    "name": "address",
    "type": "Address"
  }
]

} ]

当我提供这种模式时,它在下面的行给我提供了错误 -

GenericRecord avroRecord = new GenericData.Record(schema);

错误是 - org.apache.avro.AvroRuntimeException:不是记录架构:

【问题讨论】:

    标签: java avro jsonschema


    【解决方案1】:

    您可以像这样定义您的记录以支持嵌套对象。

    {
    
        "type": "record",
        "name": "person",
        "fields": [{
                "name": "firstname",
                "type": "string"
            }, {
                "name": "lastname",
                "type": "string"
            }, {
                "name": "address",
                "type": {
                    "type": "record",
                    "name": "Address",
                    "fields": [{
                            "name": "streetaddress",
                            "type": "string"
                        }, {
                            "name": "city",
                            "type": "string"
                        }
                    ]
                }
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-12
      • 1970-01-01
      • 2012-07-30
      • 2019-05-25
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      相关资源
      最近更新 更多