Class TableSchemaBuilder


  • @ThreadSafe
    @Immutable
    public class TableSchemaBuilder
    extends Object
    Builder that constructs TableSchema instances for Table definitions.

    This builder is responsible for mapping table columns to fields in Kafka Connect Schemas, and this is necessarily dependent upon the database's supported types. Although mappings are defined for standard types, this class may need to be subclassed for each DBMS to add support for DBMS-specific types by overriding any of the "add*Field" methods.

    See the Java SE Mapping SQL and Java Types for details about how JDBC types map to Java value types.

    Author:
    Randall Hauch
    • Constructor Detail

      • TableSchemaBuilder

        public TableSchemaBuilder​(ValueConverterProvider valueConverterProvider,
                                  SchemaNameAdjuster schemaNameAdjuster,
                                  CustomConverterRegistry customConverterRegistry,
                                  org.apache.kafka.connect.data.Schema sourceInfoSchema,
                                  boolean sanitizeFieldNames,
                                  boolean multiPartitionMode)
        Create a new instance of the builder.
        Parameters:
        valueConverterProvider - the provider for obtaining ValueConverters and SchemaBuilders; may not be null
        schemaNameAdjuster - the adjuster for schema names; may not be null
      • TableSchemaBuilder

        public TableSchemaBuilder​(ValueConverterProvider valueConverterProvider,
                                  DefaultValueConverter defaultValueConverter,
                                  SchemaNameAdjuster schemaNameAdjuster,
                                  CustomConverterRegistry customConverterRegistry,
                                  org.apache.kafka.connect.data.Schema sourceInfoSchema,
                                  boolean sanitizeFieldNames,
                                  boolean multiPartitionMode)
        Create a new instance of the builder.
        Parameters:
        valueConverterProvider - the provider for obtaining ValueConverters and SchemaBuilders; may not be null
        defaultValueConverter - is used to convert the default value literal to a Java type recognized by value converters for a subset of types. may be null.
        schemaNameAdjuster - the adjuster for schema names; may not be null
    • Method Detail

      • create

        public TableSchema create​(String schemaPrefix,
                                  String envelopSchemaName,
                                  Table table,
                                  Tables.ColumnNameFilter filter,
                                  ColumnMappers mappers,
                                  Key.KeyMapper keysMapper)
        Create a TableSchema from the given table definition. The resulting TableSchema will have a key schema that contains all of the columns that make up the table's primary key, and a value schema that contains only those columns that are not in the table's primary key.

        This is equivalent to calling create(table,false).

        Parameters:
        schemaPrefix - the prefix added to the table identifier to construct the schema names; may be null if there is no prefix
        envelopSchemaName - the name of the schema of the built table's envelope
        table - the table definition; may not be null
        filter - the filter that specifies whether columns in the table should be included; may be null if all columns are to be included
        mappers - the mapping functions for columns; may be null if none of the columns are to be mapped to different values
        Returns:
        the table schema that can be used for sending rows of data for this table to Kafka Connect; never null
      • tableSchemaName

        private String tableSchemaName​(TableId tableId)
        Returns the type schema name for the given table.
      • createKeyGenerator

        protected StructGenerator createKeyGenerator​(org.apache.kafka.connect.data.Schema schema,
                                                     TableId columnSetName,
                                                     List<Column> columns)
        Creates the function that produces a Kafka Connect key object for a row of data.
        Parameters:
        schema - the Kafka Connect schema for the key; may be null if there is no known schema, in which case the generator will be null
        columnSetName - the name for the set of columns, used in error messages; may not be null
        columns - the column definitions for the table that defines the row; may not be null
        Returns:
        the key-generating function, or null if there is no key schema
      • validateIncomingRowToInternalMetadata

        private void validateIncomingRowToInternalMetadata​(int[] recordIndexes,
                                                           org.apache.kafka.connect.data.Field[] fields,
                                                           ValueConverter[] converters,
                                                           Object[] row,
                                                           int position)
      • createValueGenerator

        protected StructGenerator createValueGenerator​(org.apache.kafka.connect.data.Schema schema,
                                                       TableId tableId,
                                                       List<Column> columns,
                                                       Tables.ColumnNameFilter filter,
                                                       ColumnMappers mappers)
        Creates the function that produces a Kafka Connect value object for a row of data.
        Parameters:
        schema - the Kafka Connect schema for the value; may be null if there is no known schema, in which case the generator will be null
        tableId - the table identifier; may not be null
        columns - the column definitions for the table that defines the row; may not be null
        filter - the filter that specifies whether columns in the table should be included; may be null if all columns are to be included
        mappers - the mapping functions for columns; may be null if none of the columns are to be mapped to different values
        Returns:
        the value-generating function, or null if there is no value schema
      • indexesForColumns

        protected int[] indexesForColumns​(List<Column> columns)
      • fieldsForColumns

        protected org.apache.kafka.connect.data.Field[] fieldsForColumns​(org.apache.kafka.connect.data.Schema schema,
                                                                         List<Column> columns)
      • convertersForColumns

        protected ValueConverter[] convertersForColumns​(org.apache.kafka.connect.data.Schema schema,
                                                        TableId tableId,
                                                        List<Column> columns,
                                                        ColumnMappers mappers)
        Obtain the array of converters for each column in a row. A converter might be null if the column is not be included in the records.
        Parameters:
        schema - the schema; may not be null
        tableId - the identifier of the table that contains the columns
        columns - the columns in the row; may not be null
        mappers - the mapping functions for columns; may be null if none of the columns are to be mapped to different values
        Returns:
        the converters for each column in the rows; never null
      • addField

        protected void addField​(org.apache.kafka.connect.data.SchemaBuilder builder,
                                Table table,
                                Column column,
                                ColumnMapper mapper)
        Add to the supplied SchemaBuilder a field for the column with the given information.
        Parameters:
        builder - the schema builder; never null
        table - the table definition; never null
        column - the column definition
        mapper - the mapping function for the column; may be null if the columns is not to be mapped to different values
      • createValueConverterFor

        protected ValueConverter createValueConverterFor​(TableId tableId,
                                                         Column column,
                                                         org.apache.kafka.connect.data.Field fieldDefn)
        Create a ValueConverter that can be used to convert row values for the given column into the Kafka Connect value object described by the field definition. This uses the supplied ValueConverterProvider object.
        Parameters:
        tableId - the id of the table containing the column; never null
        column - the column describing the input values; never null
        fieldDefn - the definition for the field in a Kafka Connect Schema describing the output of the function; never null
        Returns:
        the value conversion function; may not be null