Managing Schemas
Schemas, like other entities in Pulsar, can be managed using the admin API.
Schema resources​
A Pulsar schema is a fairly simple data structure stored in Pulsar for representing the structure of messages stored in a Pulsar topic. The schema structure consists of:
- Name: A schema's name is the topic that the schema is associated to.
- Type: A schema type represents the type of the schema. The predefined schema types can be found here. If it is a customized schema, it is left as an empty string.
- Payload: It is a binary representation of the schema. How to interpret it is up to the implementation of the schema.
- Properties: It is a user defined properties as a string/string map. Applications can use this bag for carrying any application specific logics. Possible properties
might be the Git hash associated with the schema, an environment string like
dev
orprod
, etc.
All the schemas are versioned with versions. So you can retrieve the schema definition of a given version if the version is not deleted.
Upload Schema​
pulsar-admin​
You can upload a new schema using the upload
subcommand:
$ pulsar-admin schemas upload <topic-name> --filename /path/to/schema-definition-file
The schema definition file should contain following json string on defining how the schema look like:
{
"type": "STRING",
"schema": "",
"properties": {
"key1" : "value1"
}
}
An example of the schema definition file can be found at SchemaExample.
REST​
POST /admin/v2/schemas/:tenant/:namespace/:topic/schema/uploadSchema
Get Schema​
pulsar-admin​
You can get the latest version of Schema using the get
subcommand.
$ pulsar-admin schemas get <topic-name>
{
"version": 0,
"type": "String",
"timestamp": 0,
"data": "string",
"properties": {
"property1": "string",
"property2": "string"
}
}
You can also retrieve the Schema of a given version by specifying --version
option.
$ pulsar-admin schemas get <topic-name> --version <version>
REST API​
Retrieve the latest version of the schema:
GET /admin/v2/schemas/:tenant/:namespace/:topic/schema/getSchema
Retrieve the schema of a given version:
GET /admin/v2/schemas/:tenant/:namespace/:topic/schema/:version/getSchema
Delete Schema​
pulsar-admin​
You can delete a schema using the delete
subcommand.
$ pulsar-admin schemas delete <topic-name>
REST API​
DELETE /admin/v2/schemas/:tenant/:namespace/:topic/schema/deleteSchema