Avro 架构格式异常 - "record" 不是定义的名称

Avro Schema format Exception - "record" is not a defined name

我正在尝试使用这个 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-tools 生成 avro 文件:

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

但我收到以下错误消息:

Exception in thread "main" org.apache.avro.SchemaParseException: "record" is not a defined name. The type of the "customerContacts" field must be a defined name or a {"type": ...} expression.

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

您必须先定义记录才能使用它。您可以在这里找到答案(示例): Problems in creating scheme .avsc Avro

The type of the "customerContacts" field must be a defined name or a {"type": ...} expression

您似乎没有正确定义嵌套记录。我复制了你的模式并得出了这个,试一试:

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