Overview
TheSchema interface defines how messages are serialized and deserialized in Pulsar. It provides type-safe message encoding and decoding, enabling producers and consumers to work with structured data types instead of raw bytes.
Core Methods
encode()
Encode an object representing the message content into a byte array.The message object
A byte array with the serialized content
SchemaSerializationException if the serialization fails
encode(topic, message)
Encode a message for a specific topic.The topic name
The message object
Encoded data wrapper
decode()
Decode a byte array into an object using the schema definition and deserializer implementation.The byte array to decode
The deserialized object
decode(bytes, schemaVersion)
Decode a byte array into an object using a given version.The byte array to decode
The schema version to decode the object (null indicates using latest version)
The deserialized object
decode(ByteBuffer)
Decode a ByteBuffer into an object.The ByteBuffer to decode
The deserialized object
decode(ByteBuffer, schemaVersion)
Decode a ByteBuffer into an object using a given version.The ByteBuffer to decode
The schema version to decode the object
The deserialized object
validate()
Check if the message is a valid object for this schema.The message to verify
SchemaSerializationException if it is not a valid message
The implementation can choose what its most efficient approach to validate the schema. If the implementation doesn’t provide it, it will attempt to use decode(byte[]) to see if this schema can decode this message.
getSchemaInfo()
Get an object that represents the Schema associated metadata.The schema information object
clone()
Duplicate the schema.The duplicated schema
Built-in Primitive Schemas
BYTES
Schema that doesn’t perform any encoding on the message payloads. Accepts a byte array and passes it through.BYTEBUFFER
ByteBuffer Schema.STRING
Schema that can be used to encode/decode messages whose values are String. The payload is encoded with UTF-8.INT8
INT8 Schema.INT16
INT16 Schema.INT32
INT32 Schema.INT64
INT64 Schema.BOOL
Boolean Schema.FLOAT
Float Schema.DOUBLE
Double Schema.DATE
Date Schema.TIME
Time Schema.TIMESTAMP
Timestamp Schema.INSTANT
Instant Schema.LOCAL_DATE
LocalDate Schema.LOCAL_TIME
LocalTime Schema.LOCAL_DATE_TIME
LocalDateTime Schema.Structured Schemas
AVRO(Class)
Create an Avro schema type by default configuration of the class.The POJO class to be used to extract the Avro schema
A Schema instance
AVRO(SchemaDefinition)
Create an Avro schema type with schema definition.The definition of the schema
A Schema instance
JSON(Class)
Create a JSON schema type by extracting the fields of the specified class.The POJO class to be used to extract the JSON schema
A Schema instance
JSON(SchemaDefinition)
Create a JSON schema type with schema definition.The definition of the schema
A Schema instance
PROTOBUF(Class)
Create a Protobuf schema type by extracting the fields of the specified class.The Protobuf generated class to be used to extract the schema
A Schema instance
PROTOBUF(SchemaDefinition)
Create a Protobuf schema type with schema definition.PROTOBUF_NATIVE(Class)
Create a Protobuf-Native schema type by extracting the fields of the specified class.PROTOBUF_NATIVE(SchemaDefinition)
Create a Protobuf-Native schema type with schema definition.KeyValue Schemas
KeyValue(Class, Class, SchemaType)
Key Value Schema using passed in schema type.The key class
The value class
The schema type (supports JSON and AVRO)
A KeyValue Schema instance
KeyValue(Class, Class)
Key Value Schema whose underneath key and value schemas are JSONSchema.KeyValue(Schema, Schema)
Key Value Schema using passed in key and value schemas with INLINE encoding type.The key schema
The value schema
A KeyValue Schema instance
KeyValue(Schema, Schema, KeyValueEncodingType)
Key Value Schema using passed in key, value and encoding type schemas.The key schema
The value schema
The encoding type (INLINE or SEPARATED)
A KeyValue Schema instance
KV_BYTES()
Schema that can be used to encode/decode KeyValue with byte arrays.A KeyValue bytes Schema instance
Auto Schema
AUTO_CONSUME()
Create a schema instance that automatically deserializes messages based on the current topic schema.The auto schema instance
GenericRecord object, that extends the GenericObject interface.
Example:
AUTO_PRODUCE_BYTES()
Create a schema instance that accepts a serialized payload and validates it against the topic schema.The auto schema instance
AUTO_PRODUCE_BYTES(Schema)
Create a schema instance that accepts a serialized payload and validates it against the schema specified.The schema to validate against
The auto schema instance
NATIVE_AVRO()
Create a schema instance that accepts a serialized Avro payload without validating it against the schema specified.The Avro schema
The auto schema instance
Utility Methods
getSchema()
Get a schema instance from SchemaInfo.The schema information
A schema instance
generic()
Returns a generic schema of existing schema info.Schema info
A generic schema instance
getNativeSchema()
Return the native schema that is wrapped by Pulsar API.The internal schema or null if not present