Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to use this 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"
    }
  ]
}

To generate and avro file using the avro-tools:

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

But I am getting the following error message:

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.

Can anyone tell me why record is not identified as a defined name?

like image 390
Ignacio Alorre Avatar asked Apr 20 '17 07:04

Ignacio Alorre


People also ask

Is namespace mandatory in Avro schema?

@johndcal A namespace is not required within the avro schema source in Schema Registry.

What is an Avro record?

Avro is an open source data serialization system that helps with data exchange between systems, programming languages, and processing frameworks. Avro helps define a binary format for your data, as well as map it to the programming language of your choice.

What is default in Avro schema?

In this case, the type of the key1 field is Union (type: [null, string] in Avro Schema). If the key1 field in the source data is not transferred or the transferred value is null, null is automatically filled as the default value.

Does Avro file contains schema?

Avro uses a schema to structure the data that is being encoded. It has two different types of schema languages; one for human editing (Avro IDL) and another which is more machine-readable based on JSON.


1 Answers

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

Doesn't look like your defining your nested records properly. I reproduced your schema and came out with this, give it a try:

{  
    "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"
        }
    ]
}
like image 167
JSteward Avatar answered Sep 20 '22 00:09

JSteward