如何在Avro模式中声明对象类型的实体

时间:2019-01-14 22:15:28

标签: schema avro

我有一个JSON对象,其一部分如下:

{
  "bounding_box": {
    "coordinates": [
      [
        [
          -74.026675,
          40.683935
        ],
        [
          -74.026675,
          40.877483
        ]
      ]
    ],
    "type": "Polygon"
  }
}

在这里,坐标以对象数组的形式发送。现在,对于这个JSON对象,我想创建avro模式(.avsc文件),到目前为止,它如下:

{
    "name": "bounding_box",
    "type": {
        "namespace": "my.tweet.stream",
        "type": "record",
        "name": "BoundingBox",
        "fields": [{
                "name": "coordinates",
                "type": {
                    "type": "array",
                    "items": "object"
                }
            },
            {
                "name": "type",
                "type": ["string", "null"]
            }
        ]
    }
}

但是,在当前架构下,出现以下错误:

  

目标org.apache.avro:avro-maven-plugin:1.8.1:schema的执行生成ID:未定义名称:“对象”

有人可以帮忙,如何在avro模式中指定java.lang.Object类型?

谢谢。

1 个答案:

答案 0 :(得分:1)

Avro是跨语言的,因此没有java.lang.Object映射,只有record类型可以嵌套。

您可以嵌套数组(我只做过两个级别,但您应该可以拥有更多级别)

在IDL(payload.avdl)中

@namespace("com.example.mycode.avro")
protocol ExampleProtocol {
  record BoundingBox {
    array<array<double>> coordinates;
   }

  record Payload {
    BoundingBox bounding_box;
    union{null, string} type = null;
  }
}

或者在AVSC中

java -jar ~/Applications/avro-tools-1.8.1.jar idl2schemata payload.avdl

生成
{
  "type" : "record",
  "name" : "Payload",
  "namespace" : "com.example.mycode.avro",
  "fields" : [ {
    "name" : "bounding_box",
    "type" : {
      "type" : "record",
      "name" : "BoundingBox",
      "fields" : [ {
        "name" : "coordinates",
        "type" : {
          "type" : "array",
          "items" : {
            "type" : "array",
            "items" : "double"
          }
        }
      } ]
    }
  }, {
    "name" : "type",
    "type" : [ "null", "string" ],
    "default" : null
  } ]
}
相关问题