Avro-Tools JSON to Avro Schema fails: org.apache.avro.SchemaParseException: Undefined name:

StackOverflow https://stackoverflow.com/questions/22443051

  •  15-06-2023
  •  | 
  •  

Domanda

I am trying to create two Avro schemas using the avro-tools-1.7.4.jar create schema command.

I have two JSON schemas which look like this:

{
 "name": "TestAvro",
 "type": "record",
 "namespace": "com.avro.test",
 "fields": [
    {"name": "first",     "type": "string"},
    {"name": "last",      "type": "string"},
    {"name": "amount",     "type": "double"} 
 ]
}


{
 "name": "TestArrayAvro",
 "type": "record",
 "namespace": "com.avro.test",
 "fields": [
    {"name": "date",     "type": "string"},
    {"name": "records",  "type":      
           {"type":"array","items":"com.avro.test.TestAvro"}}
    ]
 }

When I run the create schema on these two files the first one works fine and generates the java. The second one fails every time. It does not like the array items when I try and use the first Schema as the type. This is the error I get:

Exception in thread "main" org.apache.avro.SchemaParseException: Undefined name: "com.test.avro.TestAvro"
at org.apache.avro.Schema.parse(Schema.java:1052)

Both files are located in the same path directory.

È stato utile?

Soluzione

Use the below avsc file:

[{
    "name": "TestAvro",
    "type": "record",
    "namespace": "com.avro.test",
    "fields": [
        {
            "name": "first",
            "type": "string"
        },
        {
            "name": "last",
            "type": "string"
        },
        {
            "name": "amount",
            "type": "double"
        }
    ]
},
{
    "name": "TestArrayAvro",
    "type": "record",
    "namespace": "com.avro.test",
    "fields": [
        {
            "name": "date",
            "type": "string"
        },
        {
            "name": "records",
            "type": {
                "type": "array",
                "items": "com.avro.test.TestAvro"
            }
        }
    ]
}]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top