将Avro-Tools JSON转换为Avro Schema失败:org.apache.avro.SchemaParseException:未定义的名称:

med*_*ium 4 avro

我正在尝试使用av​​ro-tools-1.7.4.jar create schema命令创建两个Avro模式。

我有两个看起来像这样的JSON模式:

{
 "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"}}
    ]
 }
Run Code Online (Sandbox Code Playgroud)

当我在这两个文件上运行创建模式时,第一个可以正常工作并生成Java。第二个每次都会失败。当我尝试使用第一个Schema作为类型时,它不喜欢数组项。这是我得到的错误:

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

这两个文件位于同一路径目录中。

Pri*_*mes 7

使用以下avsc文件:

[{
    "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"
            }
        }
    ]
}]
Run Code Online (Sandbox Code Playgroud)

  • 对于要编译的第二个模式,应该已经创建了 TestAvro 类并且 avro 解析器应该知道它。在单个模式文件中创建记录数组是一种更简单的解决方案。使用导入也可以实现相同的目的。 (2认同)