Interface Schema<T>

  • All Superinterfaces:
    java.lang.Cloneable
    All Known Subinterfaces:
    GenericSchema<T>, KeyValueSchema<K,​V>

    @Public
    @Stable
    public interface Schema<T>
    extends java.lang.Cloneable
    Message schema definition.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Schema<java.lang.Boolean> BOOL
      Boolean Schema.
      static Schema<java.nio.ByteBuffer> BYTEBUFFER
      ByteBuffer Schema.
      static Schema<byte[]> BYTES
      Schema that doesn't perform any encoding on the message payloads.
      static Schema<java.util.Date> DATE
      Date Schema.
      static Schema<java.lang.Double> DOUBLE
      Double Schema.
      static Schema<java.lang.Float> FLOAT
      Float Schema.
      static Schema<java.time.Instant> INSTANT
      Instant Schema.
      static Schema<java.lang.Short> INT16
      INT16 Schema.
      static Schema<java.lang.Integer> INT32
      INT32 Schema.
      static Schema<java.lang.Long> INT64
      INT64 Schema.
      static Schema<java.lang.Byte> INT8
      INT8 Schema.
      static Schema<java.time.LocalDate> LOCAL_DATE
      LocalDate Schema.
      static Schema<java.time.LocalDateTime> LOCAL_DATE_TIME
      LocalDateTime Schema.
      static Schema<java.time.LocalTime> LOCAL_TIME
      LocalTime Schema.
      static Schema<java.lang.String> STRING
      Schema that can be used to encode/decode messages whose values are String.
      static Schema<java.sql.Time> TIME
      Time Schema.
      static Schema<java.sql.Timestamp> TIMESTAMP
      Timestamp Schema.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods Deprecated Methods 
      Modifier and Type Method Description
      static Schema<GenericRecord> AUTO()
      Deprecated.
      static Schema<GenericRecord> AUTO_CONSUME()
      Create a schema instance that automatically deserialize messages based on the current topic schema.
      static Schema<byte[]> AUTO_PRODUCE_BYTES()
      Create a schema instance that accepts a serialized payload and validates it against the topic schema.
      static Schema<byte[]> AUTO_PRODUCE_BYTES​(Schema<?> schema)
      Create a schema instance that accepts a serialized payload and validates it against the schema specified.
      static <T> Schema<T> AVRO​(java.lang.Class<T> pojo)
      Create a Avro schema type by default configuration of the class.
      static <T> Schema<T> AVRO​(SchemaDefinition<T> schemaDefinition)
      Create a Avro schema type with schema definition.
      Schema<T> clone()
      Duplicates the schema.
      default void configureSchemaInfo​(java.lang.String topic, java.lang.String componentName, SchemaInfo schemaInfo)
      Configure the schema to use the provided schema info.
      default T decode​(byte[] bytes)
      Decode a byte array into an object using the schema definition and deserializer implementation.
      default T decode​(byte[] bytes, byte[] schemaVersion)
      Decode a byte array into an object using a given version.
      default T decode​(java.nio.ByteBuffer data, byte[] schemaVersion)
      Decode a ByteBuffer into an object using a given version.
      byte[] encode​(T message)
      Encode an object representing the message content into a byte array.
      static GenericSchema<GenericRecord> generic​(SchemaInfo schemaInfo)
      Returns a generic schema of existing schema info.
      default java.util.Optional<java.lang.Object> getNativeSchema()
      Return the native schema that is wrapped by Pulsar API.
      static Schema<?> getSchema​(SchemaInfo schemaInfo)  
      SchemaInfo getSchemaInfo()  
      static <T> Schema<T> JSON​(java.lang.Class<T> pojo)
      Create a JSON schema type by extracting the fields of the specified class.
      static <T> Schema<T> JSON​(SchemaDefinition schemaDefinition)
      Create a JSON schema type with schema definition.
      static <K,​V>
      Schema<KeyValue<K,​V>>
      KeyValue​(java.lang.Class<K> key, java.lang.Class<V> value)
      Key Value Schema whose underneath key and value schemas are JSONSchema.
      static <K,​V>
      Schema<KeyValue<K,​V>>
      KeyValue​(java.lang.Class<K> key, java.lang.Class<V> value, SchemaType type)
      Key Value Schema using passed in schema type, support JSON and AVRO currently.
      static <K,​V>
      Schema<KeyValue<K,​V>>
      KeyValue​(Schema<K> key, Schema<V> value)
      Key Value Schema using passed in key and value schemas.
      static <K,​V>
      Schema<KeyValue<K,​V>>
      KeyValue​(Schema<K> key, Schema<V> value, KeyValueEncodingType keyValueEncodingType)
      Key Value Schema using passed in key, value and encoding type schemas.
      static Schema<KeyValue<byte[],​byte[]>> KV_BYTES()
      Schema that can be used to encode/decode KeyValue.
      static Schema<byte[]> NATIVE_AVRO​(java.lang.Object schema)
      Create a schema instance that accepts a serialized Avro payload without validating it against the schema specified.
      static <T extends com.google.protobuf.GeneratedMessageV3>
      Schema<T>
      PROTOBUF​(java.lang.Class<T> clazz)
      Create a Protobuf schema type by extracting the fields of the specified class.
      static <T extends com.google.protobuf.GeneratedMessageV3>
      Schema<T>
      PROTOBUF​(SchemaDefinition<T> schemaDefinition)
      Create a Protobuf schema type with schema definition.
      static <T extends com.google.protobuf.GeneratedMessageV3>
      Schema<T>
      PROTOBUF_NATIVE​(java.lang.Class<T> clazz)
      Create a Protobuf-Native schema type by extracting the fields of the specified class.
      static <T extends com.google.protobuf.GeneratedMessageV3>
      Schema<T>
      PROTOBUF_NATIVE​(SchemaDefinition<T> schemaDefinition)
      Create a Protobuf-Native schema type with schema definition.
      default boolean requireFetchingSchemaInfo()
      Check if this schema requires fetching schema info to configure the schema.
      default void setSchemaInfoProvider​(SchemaInfoProvider schemaInfoProvider)  
      default boolean supportSchemaVersioning()
      Returns whether this schema supports versioning.
      default void validate​(byte[] message)
      Check if the message is a valid object for this schema.
    • Field Detail

      • BYTES

        static final Schema<byte[]> BYTES
        Schema that doesn't perform any encoding on the message payloads. Accepts a byte array and it passes it through.
      • BYTEBUFFER

        static final Schema<java.nio.ByteBuffer> BYTEBUFFER
        ByteBuffer Schema.
      • STRING

        static final Schema<java.lang.String> STRING
        Schema that can be used to encode/decode messages whose values are String. The payload is encoded with UTF-8.
      • INT8

        static final Schema<java.lang.Byte> INT8
        INT8 Schema.
      • INT16

        static final Schema<java.lang.Short> INT16
        INT16 Schema.
      • INT32

        static final Schema<java.lang.Integer> INT32
        INT32 Schema.
      • INT64

        static final Schema<java.lang.Long> INT64
        INT64 Schema.
      • BOOL

        static final Schema<java.lang.Boolean> BOOL
        Boolean Schema.
      • FLOAT

        static final Schema<java.lang.Float> FLOAT
        Float Schema.
      • DOUBLE

        static final Schema<java.lang.Double> DOUBLE
        Double Schema.
      • DATE

        static final Schema<java.util.Date> DATE
        Date Schema.
      • TIME

        static final Schema<java.sql.Time> TIME
        Time Schema.
      • TIMESTAMP

        static final Schema<java.sql.Timestamp> TIMESTAMP
        Timestamp Schema.
      • INSTANT

        static final Schema<java.time.Instant> INSTANT
        Instant Schema.
      • LOCAL_DATE

        static final Schema<java.time.LocalDate> LOCAL_DATE
        LocalDate Schema.
      • LOCAL_TIME

        static final Schema<java.time.LocalTime> LOCAL_TIME
        LocalTime Schema.
      • LOCAL_DATE_TIME

        static final Schema<java.time.LocalDateTime> LOCAL_DATE_TIME
        LocalDateTime Schema.
    • Method Detail

      • validate

        default void validate​(byte[] message)
        Check if the message is a valid object for this schema.

        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 or not as a validation mechanism to verify the bytes.

        Parameters:
        message - the messages to verify
        Throws:
        SchemaSerializationException - if it is not a valid message
      • encode

        byte[] encode​(T message)
        Encode an object representing the message content into a byte array.
        Parameters:
        message - the message object
        Returns:
        a byte array with the serialized content
        Throws:
        SchemaSerializationException - if the serialization fails
      • supportSchemaVersioning

        default boolean supportSchemaVersioning()
        Returns whether this schema supports versioning.

        Most of the schema implementations don't really support schema versioning, or it just doesn't make any sense to support schema versionings (e.g. primitive schemas). Only schema returns GenericRecord should support schema versioning.

        If a schema implementation returns false, it should implement decode(byte[]); while a schema implementation returns true, it should implement decode(byte[], byte[]) instead.

        Returns:
        true if this schema implementation supports schema versioning; otherwise returns false.
      • setSchemaInfoProvider

        default void setSchemaInfoProvider​(SchemaInfoProvider schemaInfoProvider)
      • decode

        default T decode​(byte[] bytes)
        Decode a byte array into an object using the schema definition and deserializer implementation.
        Parameters:
        bytes - the byte array to decode
        Returns:
        the deserialized object
      • decode

        default T decode​(byte[] bytes,
                         byte[] schemaVersion)
        Decode a byte array into an object using a given version.
        Parameters:
        bytes - the byte array to decode
        schemaVersion - the schema version to decode the object. null indicates using latest version.
        Returns:
        the deserialized object
      • decode

        default T decode​(java.nio.ByteBuffer data,
                         byte[] schemaVersion)
        Decode a ByteBuffer into an object using a given version.
        Parameters:
        data - the ByteBuffer to decode
        schemaVersion - the schema version to decode the object. null indicates using latest version.
        Returns:
        the deserialized object
      • getSchemaInfo

        SchemaInfo getSchemaInfo()
        Returns:
        an object that represents the Schema associated metadata
      • requireFetchingSchemaInfo

        default boolean requireFetchingSchemaInfo()
        Check if this schema requires fetching schema info to configure the schema.
        Returns:
        true if the schema requires fetching schema info to configure the schema, otherwise false.
      • configureSchemaInfo

        default void configureSchemaInfo​(java.lang.String topic,
                                         java.lang.String componentName,
                                         SchemaInfo schemaInfo)
        Configure the schema to use the provided schema info.
        Parameters:
        topic - topic name
        componentName - component name
        schemaInfo - schema info
      • clone

        Schema<T> clone()
        Duplicates the schema.
        Returns:
        The duplicated schema.
      • getNativeSchema

        default java.util.Optional<java.lang.Object> getNativeSchema()
        Return the native schema that is wrapped by Pulsar API. For instance with this method you can access the Avro schema
        Returns:
        the internal schema or null if not present
      • PROTOBUF

        static <T extends com.google.protobuf.GeneratedMessageV3> Schema<T> PROTOBUF​(java.lang.Class<T> clazz)
        Create a Protobuf schema type by extracting the fields of the specified class.
        Parameters:
        clazz - the Protobuf generated class to be used to extract the schema
        Returns:
        a Schema instance
      • PROTOBUF

        static <T extends com.google.protobuf.GeneratedMessageV3> Schema<T> PROTOBUF​(SchemaDefinition<T> schemaDefinition)
        Create a Protobuf schema type with schema definition.
        Parameters:
        schemaDefinition - schemaDefinition the definition of the schema
        Returns:
        a Schema instance
      • PROTOBUF_NATIVE

        static <T extends com.google.protobuf.GeneratedMessageV3> Schema<T> PROTOBUF_NATIVE​(java.lang.Class<T> clazz)
        Create a Protobuf-Native schema type by extracting the fields of the specified class.
        Parameters:
        clazz - the Protobuf generated class to be used to extract the schema
        Returns:
        a Schema instance
      • PROTOBUF_NATIVE

        static <T extends com.google.protobuf.GeneratedMessageV3> Schema<T> PROTOBUF_NATIVE​(SchemaDefinition<T> schemaDefinition)
        Create a Protobuf-Native schema type with schema definition.
        Parameters:
        schemaDefinition - schemaDefinition the definition of the schema
        Returns:
        a Schema instance
      • AVRO

        static <T> Schema<T> AVRO​(java.lang.Class<T> pojo)
        Create a Avro schema type by default configuration of the class.
        Parameters:
        pojo - the POJO class to be used to extract the Avro schema
        Returns:
        a Schema instance
      • AVRO

        static <T> Schema<T> AVRO​(SchemaDefinition<T> schemaDefinition)
        Create a Avro schema type with schema definition.
        Parameters:
        schemaDefinition - the definition of the schema
        Returns:
        a Schema instance
      • JSON

        static <T> Schema<T> JSON​(java.lang.Class<T> pojo)
        Create a JSON schema type by extracting the fields of the specified class.
        Parameters:
        pojo - the POJO class to be used to extract the JSON schema
        Returns:
        a Schema instance
      • JSON

        static <T> Schema<T> JSON​(SchemaDefinition schemaDefinition)
        Create a JSON schema type with schema definition.
        Parameters:
        schemaDefinition - the definition of the schema
        Returns:
        a Schema instance
      • KeyValue

        static <K,​V> Schema<KeyValue<K,​V>> KeyValue​(java.lang.Class<K> key,
                                                                java.lang.Class<V> value,
                                                                SchemaType type)
        Key Value Schema using passed in schema type, support JSON and AVRO currently.
      • KV_BYTES

        static Schema<KeyValue<byte[],​byte[]>> KV_BYTES()
        Schema that can be used to encode/decode KeyValue.
      • KeyValue

        static <K,​V> Schema<KeyValue<K,​V>> KeyValue​(java.lang.Class<K> key,
                                                                java.lang.Class<V> value)
        Key Value Schema whose underneath key and value schemas are JSONSchema.
      • KeyValue

        static <K,​V> Schema<KeyValue<K,​V>> KeyValue​(Schema<K> key,
                                                                Schema<V> value)
        Key Value Schema using passed in key and value schemas.
      • AUTO_CONSUME

        static Schema<GenericRecord> AUTO_CONSUME()
        Create a schema instance that automatically deserialize messages based on the current topic schema.

        The messages values are deserialized into a GenericRecord object, that extends the GenericObject interface.

        Returns:
        the auto schema instance
      • AUTO_PRODUCE_BYTES

        static Schema<byte[]> AUTO_PRODUCE_BYTES()
        Create a schema instance that accepts a serialized payload and validates it against the topic schema.

        Currently this is only supported with Avro and JSON schema types.

        This method can be used when publishing a raw JSON payload, for which the format is known and a POJO class is not available.

        Returns:
        the auto schema instance
      • AUTO_PRODUCE_BYTES

        static Schema<byte[]> AUTO_PRODUCE_BYTES​(Schema<?> schema)
        Create a schema instance that accepts a serialized payload and validates it against the schema specified.
        Returns:
        the auto schema instance
        Since:
        2.5.0
        See Also:
        AUTO_PRODUCE_BYTES()
      • NATIVE_AVRO

        static Schema<byte[]> NATIVE_AVRO​(java.lang.Object schema)
        Create a schema instance that accepts a serialized Avro payload without validating it against the schema specified. It can be useful when migrating data from existing event or message stores.
        Returns:
        the auto schema instance
        Since:
        2.9.0
      • generic

        static GenericSchema<GenericRecord> generic​(SchemaInfo schemaInfo)
        Returns a generic schema of existing schema info.

        Only supports AVRO and JSON.

        Parameters:
        schemaInfo - schema info
        Returns:
        a generic schema instance