Senior Developer Advocate (Presenter)
In this exercise you are going to complete the following tasks:
The confluent.properties file you created before is used here for the API key and secret to connect to Schema Registry in Confluent Cloud.
In this exercise you will create a schema locally, but in a later exercise you will download one.
With the latest version of that schema—you will test the compatibility of a schema in the last exercise.
Now that you have reviewed the contents of build.gradle, you are now ready to create a new schema.
Some of the details of the schema are already completed. Let’s quickly review them.
This setting will also be the Java package for the generated objects.
Now let’s complete the schema.
First, create the message type named Purchase.
syntax = "proto3";
package io.confluent.developer.proto;
option java_outer_classname = "PurchaseProto";
// Complete this file by adding the message implementation as directed in the course
message Purchase {
}
Add the item field with a type of string and a tag of 1. This field will represent the name of the purchased item.
syntax = "proto3";
package io.confluent.developer.proto;
option java_outer_classname = "PurchaseProto";
// Complete this file by adding the message implementation as directed in the course
message Purchase {
string item = 1;
}
Add the total_cost field with a type of double and give it a tag of 2. This field represents the price of the purchased item.
Add the customer_id field with a type of string and a tag of 3.
Note: Remember the “tags” are the position of the field in the binary payload.
Save the purchase.proto file.
Now let’s create an Avro schema.
There are also some fields that have been pre-filled for you. Let’s review them now.
This will be the package name for Java objects generated from the schema as well.
Now let’s complete the schema.
First, add the name of the record Purchase.
{
"type":"record",
"namespace": "io.confluent.developer.avro",
// Complete the JSON for the Avro record as directed in the course
"name":"Purchase",
}
Now add a fields key to the JSON—this will point to a JSON array containing the record’s fields.
Add a JSON object with the name of item and a type of string.
"name":"Purchase",
"fields": [
{"name": "item", "type":"string"},
Note: Remember to add a comma after each field object, excluding the last one.
Add a second field with the name of total_cost and a type of double.
Add the final field with a name of customer_id and a type of string.
To complete the fields array, add a closing bracket.
{
"type":"record",
"namespace": "io.confluent.developer.avro",
"name":"Purchase",
"fields": [
{"name": "item", "type":"string"},
{"name": "total_cost", "type": "double" },
{"name": "customer_id", "type": "string"}
]
}
Save the purchase.avsc file.
Now let’s generate the objects from our schemas.
You will see the output of the build in the terminal and it should end with a Build Successful message.
Now let’s go take a look at where the generated files end up. Note that you won’t need to touch the generated files directly. You need them when you build your app. The generated objects are automatically added to the classpath. Referencing them from your app is no issue.
To register a schema, the topic it will be associated with needs to exist. Let’s create the required topics now.
You are now ready to create the new schemas.
When the task completes you should see a BUILD SUCCESSFUL message.
Now go back to the Confluent Cloud Console to inspect the new schemas.
This concludes the exercise.
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.
Hi, I'm Danica Fine. In this Schema Registry exercise, we'll configure and build Protobuf and Avro schemas, and then register these schemas in the Confluent Cloud Schema Registry. Let's see what you can expect as you work through it. First, you'll examine the Gradle configuration file that contains the Gradle scripts that you'll run during the exercise. The Schema Registry section will define the properties needed to connect with Schema Registry, as well as scripts that can be run to register, download, and check compatibility of your schemas. You'll configure a Protobuf schema definition, after which, you'll configure an Avro schema definition. Then you'll run the Gradle build script to generate the schema model objects. And finally, you'll register the schemas in the Confluent Cloud Schema Registry using Gradle. Once you're done, I'll see you in the next module.