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 Type
    Method
    Description
    Get a schema that auto-detects the topic schema at runtime and decodes each message into a GenericRecord.
    static Schema<byte[]>
    Get a schema that auto-detects the topic schema and produces raw bytes accordingly.
    static Schema<byte[]>
    Get a schema that produces raw bytes while validating them against the supplied base schema (in addition to the topic schema).
    static <T> Schema<T>
    avro(Class<T> pojo)
    Get an Avro schema for a POJO class.
    static Schema<Boolean>
    Get a schema for boolean values.
    static Schema<byte[]>
    Get a schema for raw byte arrays (no serialization).
    decode(byte[] bytes)
    Decode bytes to a value.
    default T
    decode(byte[] bytes, byte[] schemaVersion)
    Decode bytes with a specific schema version.
    default T
    Decode from a ByteBuffer.
    byte[]
    encode(T message)
    Encode a value to bytes.
    static Schema<Float>
    Get a schema for 32-bit floating point numbers.
    static Schema<Double>
    Get a schema for 64-bit floating point numbers.
    static Schema<?>
    generic(SchemaInfo schemaInfo)
    Build a generic schema from a raw SchemaInfo definition.
    static Schema<Short>
    Get a schema for 16-bit signed integers.
    static Schema<Integer>
    Get a schema for 32-bit signed integers.
    static Schema<Long>
    Get a schema for 64-bit signed integers.
    static Schema<Byte>
    Get a schema for 8-bit signed integers.
    static <T> Schema<T>
    json(Class<T> pojo)
    Get a JSON schema for a POJO class.
    static <T extends com.google.protobuf.Message>
    Schema<T>
    protobuf(Class<T> clazz)
    Get a Protobuf schema for a generated message class.
    The schema descriptor for broker-side negotiation.
    static Schema<String>
    Get a schema for UTF-8 strings.
  • Method Details

    • encode

      byte[] encode(T message)
      Encode a value to bytes.
      Parameters:
      message - the value to encode
      Returns:
      the serialized byte array representation of the value
    • decode

      T decode(byte[] bytes)
      Decode bytes to a value.
      Parameters:
      bytes - the byte array to decode
      Returns:
      the deserialized value
    • decode

      default T decode(byte[] bytes, byte[] schemaVersion)
      Decode bytes with a specific schema version. Useful for schema evolution.
      Parameters:
      bytes - the byte array to decode
      schemaVersion - the schema version to use for decoding, or null for the latest version
      Returns:
      the deserialized value
    • decode

      default T decode(ByteBuffer data)
      Decode from a ByteBuffer.
      Parameters:
      data - the ByteBuffer to decode, or null
      Returns:
      the deserialized value, or null if the input is null
    • schemaInfo

      SchemaInfo schemaInfo()
      The schema descriptor for broker-side negotiation.
      Returns:
      the SchemaInfo describing this schema's type and definition
    • bytes

      static Schema<byte[]> bytes()
      Get a schema for raw byte arrays (no serialization).
      Returns:
      a Schema for byte arrays
    • string

      static Schema<String> string()
      Get a schema for UTF-8 strings.
      Returns:
      a Schema for String values
    • bool

      static Schema<Boolean> bool()
      Get a schema for boolean values.
      Returns:
      a Schema for Boolean values
    • int8

      static Schema<Byte> int8()
      Get a schema for 8-bit signed integers.
      Returns:
      a Schema for Byte values
    • int16

      static Schema<Short> int16()
      Get a schema for 16-bit signed integers.
      Returns:
      a Schema for Short values
    • int32

      static Schema<Integer> int32()
      Get a schema for 32-bit signed integers.
      Returns:
      a Schema for Integer values
    • int64

      static Schema<Long> int64()
      Get a schema for 64-bit signed integers.
      Returns:
      a Schema for Long values
    • float32

      static Schema<Float> float32()
      Get a schema for 32-bit floating point numbers.
      Returns:
      a Schema for Float values
    • float64

      static Schema<Double> float64()
      Get a schema for 64-bit floating point numbers.
      Returns:
      a Schema for Double values
    • json

      static <T> Schema<T> json(Class<T> pojo)
      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 Schema that encodes and decodes the POJO using JSON
    • avro

      static <T> Schema<T> avro(Class<T> pojo)
      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 Schema that encodes and decodes the POJO using Avro
    • protobuf

      static <T extends com.google.protobuf.Message> Schema<T> protobuf(Class<T> clazz)
      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 Schema that encodes and decodes the Protobuf message
    • autoProduceBytes

      static Schema<byte[]> 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 Schema for producing raw bytes with automatic schema validation
    • generic

      static Schema<?> generic(SchemaInfo schemaInfo)
      Build a generic schema from a raw SchemaInfo definition. 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 Schema for the given definition
      See Also:
    • autoProduceBytesOf

      static Schema<byte[]> autoProduceBytesOf(Schema<?> base)
      Get a schema that produces raw bytes while validating them against the supplied base schema (in addition to the topic schema). This is the wrapping form of autoProduceBytes() — the producer sends already-encoded bytes, and the bytes are checked for compatibility with base before being published.
      Parameters:
      base - the schema the produced bytes must conform to
      Returns:
      a Schema for producing pre-encoded bytes validated against base
    • autoConsume

      static Schema<GenericRecord> autoConsume()
      Get a schema that auto-detects the topic schema at runtime and decodes each message into a GenericRecord. Use this on the consumer side when the value type is not known at compile time (e.g. a generic consume tool). The decoded GenericRecord exposes the value's runtime SchemaType and, for structured types, its fields.
      Returns:
      a Schema that decodes messages into GenericRecord values