Get Started Free
‹ Back to courses
course: Schema Registry 101

The Schema Workflow

4 min
Danica Fine

Danica Fine

Senior Developer Advocate (Presenter)

Understanding the Schema Registry Workflow

The workflow of Schema Registry includes:

  • Writing a schema file
  • Adding schema files to a project
  • Leveraging schema tools and plugins—Maven and Gradle
  • Configuring Schema Registry plugins
  • Generating the model objects from a schema
  • Locating and exploring the generated files

In this module you will learn about the workflow of using schemas. This workflow starts with writing schema files, adding them to a project, and leveraging tools such as Maven and Gradle to generate the model objects that schemas represent as well as register and update them in a schema registry.

Writing a Schema File – Protobuf

sr101-m3-01

Let’s first look at a schema defined using the Protobuf format.

  • The first line declares the version of Protocol Buffers to use. This course uses version 3.
  • The package declaration creates a name space for proto files to prevent name clashes. It ends up being the package name in the generated Java class unless you specify an option java_package in the proto file.
  • The option java_outer_classname field describes the name of the generated java file
  • Protocol Buffers define the message in the schema as an inner class in the generated file

More detail of how Protobuf works is in the next module.

Writing a Schema File – Avro

sr101-m3-02

Next, let’s examine what the same schema looks like using the Avro format. Notice that it’s defined in JSON format.

  • The namespace field serves the same purpose as in the Protobuf file, preventing name collisions. The namespace also becomes the package name for the generated Java file.
  • Fields are declared in a JSON array.

More detail about Avro is in the next module as well.

Placing Schema Files in a Project

sr101-m3-03

The schema files you write need to go in your project somewhere. In this course the Avro schema files go in the src/main/avro directory and Protobuf files land in src/main/proto.

Available Plugins

Avro – Gradle and Maven plugins

Protobuf – Gradle and Maven plugins

Once you’ve written your schema files you will want to generate the model/data objects from the schema files. Fortunately, there are plugins available, either Maven or Gradle, that can integrate into your local build.

This course focuses on how to configure and use the Gradle plugin. In the upcoming exercise you will configure a build.gradle file to use with Avro and Protobuf schemas.

We should note that Schema Registry supports JSON Schema as well, but that’s more of a specification while Avro and Protobuf are full serialization platforms. As a result the tooling is much better for these two versus JSON Schema. JSON Schema will be covered when we go in-depth on the schema types, but the rest of the course sticks to either Avro or Protocol Buffers.

Plugin Configuration

sr101-m3-05

Let’s now do a quick walkthrough of a Gradle configuration file. The plugins section is where you identify the plugins that will be used when running Gradle scripts. This example includes plugins required for working with Schema Registry.

Protobuf Plugin Configuration

sr101-m3-06

Here’s how the Protobuf plugin is set up. The generatedFilesBaseDir points to the directory where the generated files land.

The upcoming exercise will use the Avro plugin defaults so the gradle.build file doesn’t contain configuration settings for it.

Schema Registry Configuration

sr101-m3-07

Here you have the schemaRegistry configuration block for the Gradle schema registry plugin.

  • The url field is set to the endpoint address for the Scheme Registry
  • The credentials block includes authentication details that allow connectivity with Schema Registry in Confluent Cloud. The credentials included in requests sent to Schema Registry need to be in the form API Key:API Secret. The Gradle schema registry plugin will form these credentials using the values set for <username> and <password>. This will result in <username>:<password> being included in the request.
  • The register block contains all the information the plugin uses to register a schema.

Generated Files in a Project

sr101-m3-08

To generate the objects from schema files, run the ./gradlew clean build command. This will generate the files seen in this illustration. Note that the files shown here don’t necessarily reflect what you will see when you go through the exercise that follows.

Use the promo code SCHEMA101 & CONFLUENTDEV1 to get $25 of free Confluent Cloud usage and skip credit card entry.

Be the first to get updates and new content

We will only share developer content and updates, including notifications when new content is added. We will never send you sales emails. 🙂 By subscribing, you understand we will process your personal information in accordance with our Privacy Statement.

The Schema Workflow

Hi, I'm Danica Fine. Welcome back to the Schema Registry course. In this Schema Registry module, we'll explore the basic workflow you'll take when working with schemas. This involves writing schema files and generating the model objects that they represent. You'll also learn how to use plugins for Maven and Gradle that make it easier for you to work with schemas. Let's get started by writing a schema file in Protobuf format. Here's a schema outlining a purchase type, along with all of the fields that we'd need to describe a purchase. The first line declares the version of protocol buffers to use. Throughout the course, we're going to use version three. The package declaration creates a name space for proto files to prevent name clashes. By default, it'll be the package name of the generated Java class, unless you specify option java_package in the proto file. The option java_outer_classname field describes the name of the generated Java file. Protocol Buffers define the records as a message. And within that, we can specify the fields and their types. We'll go into more detail on how Protobuf works in the next module. But in the meantime, here's an Avro representation of that same purchase record. Notice that it's defined in JSON format. The namespace field serves the same purpose as in the Protobuf file, preventing name collisions. The namespace also becomes the package name for the generated Java file. For Avro, fields are declared in a JSON array. Again, we define this purchase record using the same fields as in the Protobuf file. For now, we're staying pretty high level, but in the next module, we'll take some time to go into more detail about Avro as well. Now that we've written some schema files, you might be wondering where we ought to put them to make use of them in our application. They need to live somewhere within the Java project. We're going to take the opinionated, but hopefully not controversial, stance that the Avro schema files go in the src/main/avro directory and that Protobuf files land in src/main/proto. Once you've written your schema files, you'll want to generate the model/data objects from the schema files. Fortunately, there are plugins available that you can integrate into your local build with either Maven or Gradle. In this course, we're going to work with Gradle. In the upcoming exercise, you'll configure a build.gradle file to use Avro and Protobuf. Before we move on though, we should note that Schema Registry supports JSONSchema as well. But that's more of a specification, while Avro and Protobuf are full serialization platforms. As a result, the tooling is much better for these two versus JSONSchema. Let's now do a quick walkthrough of a Gradle configuration file. The plugin section is where you identify the plugins that will be used when running Gradle scripts. This example includes plugins required for working with Schema Registry. For the Protobuf plugin, we use generatedFilesBaseDir to point to the directory where the generated files should land. The upcoming exercise will use the Avro plugin defaults. So the build.gradle file doesn't contain configuration settings for it. All right, now let's put together the Gradle configuration block to connect to Schema Registry. You set the URL field to the value from the Schema Registry URL property. Next, we have the credentials block. Here, you'll set the credentials necessary to communicate with Schema Registry in Confluent Cloud. You'll split the basic.auth.user.info field into two parts. All of the letters and numbers up to the colon will form the username. Everything after the colon will form the password field. Next up is the register block. It contains all of the information that the plugin uses to register a schema. To generate the objects from schema files, all you do is run the clean build command. And here are the files that are generated as a result of that build command. Note that the files shown here don't necessarily reflect what you'll see when you go through the exercise. But that's it. Now you're ready to configure and build your own schema files in the next hands-on. See you there.