Search code examples
avro

Avro Schema failure


I have the following object:

[
{"ProductId":10,"ProductName":"some name"}
]

Bu sometimes I have just null. I am trying to come up with a schema for this. But it doesn't seem to work :(.

I tried the following two:

{
    "name": "ProductsOrNull", 
    "type": ["null", {
      "type": "array",
      "name": "Products",
      "items": {
        "type":"record",
        "name": "Product",
        "fields": [{
            "name":"ProductId",
            "type":"long"
          }, {
            "name":"ProductName",
            "type":"string"
          }
        ]
      }
    }],
    "default": null
}

But it fails with Exception in thread "main" org.apache.avro.SchemaParseException: No type: <...> when running java -jar avro-tools-1.8.2.jar fromjson --schema-file prod.avsc prod.json > lol.avro.

I also trie this with the same error:

{
  "type": ["null", "array"],
  "name": "Products",
  "items": {
    "type":"record",
    "name": "Product",
    "namespace": "{{ dataModelSchema }}",
    "fields": [{
        "name":"ProductId",
        "type":"long"
      }, {
        "name":"ProductName",
        "type":"string"
      }
    ]
  }
}

I don't really understand where the problem is and what is the difference between the two.


Solution

  • Base on your schemas, your json object should look like

    {
    "ProductsOrNull" : 
    [
    {"ProductId":10,"ProductName":"some name"}
    ] 
    }
    

    There is no way to define and avro schema with an array without a field name.