Interface Schema<T>
- Type Parameters:
T- the type of the message value
public interface Schema<T>
Defines how message values are serialized to bytes and deserialized from bytes.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Schema<GenericRecord> Get a schema that auto-detects the topic schema at runtime and decodes each message into aGenericRecord.static Schema<byte[]> Get a schema that auto-detects the topic schema and produces raw bytes accordingly.static Schema<byte[]> autoProduceBytesOf(Schema<?> base) Get a schema that produces raw bytes while validating them against the suppliedbaseschema (in addition to the topic schema).static <T> Schema<T> Get an Avro schema for a POJO class.bool()Get a schema for boolean values.static Schema<byte[]> bytes()Get a schema for raw byte arrays (no serialization).decode(byte[] bytes) Decode bytes to a value.default Tdecode(byte[] bytes, byte[] schemaVersion) Decode bytes with a specific schema version.default Tdecode(ByteBuffer data) Decode from a ByteBuffer.byte[]Encode a value to bytes.float32()Get a schema for 32-bit floating point numbers.float64()Get a schema for 64-bit floating point numbers.static Schema<?> generic(SchemaInfo schemaInfo) Build a generic schema from a rawSchemaInfodefinition.int16()Get a schema for 16-bit signed integers.int32()Get a schema for 32-bit signed integers.int64()Get a schema for 64-bit signed integers.int8()Get a schema for 8-bit signed integers.static <T> Schema<T> Get a JSON schema for a POJO class.static <T extends com.google.protobuf.Message>
Schema<T> Get a Protobuf schema for a generated message class.The schema descriptor for broker-side negotiation.string()Get a schema for UTF-8 strings.
-
Method Details
-
encode
Encode a value to bytes.- Parameters:
message- the value to encode- Returns:
- the serialized byte array representation of the value
-
decode
Decode bytes to a value.- Parameters:
bytes- the byte array to decode- Returns:
- the deserialized value
-
decode
Decode bytes with a specific schema version. Useful for schema evolution.- Parameters:
bytes- the byte array to decodeschemaVersion- the schema version to use for decoding, ornullfor the latest version- Returns:
- the deserialized value
-
decode
Decode from a ByteBuffer.- Parameters:
data- theByteBufferto decode, ornull- Returns:
- the deserialized value, or
nullif the input isnull
-
schemaInfo
SchemaInfo schemaInfo()The schema descriptor for broker-side negotiation.- Returns:
- the
SchemaInfodescribing this schema's type and definition
-
bytes
Get a schema for raw byte arrays (no serialization).- Returns:
- a
Schemafor byte arrays
-
string
Get a schema for UTF-8 strings. -
bool
Get a schema for boolean values. -
int8
Get a schema for 8-bit signed integers. -
int16
Get a schema for 16-bit signed integers. -
int32
Get a schema for 32-bit signed integers. -
int64
Get a schema for 64-bit signed integers. -
float32
Get a schema for 32-bit floating point numbers. -
float64
Get a schema for 64-bit floating point numbers. -
json
Get a JSON schema for a POJO class.- Type Parameters:
T- the POJO type- Parameters:
pojo- the class of the POJO to serialize and deserialize as JSON- Returns:
- a
Schemathat encodes and decodes the POJO using JSON
-
avro
Get an Avro schema for a POJO class.- Type Parameters:
T- the POJO type- Parameters:
pojo- the class of the POJO to serialize and deserialize using Avro- Returns:
- a
Schemathat encodes and decodes the POJO using Avro
-
protobuf
Get a Protobuf schema for a generated message class.- Type Parameters:
T- the Protobuf message type- Parameters:
clazz- the Protobuf generated message class- Returns:
- a
Schemathat encodes and decodes the Protobuf message
-
autoProduceBytes
Get a schema that auto-detects the topic schema and produces raw bytes accordingly.This schema validates that the bytes being produced are compatible with the schema configured on the topic.
- Returns:
- a
Schemafor producing raw bytes with automatic schema validation
-
generic
Build a generic schema from a rawSchemaInfodefinition. Use this when the schema is described by a definition document (e.g. an Avro or JSON schema string) rather than a compiled POJO class.- Parameters:
schemaInfo- the schema descriptor (type + definition bytes)- Returns:
- a generic
Schemafor the given definition - See Also:
-
autoProduceBytesOf
Get a schema that produces raw bytes while validating them against the suppliedbaseschema (in addition to the topic schema). This is the wrapping form ofautoProduceBytes()— the producer sends already-encoded bytes, and the bytes are checked for compatibility withbasebefore being published.- Parameters:
base- the schema the produced bytes must conform to- Returns:
- a
Schemafor producing pre-encoded bytes validated againstbase
-
autoConsume
Get a schema that auto-detects the topic schema at runtime and decodes each message into aGenericRecord. Use this on the consumer side when the value type is not known at compile time (e.g. a generic consume tool). The decodedGenericRecordexposes the value's runtimeSchemaTypeand, for structured types, its fields.- Returns:
- a
Schemathat decodes messages intoGenericRecordvalues
-