Search code examples
javaavro

How to include more than one record in Avro schema?


I am new to Apache Avro. I am serializing the data by reading the schema using Parsers. The below details includes my schema. I need to include more than one record in the same schema.

{  "namespace": "tutorial.model",
   "type": "record",
   "name": "Employee",
   "fields": [
               {"name": "firstName", "type": "string"},
               {"name": "lastName", "type": "string"},
               {"name": "age",  "type": "int"},
               {"name": "id",  "type": "string"},
               {"name" : "company", "type" : "string"}
             ]
}

Solution

  • You can define embedded records as explained here GettingStartedGuide.

    So, your schema would be something like this

    {
        "type" : "record",
        "name" : "userInfo",
        "namespace" : "my.example",
        "fields" : [{"name" : "username", 
                     "type" : "string", 
                     "default" : "NONE"},
    
                    {"name" : "age", 
                     "type" : "int",
                     "default" : -1},
    
                     {"name" : "phone", 
                      "type" : "string", 
                      "default" : "NONE"},
    
                     {"name" : "housenum", 
                      "type" : "string", 
                      "default" : "NONE"},
    
                      {"name" : "address", 
                       "type" : {
                             "type" : "record",
                             "name" : "mailing_address",
                             "fields" : [
                                {"name" : "street", 
                                 "type" : "string", 
                                 "default" : "NONE"},
    
                                {"name" : "city", 
                                 "type" : "string", 
                                 "default" : "NONE"},
    
                                {"name" : "state_prov", 
                                 "type" : "string", 
                                 "default" : "NONE"},
    
                                {"name" : "country", 
                                 "type" : "string", 
                                 "default" : "NONE"},
    
                                {"name" : "zip", 
                                 "type" : "string", 
                                 "default" : "NONE"}
                              ]},
                              "default" : {}
                    }
        ]
    }