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

7

我正在尝试使用avro-tools-1.7.4.jar创建模式命令创建两个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"}}
    ]
 }

当我在这两个文件上运行create schema时,第一个文件可以正常工作并生成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)

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

1个回答

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

2
为了编译第二个模式,TestAvro类应该已经被创建,并且avro解析器应该知道它。在单个模式文件中创建记录数组是一个更简单的解决方案。同样的效果也可以通过导入实现。 - Princey James

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接