【问题标题】:Avro Schema format Exception - "record" is not a defined nameAvro Schema 格式异常 - “记录”不是定义的名称
【发布时间】:2017-09-16 17:03:42
【问题描述】:

我正在尝试使用这个 avro shcema

{
  "namespace": "nothing",
  "name": "myAvroSchema",
  "type": "record",
  "fields": [
    {
      "name": "checkInCustomerReference",
      "type": "string"
    },
    {
      "name": "customerContacts",
      "type": "record",
      "fields": [
        {
          "name": "customerEmail",
          "type": "array",
          "items": {
            "type": "record",
            "name": "customerEmail_element",
            "fields": [
              {
                "name": "emailAddress",
                "type": "string"
              },
              {
                "name": "typeOfEmail",
                "type": "string"
              }
            ]
          }
        },
        {
          "name": "customerPhone",
          "type": "array",
          "items": {
            "type": "record",
            "name": "customerPhone_element",
            "fields": [
              {
                "name": "fullContactNumber",
                "type": "string"
              },
              {
                "name": "ISDCode",
                "type": "string"
              }
            ]
          }
        },
        {
          "name": "DonotAskIndicator",
          "type": "record",
          "fields": [
            {
              "name": "donotAskDetails",
              "type": "string"
            }
          ]
        }
      ]
    },
    {
      "name": "somethingElseToCheck",
      "type": "string"
    }
  ]
}

使用 avro 工具生成和 avro 文件:

avro-tools fromjson --schema-file myAvroSchema.avsc myJson.json > myAvroData.avro

但我收到以下错误消息:

线程“主”org.apache.avro.SchemaParseException 中的异常: “记录”不是一个定义的名称。 “customerContacts”的类型 字段必须是定义的名称或 {"type": ...} 表达式。

谁能告诉我为什么记录没有被识别为定义的名称?

【问题讨论】:

    标签: json avro avro-tools


    【解决方案1】:

    “customerContacts”字段的类型必须是定义的名称或 {"type": ...} 表达式

    看起来您没有正确定义嵌套记录。我复制了您的架构并提出了这个,试一试:

    {  
        "type":"record",
        "name":"myAvroSchema",
        "namespace":"nothing",
        "fields":[  
            {  
                "name":"checkInCustomerReference",
                "type":"string"
            },
            {  
                "name":"customerContacts",
                "type":{  
                    "type":"record",
                    "name":"customerContacts",
                    "namespace":"nothing",
                    "fields":[  
                        {  
                            "name":"customerEmail",
                            "type":{  
                                "type":"array",
                                "items":{  
                                    "type":"record",
                                    "name":"customerEmail",
                                    "namespace":"nothing",
                                    "fields":[  
                                        {  
                                            "name":"emailAddress",
                                            "type":"string"
                                        },
                                        {  
                                            "name":"typeOfEmail",
                                            "type":"string"
                                        }
                                    ]
                                }
                            }
                        },
                        {  
                            "name":"customerPhone",
                            "type":{  
                                "type":"array",
                                "items":{  
                                    "type":"record",
                                    "name":"customerPhone",
                                    "namespace":"nothing",
                                    "fields":[  
                                        {  
                                            "name":"fullContactNumber",
                                            "type":"string"
                                        },
                                        {  
                                            "name":"ISDCode",
                                            "type":"string"
                                        }
                                    ]
                                }
                            }
                        },
                        {  
                            "name":"DonotAskIndicator",
                            "type":{  
                                "type":"record",
                                "name":"donotAskIndicator",
                                "namespace":"nothing",
                                "fields":[  
                                    {  
                                        "name":"donotAskDetails",
                                        "type":"string"
                                    }
                                ]
                            }
                        }
                    ]
                }
            },
            {  
                "name":"somethingElseToCheck",
                "type":"string"
            }
        ]
    }
    

    【讨论】:

    • 谢谢! Avro 的好方法,“type”属性具有不同的含义和类型,具体取决于它在 Avro 模式中的使用位置:-/
    • ...为此它禁止以数字开头的有效 JSON 属性名称。
    【解决方案2】:

    您必须在使用前定义记录。您可以在此处找到awser(示例): Problems in creating scheme .avsc Avro

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2012-07-20
      相关资源
      最近更新 更多