concept Avro in category kafka

appears as: Avro
Kafka in Action

This is an excerpt from Manning's book Kafka in Action.

Listing 3.8. Avro schema example
{
    "type" : "record",            #1
    "name" : "contactDetails",
    "namespace" : "avro.example",
    "fields" : [{"name" : "fullname",
                 "type" : "string",
                 "default" : "NONE"},

                {"name" : "age",  #2
                 "type" : "int",  #3
                 "default" : -1}, #4

                 {"name" : "cell_number",
                  "type" : "string",
                  "default" : "NONE"},

                  {"name" : "address",
                   "type" : {
                         "type" : "record",
                         "name" : "home_address",
                         "fields" : [
                            {"name" : "street",
                             "type" : "string",
                             "default" : "NONE"},

                            {"name" : "city",
                             "type" : "string",
                             "default" : "NONE"},

                            {"name" : "state_prov",
                             "type" : "string",
                             "default" : "NONE"},

                            {"name" : "zip",
                             "type" : "string",
                             "default" : "NONE"}
                          ]},
                          "default" : {}
                }
    ]
}

Now that we have discussed some of the advantages of using a schema, why would we look at Avro? First of all, Avro always is serialized with its schema. While not a schema itself, Avro supports schemas when reading and writing data and can apply rules to handle schemas that can change over time. In addition, if you have ever seen JSON it is pretty easy to understand Avro. Besides the data itself, the schema language itself is defined in JSON as well. The ease of readability does not have the same storage impacts of JSON however. A binary representation is used for efficient storage. An interesting point is that Avro data is serialized with its schema. If the schema changes, the old and new schema are present when processing data, so differences can be handled.

Figure 3.6. Avro Flow
Avro Flow

Another benefit for looking at Avro is the popularity of its usage. I first saw its usage on various Hadoop efforts and can be used in many other applications as well. Bindings exist for many programming languages and should not be hard to find in general. For those that have past bad experiences and prefer to avoid generated code, it can be used in a dynamic manner.

Let’s get started with how we are going to use Avro by adding it to our pom.xml. If you are not used to the pom.xml or Maven, you can find this file at the root of your project.