Senior Developer Advocate (Presenter)
In this exercise you are going to complete the following tasks:
gradle.build
fileschema-registry
project, open the build.gradle
file, and review its contents beginning near the top.plugins
section, take note of the protobuf
, schema-registry
and avro
plugins.dependencies
section, take note of the protobuf
and avro
required libraries.protobuf
section, take note of where the generated model objects end up in the project directory.schemaRegistry
section, take note of the configuration blocks for different commands offered by the plugin. The confluent.properties
file you created before is used here for the API key and secret to connect to Schema Registry in Confluent Cloud.
register
section contains the information required to register schemas with the registerSchemasTask
that you will run later in this exercise.download
section contains the required information for downloading schemas.In this exercise you will create a schema locally, but in a later exercise you will download one.
compatibility
section contains what’s needed to test if changes you make to a schema are compatible. 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.
purchase.proto
file contained in the src/main/proto
directory.Some of the details of the schema are already completed. Let’s quickly review them.
3
of the Protocol Buffer specification.io.confluent.developer.proto
.This setting will also be the Java package for the generated objects.
java_outer_class-name
option is set to PurchaseProto
.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.
```json
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.
purchase.avsc
file contained in the src/main/avro
directory.There are also some fields that have been pre-filled for you. Let’s review them now.
record
.io.confluent.developer.avro
.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.
schema-registry
project../gradlew clean build
.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.
build
directory and expand the directory structure.generated-main-proto-java
directory.generated-main-avro-java
directory.To register a schema, the topic it will be associated with needs to exist. Let’s create the required topics now.
schema-registry-101
Kafka cluster in Confluent Cloud.avro-purchase
.proto-purchase
.You are now ready to create the new schemas.
schema-registry
course project../gradlew registerSchemasTask
.When the task completes you should see a BUILD SUCCESSFUL
message.
Now go back to the Confluent Cloud Console to inspect the new schemas.
purchase.avsc
definition.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.