【问题标题】:Avro-Tools JSON to Avro Schema fails: org.apache.avro.SchemaParseException: Undefined name:Avro-Tools JSON 到 Avro Schema 失败:org.apache.avro.SchemaParseException:未定义名称:
【发布时间】:2014-10-09 19:12:07
【问题描述】:

我正在尝试使用 avro-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"}}
    ]
 }

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

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

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

【问题讨论】:

    标签: avro


    【解决方案1】:

    使用下面的 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"
                }
            }
        ]
    }]
    

    【讨论】:

    • 对于要编译的第二个模式,应该已经创建了 TestAvro 类并且 avro 解析器应该知道它。在单个模式文件中创建记录数组是一种更简单的解决方案。使用导入也可以实现相同的目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2021-05-27
    • 2019-12-21
    • 2017-09-16
    • 2019-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多