Define Avro Schema with default values

How to define Avro Schema with Default values

 

Lets define a Employee Record :

{
    "name": "com.big.data.avro.schema.Employee",
    "type": "record",
    "fields": [{
            "name": "emp_id",
            "type": "int",
            "doc": "employee Id of the employee"
        },
        {
             "name": "emp_name",
             "type": "string",
             "doc": "employee name of the employee"
        },
        {
              "name": "emp_country",
              "type": "string",
              "doc": "country of residence"
        },
        {      "name": "bonus",
               "type": ["null", "long"],
               "default": null,
               "doc": "bonus received on a yearly basis"
        },
       {
               "name": "subordinates",
               "type": ["null", {"type": "map", "values": "string"}],
               "default": null,
               "doc": "map of subordinates Name and Designation"
        },
        {
               "name": "departments",
               "type":["null", {"type":"array", "items":"string" }],
               "default":null,
               "doc": "Departments under the employee"
         }
        ]
 }

[addToAppearHere]
Field Data Type Required/Optional Default Values
emp_id int Required
emp_name String Required
emp_country String Required
bonus long Required null
subordinates null, map Optional null/ {}
departments null. array Optional null / []

 

 

To convert the Avro definition into a Java Object

 

<dependencies>
    <dependency>
        <groupId>org.apache.avro</groupId>
        <artifactId>avro</artifactId>
        <version>1.8.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.avro</groupId>
            <artifactId>avro-maven-plugin</artifactId>
            <version>1.8.1</version>
        </plugin>
    </plugins>
</build>