Pulsar Academy

Pulsar Schema Tutorial

Learn why and how to use schemas in Apache Pulsar.

Introduction

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.

How to create a schema using the Pulsar CLI?

To set a schema for a Pulsar topic, we need to provide the mandatory parameters:

  • Persistence & Tenant name & Namespace name & Topic name
  • Schema definition file path
# 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

How to update a schema using the Pulsar CLI?

To update the schema for a Pulsar topic, we need to provide the mandatory parameters:

  • Persistence & Tenant name & Namespace name & Topic name
  • Schema definition file path
./bin/pulsar-admin schemas upload persistent://my-tenant/my-namespace/schema-upload -f schema.json

How to get the schema from a Pulsar topic using the CLI?

To retrieve the schema information for a Pulsar topic, we need to provide the mandatory parameters:

  • Persistence & Tenant name & Namespace name & Topic name
./bin/pulsar-admin schemas get persistent://my-tenant/my-namespace/my-topic

How to enforce schema validation?

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:

  • Tenant name & Namespace name
./bin/pulsar-admin namespaces set-schema-validation-enforce --enable my-tenant/my-namespace

Table of Contents

Let's stay in touch

Get notified of new developments or blogposts.
Checkmark icon
You've joined the mailing list!
Oops! Something went wrong while submitting the form.