Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare an entity of object type in avro schema

Tags:

schema

avro

I have a JSON object, a part of which is as follows:

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

Here, the coordinates are sent as an array of Object. Now, for this JSON object, I want to create the avro schema (.avsc file), which is, as of now, as follows:

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

However, with the current schema, I am getting the following error:

Execution generate-id of goal org.apache.avro:avro-maven-plugin:1.8.1:schema failed: Undefined name: "object"

Could someone help, how can I specify java.lang.Object type in avro schema?

Thanks.

like image 822
PankajK Avatar asked Oct 27 '25 09:10

PankajK


1 Answers

Avro is cross-language, and so has no java.lang.Object mapping, only record types, which can be nested.

You can nest arrays (I only did two levels, but you should be able to have more)

In 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;
  }
}

Or in AVSC

generated with 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
  } ]
}
like image 104
OneCricketeer Avatar answered Oct 30 '25 15:10

OneCricketeer