Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Evolving an Avro Schema in a Kafka Application

Confluent Schema Registry is a useful tool for coordinating contracts between producers and consumers, as well as simplifies the process of serializing and deserializing complex data objects. However, it also provides some powerful functionality to help you manage changes to your data schemas. In this lab, you will have the opportunity to make a change to an existing schema by adding a new field. This will give you some hands-on experience with the process of evolving a schema using the Confluent Schema Registry.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Oct 18, 2019

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Clone the Starter Project and Run It to Make Sure Everything Is Working

    1. Clone the starter project into your home directory:
    cd ~/
    git clone https://github.com/linuxacademy/content-ccdak-schema-evolve-lab.git
    
    1. Run the code to ensure it works before modifying it:
    cd content-ccdak-schema-evolve-lab
    ./gradlew runProducer
    ./gradlew runConsumer
    

    Note: The consumer should output some records that were created by the producer.

  2. Challenge

    Update the Purchase Schema to Add the `member_id` Field

    1. Edit the schema definition file:
    vi src/main/avro/com/linuxacademy/ccdak/schemaregistry/Purchase.avsc
    
    1. Add the member_id field with a blank default:
    {
      "namespace": "com.linuxacademy.ccdak.schemaregistry",
      "compatibility": "FORWARD",
      "type": "record",
      "name": "Purchase",
      "fields": [
        {"name": "id", "type": "int"},
        {"name": "product", "type": "string"},
        {"name": "quantity", "type": "int"},
        {"name": "member_id", "type": "int", "default": 0}
      ]
    }
    
  3. Challenge

    Update the Producer to Set the `member_id` for the Records It Publishes

    1. Edit the schema definition file:
    vi src/main/avro/com/linuxacademy/ccdak/schemaregistry/Purchase.avsc
    
    1. Add the member_id field with a blank default:
    {
      "namespace": "com.linuxacademy.ccdak.schemaregistry",
      "compatibility": "FORWARD",
      "type": "record",
      "name": "Purchase",
      "fields": [
        {"name": "id", "type": "int"},
        {"name": "product", "type": "string"},
        {"name": "quantity", "type": "int"},
        {"name": "member_id", "type": "int", "default": 0}
      ]
    }
    

    Update the Producer to Set the member_id for the Records It Publishes

    1. Edit the producer Main class:
    vi src/main/java/com/linuxacademy/ccdak/schemaregistry/ProducerMain.java
    
    1. Implement the new member_id field in the producer by setting it for the records being produced:
    package com.linuxacademy.ccdak.schemaregistry;
    
    import io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig;
    import io.confluent.kafka.serializers.KafkaAvroSerializer;
    import java.util.Properties;
    import org.apache.kafka.clients.producer.KafkaProducer;
    import org.apache.kafka.clients.producer.ProducerConfig;
    import org.apache.kafka.clients.producer.ProducerRecord;
    import org.apache.kafka.common.serialization.StringSerializer;
    
    public class ProducerMain {
    
        public static void main(String[] args) {
            final Properties props = new Properties();
            props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
            props.put(ProducerConfig.ACKS_CONFIG, "all");
            props.put(ProducerConfig.RETRIES_CONFIG, 0);
            props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class);
            props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081");
    
            KafkaProducer<String, Purchase> producer = new KafkaProducer<String, Purchase>(props);
    
            Purchase apples = new Purchase(1, "apples", 17, 77543);
            producer.send(new ProducerRecord<String, Purchase>("inventory_purchases", apples.getId().toString(), apples));
    
            Purchase oranges = new Purchase(2, "oranges", 5, 56878);
            producer.send(new ProducerRecord<String, Purchase>("inventory_purchases", oranges.getId().toString(), oranges));
    
            producer.close();
        }
    
    }
    
    1. Run the producer:
    ./gradlew runProducer
    
    1. Run the consumer:
    ./gradlew runConsumer
    
    1. Verify the data in the output file. We should see the new member_id data in the last lines of the file:
    cat /home/cloud_user/output/output.txt
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans