Class TableSchema

  • All Implemented Interfaces:
    DataCollectionSchema

    @Immutable
    public class TableSchema
    extends Object
    implements DataCollectionSchema
    Defines the Kafka Connect Schema functionality associated with a given table definition, and which can be used to send rows of data that match the table definition to Kafka Connect.

    Given a Table definition, creating and using a TableSchema is straightforward:

     Table table = ...
     TableSchema tableSchema = new TableSchemaBuilder().create(table);
     
    or use a subclass of TableSchemaBuilder for the particular DBMS. Then, for each row of data:
     Object[] data = ...
     Object key = tableSchema.keyFromColumnData(data);
     Struct value = tableSchema.valueFromColumnData(data);
     Schema keySchema = tableSchema.keySchema();
     Schema valueSchema = tableSchema.valueSchema();
     
    all of which can be handed to Kafka Connect to create a new record.

    When the table structure changes, simply obtain a new or updated Table definition (e.g., via an editor), rebuild the TableSchema for that Table, and use the new TableSchema instance for subsequent records.

    Author:
    Randall Hauch
    See Also:
    TableSchemaBuilder
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • keySchema

        private final org.apache.kafka.connect.data.Schema keySchema
      • envelopeSchema

        private final Envelope envelopeSchema
      • valueSchema

        private final org.apache.kafka.connect.data.Schema valueSchema
    • Constructor Detail

      • TableSchema

        public TableSchema​(TableId id,
                           org.apache.kafka.connect.data.Schema keySchema,
                           StructGenerator keyGenerator,
                           Envelope envelopeSchema,
                           org.apache.kafka.connect.data.Schema valueSchema,
                           StructGenerator valueGenerator)
        Create an instance with the specified Schemas for the keys and values, and the functions that generate the key and value for a given row of data.
        Parameters:
        id - the id of the table corresponding to this schema
        keySchema - the schema for the primary key; may be null
        keyGenerator - the function that converts a row into a single key object for Kafka Connect; may not be null but may return nulls
        valueSchema - the schema for the values; may be null
        valueGenerator - the function that converts a row into a single value object for Kafka Connect; may not be null but may return nulls
    • Method Detail

      • valueSchema

        public org.apache.kafka.connect.data.Schema valueSchema()
        Get the Schema that represents the table's columns, excluding those that make up the keySchema().
        Returns:
        the Schema describing the columns in the table; never null
      • keySchema

        public org.apache.kafka.connect.data.Schema keySchema()
        Get the Schema that represents the table's primary key.
        Specified by:
        keySchema in interface DataCollectionSchema
        Returns:
        the Schema describing the column's that make up the primary key; null if there is no primary key
      • getEnvelopeSchema

        public Envelope getEnvelopeSchema()
        Get the Schema that represents the entire value of messages for the table, i.e. including before/after state and source info.
        Specified by:
        getEnvelopeSchema in interface DataCollectionSchema
        Returns:
        the table's envelope schema
      • keyFromColumnData

        public org.apache.kafka.connect.data.Struct keyFromColumnData​(Object[] columnData)
        Convert the specified row of values into a Kafka Connect key. The row is expected to conform to the structured defined by the table.
        Parameters:
        columnData - the column values for the table
        Returns:
        the key, or null if the columnData
      • valueFromColumnData

        public org.apache.kafka.connect.data.Struct valueFromColumnData​(Object[] columnData)
        Convert the specified row of values into a Kafka Connect value. The row is expected to conform to the structured defined by the table.
        Parameters:
        columnData - the column values for the table
        Returns:
        the value, or null if the columnData
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object