0
0
Kafkadevops~7 mins

JSON Schema and Protobuf support in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
Kafka can use JSON Schema and Protobuf to define the structure of messages. This helps ensure that data sent between services is consistent and understood by all parts of the system.
When you want to make sure all messages follow a specific format before they are sent or received.
When multiple teams or services share data and need a common agreement on message structure.
When you want to catch errors early by validating messages against a schema.
When you want to evolve message formats safely without breaking existing consumers.
When you want to use efficient binary formats like Protobuf for faster data transfer.
Config File - schema-registry-config.properties
schema-registry-config.properties
schema.registry.url=http://localhost:8081

# Enable support for JSON Schema and Protobuf
schema.registry.enable.json.schema=true
schema.registry.enable.protobuf=true

This configuration file sets up the Kafka Schema Registry to support JSON Schema and Protobuf formats. The schema.registry.url points to the running Schema Registry service. The flags schema.registry.enable.json.schema and schema.registry.enable.protobuf enable support for these schema types so Kafka can validate and serialize messages accordingly.

Commands
This command registers a JSON Schema for a Kafka topic named 'user'. It tells the Schema Registry what the message structure should be for the 'user-value' subject.
Terminal
curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "{\"type\":\"record\",\"name\":\"User\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"}]}"}' http://localhost:8081/subjects/user-value/versions
Expected OutputExpected
{"id":1}
-X POST - Send data to register the schema
-H "Content-Type: application/vnd.schemaregistry.v1+json" - Specify the content type for the Schema Registry API
This command registers a Protobuf schema for the Kafka topic 'user-protobuf'. It defines the message format using Protobuf syntax.
Terminal
curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "syntax = \"proto3\"; message User { string name = 1; }"}' http://localhost:8081/subjects/user-protobuf-value/versions
Expected OutputExpected
{"id":2}
-X POST - Send data to register the schema
-H "Content-Type: application/vnd.schemaregistry.v1+json" - Specify the content type for the Schema Registry API
This command starts a Kafka producer that sends messages to the 'user' topic using the registered JSON Schema with ID 1. It ensures messages follow the schema.
Terminal
kafka-console-producer --broker-list localhost:9092 --topic user --property value.schema.id=1 --property value.schema.type=JSON
Expected OutputExpected
No output (command runs silently)
--property value.schema.id=1 - Use the JSON Schema with ID 1 for message validation
--property value.schema.type=JSON - Specify that the message format is JSON
This command starts a Kafka consumer that reads messages from the 'user' topic and validates them against the JSON Schema with ID 1.
Terminal
kafka-console-consumer --bootstrap-server localhost:9092 --topic user --from-beginning --property print.key=true --property value.schema.id=1 --property value.schema.type=JSON
Expected OutputExpected
(shows messages in JSON format as they arrive)
--property value.schema.id=1 - Use the JSON Schema with ID 1 to decode messages
--property value.schema.type=JSON - Specify that the message format is JSON
Key Concept

If you remember nothing else from this pattern, remember: JSON Schema and Protobuf let Kafka check and enforce message formats so all parts of your system agree on data structure.

Common Mistakes
Not enabling JSON Schema or Protobuf support in the Schema Registry configuration.
Kafka will reject or fail to validate messages using these schema types.
Set schema.registry.enable.json.schema=true and schema.registry.enable.protobuf=true in the Schema Registry config.
Registering schemas with incorrect JSON escaping or invalid Protobuf syntax.
Schema Registry will reject the schema registration request.
Ensure JSON strings are properly escaped and Protobuf syntax is valid before registering.
Producing or consuming messages without specifying the correct schema ID and type properties.
Kafka clients won't know how to serialize or deserialize messages properly, causing errors.
Always specify --property value.schema.id and --property value.schema.type when using schema-based messages.
Summary
Configure Schema Registry to enable JSON Schema and Protobuf support.
Register your message schemas using the Schema Registry REST API.
Use Kafka console producer and consumer with schema ID and type properties to send and receive validated messages.