Skip to main content
A Pulsar schema is defined by a data structure called SchemaInfo. It’s stored and enforced per topic—you can’t define one at the namespace or tenant level. Here’s a SchemaInfo for a string schema:

Schema types

Pulsar schema types fall into three categories: primitive, complex, and auto.

Primitive types

Pulsar stores no schema data in SchemaInfo for primitive types. Some primitive schema implementations use properties to hold implementation-specific settings—a string schema, for example, can store the character encoding it uses to serialize and deserialize strings.

Complex types

KeyValue schema

A KeyValue schema lets an application define a schema for the key and a schema for the value. Pulsar stores both SchemaInfo records together. There are two ways to encode a single key/value pair in a message:
  • INLINE—the key and value are encoded together in the message payload.
  • SEPARATED—the key is stored as the message key and the value as the message payload.

Struct schema

NativeAvroBytesSchema exists for migration. When you ingest events from an external system such as Kafka or Cassandra, the data is often already serialized as Avro and has already been validated against a schema—including compatibility checks—by the system that produced it. In that case the Pulsar producer doesn’t need to repeat the validation. It passes each message through with its schema.
To use Kafka Avro, JSON Schema, or Protobuf schemas from a Pulsar client—with schemas resolved from the Kafka Schema Registry rather than Pulsar’s—see Use External Avro Schema with Pulsar clients.
There are three ways to construct a struct schema.
Predefine the struct as a POJO in Java, a struct in Go, or a class generated by Avro or Protobuf tooling. Pulsar reads the schema definition from it using an Avro library, and that definition becomes the schema data inside SchemaInfo.
  1. Define the class for the messages you send:
  2. Create a producer with the struct schema and send a message:
  3. Create a consumer with the struct schema and receive it:

Auto schemas

When you can’t know a topic’s schema type in advance, use an auto schema to produce or consume generic records.
  • AUTO_PRODUCE sends data to a topic that already has a schema, and validates that the outbound bytes are compatible with it.
  • AUTO_CONSUME reads from a topic that has a schema and deserializes each message into a language-specific GenericRecord, using the SchemaInfo it retrieves from the broker.

What’s next

Compatibility

Compatibility strategies, versioning, and safe evolution.

Use with clients

Construct each schema type in your application.
Parts of this page are adapted from the Apache Pulsar documentation, licensed under the Apache License 2.0.