Alibaba Cloud MQ for Confluent uses Schema Registry to manage schemas. This topic describes how to manage schemas using Schema Registry in a Linux environment.
Prerequisites
-
You have an Alibaba Cloud MQ for Confluent instance. For more information, see Purchase and deploy an instance.
-
You have access permissions for the Kafka and Schema Registry clusters. For more information, see RBAC authorization.
-
Java 8 or 11 is installed. For information about Java version compatibility, see Java version support in Confluent Platform.
-
Maven 3.8 or later is installed. For more information, see Install Maven.
Step 1: Prepare the sample code
-
Run the following commands to clone the sample code and switch to the
7.9.0-postbranch.git clone https://github.com/confluentinc/examples.git cd examples/clients/avro git checkout 7.9.0-post -
Create a client configuration file named
java.configin the$HOME/.confluent/directory, where$HOMEis your user's home directory. Add the following settings to the file.# Required connection configs for Kafka producer, consumer, and admin bootstrap.servers={{ BROKER_ENDPOINT }} security.protocol=SASL_SSL sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username='{{ CLUSTER_API_KEY }}' password='{{ CLUSTER_API_SECRET }}'; sasl.mechanism=PLAIN # Required for correctness in Apache Kafka clients prior to 2.6 client.dns.lookup=use_all_dns_ips # Best practice for higher availability in Apache Kafka clients prior to 3.0 session.timeout.ms=45000 # Best practice for Kafka producer to prevent data loss acks=all # Required connection configs for Confluent Cloud Schema Registry schema.registry.url=https://{{ SR_ENDPOINT }} basic.auth.credentials.source=USER_INFO basic.auth.user.info={{ SR_API_KEY }}:{{ SR_API_SECRET }}Parameter
Description
Example value
BROKER_ENDPOINT
The endpoint of the KAFKA service.
Obtain the endpoint from the Access Links and Ports page of the ApsaraMQ for Confluent console. To use the public endpoint, you must enable public network access. For details on other security configurations, see Network access and security settings.
pub-kafka-xxxxxxxxxxx.csp.aliyuncs.com:9092
CLUSTER_API_KEY
The username and password of an LDAP user from the Users page in the ApsaraMQ for Confluent console.
During testing, you can use the root account and its password. To use other users, create them in the ApsaraMQ for Confluent console and grant them the required permissions for the Kafka cluster. For details, see Manage users and grant permissions.
root
CLUSTER_API_SECRET
**
SR_ENDPOINT
The endpoint of the SCHEMA_REGISTRY service.
Obtain the endpoint from the Access Links and Ports page of the ApsaraMQ for Confluent console. To use the public endpoint, you must enable public network access. For details on other security configurations, see Network access and security settings.
pub-schemaregistry-xxxxxxxxxxx.csp.aliyuncs.com:443
SR_API_KEY
The username and password of an LDAP user from the Users page in the ApsaraMQ for Confluent console.
During testing, you can use the root account and its password. To use other users, create them in the ApsaraMQ for Confluent console and grant them the required permissions for Schema Registry. For details, see Manage users and grant permissions.
root
SR_API_SECRET
**
Step 2: Create a topic
For this tutorial, create a topic named transactions. If you use a different topic name, you must update the parameter in the code.
-
Log on to Control Center. On the Home page, click the controlcenter.clusterk card to go to the Cluster overview page.
The left-side navigation menu contains options such as Cluster overview, Brokers, Topics, and Consumers. The right side of the page displays an overview of the cluster's brokers and topics.
-
In the left-side navigation pane, click Topics. On the topic list page, click + Add topic.
-
On the New topic page, set a topic name and the number of partitions, and then click Create with defaults.
For example, set Topic name to
transactionsand Number of partitions to1. -
After you create the topic, you are redirected to its details page.
The page includes four tabs: Overview, Messages, Schema, and Configuration. The Overview tab shows metrics such as Production (production rate), Consumption (consumption rate), and Availability (availability).
Step 3: Enable schema validation
-
On the topic details page, click the Configuration tab, and then click Edit settings.
-
Then, click Switch to expert mode.
-
Set the confluent_value_schema_validation field to true and then click Save changes to enable schema validation for message content. After this feature is enabled, message formats are validated during production and consumption.
Step 4: Create a schema
-
Navigate to the
examples/clients/avrodirectory of your project and run the following command to view the contents of thePayment.avscfile.cat src/main/resources/avro/io/confluent/examples/clients/basicavro/Payment.avscThe command returns the following output:
{ "namespace": "io.confluent.examples.clients.basicavro", "type": "record", "name": "Payment", "fields": [ {"name": "id", "type": "string"}, {"name": "amount", "type": "double"} ] } -
On the topic details page in the Control Center console, click the Schema tab, and then click Set a schema.
-
On the Schema tab, click Avro, paste the content from the
Payment.avscfile into the editor, and click Create.
Step 5: Send and consume messages
Send messages
If your schema uses the Avro validation format, you must specify the message serialization method as the KafkaAvroSerializer class and configure the message value class as the Payment class when you send messages.
The following code shows an example:
Follow these steps to send messages:
-
Go to the
examples/clients/avroproject directory and run the following command to compile the project.mvn clean compile package -
After the compilation completes, run the following command to produce messages.
mvn exec:java -Dexec.mainClass=io.confluent.examples.clients.basicavro.ProducerExample \ -Dexec.args="$HOME/.confluent/java.config"The following output indicates that the messages were produced successfully.
... Successfully produced 10 messages to a topic called transactions [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ... -
You can view the produced messages in the Control Center console.
On the Topics page, select the transactions topic and click the Messages tab. You will see 10 messages with keys ranging from
"id0"to"id9", and the value is in JSON format, for example,{"id":"id9","amount":1000}.
Consume messages
If the schema's validation format is Avro, when you consume messages, you must specify the message deserialization method as the KafkaAvroDeSerializer class and configure the message value class as the Payment class.
The following code shows an example:
Follow these steps to consume messages:
-
If you have not yet compiled the project, go to the
examples/clients/avroproject directory and run the following command:mvn clean compile package -
Run the following command to consume messages.
mvn exec:java -Dexec.mainClass=io.confluent.examples.clients.basicavro.ConsumerExample \ -Dexec.args="$HOME/.confluent/java.config" -
The following output indicates that the messages were consumed successfully.
... key = id0, value = {"id": "id0", "amount": 1000.0} key = id1, value = {"id": "id1", "amount": 1000.0} key = id2, value = {"id": "id2", "amount": 1000.0} key = id3, value = {"id": "id3", "amount": 1000.0} key = id4, value = {"id": "id4", "amount": 1000.0} key = id5, value = {"id": "id5", "amount": 1000.0} key = id6, value = {"id": "id6", "amount": 1000.0} key = id7, value = {"id": "id7", "amount": 1000.0} key = id8, value = {"id": "id8", "amount": 1000.0} key = id9, value = {"id": "id9", "amount": 1000.0} ...
References
For more information about Schema Registry, see Schema Registry Overview.