Schemas play a crucial role by defining the structure of data in your topics. This helps prevent data that has the wrong format, as well as providing a format that both producing and consuming applications know they can rely on. The schema acts as a contract between both parties. Before you run the commands below, make sure you have started Pulsar.
To set a schema for a Pulsar topic, we need to provide the mandatory parameters:
# Content of schema.json
{
"type": "record",
"name": "ExampleSchema",
"fields": [
{ "name": "id", "type": "int" },
{ "name": "name", "type": "string" }
]
}
./bin/pulsar-admin schemas upload persistent://my-tenant/my-namespace/schema-upload -f schema.json
To update the schema for a Pulsar topic, we need to provide the mandatory parameters:
./bin/pulsar-admin schemas upload persistent://my-tenant/my-namespace/schema-upload -f schema.json
To retrieve the schema information for a Pulsar topic, we need to provide the mandatory parameters:
./bin/pulsar-admin schemas get persistent://my-tenant/my-namespace/my-topic
By default, schema validation is not enforced, meaning you are still at risk of producers sending incorrect data to Pulsar. To make the broker validate that all data sent is adhering to the schema, you can enable schema validation at the namespace level. To do so, we need to provide the mandatory parameters:
./bin/pulsar-admin namespaces set-schema-validation-enforce --enable my-tenant/my-namespace