@ThreadSafe @Immutable public class TableSchemaBuilder extends Object
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.
| Modifier and Type | Field and Description |
|---|---|
private static LocalDate |
EPOCH_DAY |
private static org.slf4j.Logger |
LOGGER |
| Constructor and Description |
|---|
TableSchemaBuilder()
Create a new instance of the builder.
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addField(org.apache.kafka.connect.data.SchemaBuilder builder,
Column column,
ColumnMapper mapper)
Add to the supplied
SchemaBuilder a field for the column with the given information. |
protected org.apache.kafka.connect.data.SchemaBuilder |
addOtherField(Column column,
ColumnMapper mapper)
Return a
SchemaBuilder for a field for the column with the given information. |
protected Object |
convertBigInt(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.INTEGER. |
protected Object |
convertBinary(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.BLOB, Types.BINARY,
Types.VARBINARY, Types.LONGVARBINARY. |
protected Object |
convertBit(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.BIT. |
protected Object |
convertBoolean(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.BOOLEAN. |
protected Object |
convertDate(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.DATE. |
protected Object |
convertDecimal(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.NUMERIC. |
protected Object |
convertDouble(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.DOUBLE. |
protected ValueConverter[] |
convertersForColumns(org.apache.kafka.connect.data.Schema schema,
TableId tableId,
List<Column> columns,
Predicate<ColumnId> filter,
ColumnMappers mappers)
Obtain the array of converters for each column in a row.
|
protected Object |
convertFloat(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.FLOAT. |
protected Object |
convertInteger(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.INTEGER. |
protected Object |
convertNumeric(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.NUMERIC. |
protected Object |
convertReal(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.REAL. |
protected Object |
convertRowId(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.ROWID. |
protected Object |
convertSmallInt(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.SMALLINT. |
protected Object |
convertString(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.CHAR, Types.VARCHAR,
Types.LONGVARCHAR, Types.CLOB, Types.NCHAR, Types.NVARCHAR, Types.LONGNVARCHAR,
Types.NCLOB, Types.DATALINK, and Types.SQLXML. |
protected Object |
convertTime(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TIME. |
protected Object |
convertTimestamp(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TIMESTAMP. |
protected Object |
convertTimestampWithZone(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TIMESTAMP_WITH_TIMEZONE. |
protected Object |
convertTimeWithZone(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TIME_WITH_TIMEZONE. |
protected Object |
convertTinyInt(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TINYINT. |
TableSchema |
create(ResultSet resultSet,
String name)
Create a
TableSchema from the given JDBC ResultSet. |
TableSchema |
create(String schemaPrefix,
Table table)
Create a
TableSchema from the given table definition. |
TableSchema |
create(String schemaPrefix,
Table table,
Predicate<ColumnId> filter,
ColumnMappers mappers)
Create a
TableSchema from the given table definition. |
protected Function<Object[],Object> |
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.
|
protected ValueConverter |
createValueConverterFor(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. |
protected Function<Object[],org.apache.kafka.connect.data.Struct> |
createValueGenerator(org.apache.kafka.connect.data.Schema schema,
TableId tableId,
List<Column> columns,
Predicate<ColumnId> filter,
ColumnMappers mappers)
Creates the function that produces a Kafka Connect value object for a row of data.
|
protected org.apache.kafka.connect.data.Field[] |
fieldsForColumns(org.apache.kafka.connect.data.Schema schema,
List<Column> columns) |
protected Object |
handleUnknownData(Column column,
org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Convert a value object from the specified column to the value type described by the specified Kafka Connect
field. |
protected int[] |
indexesForColumns(List<Column> columns) |
protected byte[] |
unexpectedBinary(Object value,
org.apache.kafka.connect.data.Field fieldDefn)
Handle the unexpected value from a row with a column type of
Types.BLOB, Types.BINARY,
Types.VARBINARY, Types.LONGVARBINARY. |
protected Date |
unexpectedDate(Object value,
org.apache.kafka.connect.data.Field fieldDefn)
Handle the unexpected value from a row with a column type of
Types.DATE. |
protected Date |
unexpectedTime(Object value,
org.apache.kafka.connect.data.Field fieldDefn)
Handle the unexpected value from a row with a column type of
Types.TIME. |
protected Date |
unexpectedTimestamp(Object value,
org.apache.kafka.connect.data.Field fieldDefn)
Handle the unexpected value from a row with a column type of
Types.TIMESTAMP. |
protected OffsetDateTime |
unexpectedTimestampWithZone(Object value,
org.apache.kafka.connect.data.Field fieldDefn)
Handle the unexpected value from a row with a column type of
Types.TIMESTAMP_WITH_TIMEZONE. |
protected OffsetTime |
unexpectedTimeWithZone(Object value,
org.apache.kafka.connect.data.Field fieldDefn)
Handle the unexpected value from a row with a column type of
Types.TIME_WITH_TIMEZONE. |
private static final org.slf4j.Logger LOGGER
private static final LocalDate EPOCH_DAY
public TableSchemaBuilder()
public TableSchema create(ResultSet resultSet, String name) throws SQLException
TableSchema from the given JDBC ResultSet. The resulting TableSchema will have no primary key,
and its TableSchema.valueSchema() will contain fields for each column in the result set.resultSet - the result set for a query; may not be nullname - the name of the value schema; may not be nullSQLException - if an error occurs while using the result set's metadatapublic TableSchema create(String schemaPrefix, Table table)
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).
schemaPrefix - the prefix added to the table identifier to construct the schema names; may be null if there is no
prefixtable - the table definition; may not be nullpublic TableSchema create(String schemaPrefix, Table table, Predicate<ColumnId> filter, ColumnMappers mappers)
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).
schemaPrefix - the prefix added to the table identifier to construct the schema names; may be null if there is no
prefixtable - the table definition; may not be nullfilter - the filter that specifies whether columns in the table should be included; may be null if all columns
are to be includedmappers - the mapping functions for columns; may be null if none of the columns are to be mapped to different valuesprotected Function<Object[],Object> createKeyGenerator(org.apache.kafka.connect.data.Schema schema, TableId columnSetName, List<Column> columns)
schema - the Kafka Connect schema for the key; may be null if there is no known schema, in which case the generator
will be nullcolumnSetName - the name for the set of columns, used in error messages; may not be nullcolumns - the column definitions for the table that defines the row; may not be nullprotected Function<Object[],org.apache.kafka.connect.data.Struct> createValueGenerator(org.apache.kafka.connect.data.Schema schema, TableId tableId, List<Column> columns, Predicate<ColumnId> filter, ColumnMappers mappers)
schema - the Kafka Connect schema for the value; may be null if there is no known schema, in which case the generator
will be nulltableId - the table identifier; may not be nullcolumns - the column definitions for the table that defines the row; may not be nullfilter - the filter that specifies whether columns in the table should be included; may be null if all columns
are to be includedmappers - the mapping functions for columns; may be null if none of the columns are to be mapped to different valuesprotected org.apache.kafka.connect.data.Field[] fieldsForColumns(org.apache.kafka.connect.data.Schema schema,
List<Column> columns)
protected ValueConverter[] convertersForColumns(org.apache.kafka.connect.data.Schema schema, TableId tableId, List<Column> columns, Predicate<ColumnId> filter, ColumnMappers mappers)
schema - the schema; may not be nulltableId - the identifier of the table that contains the columnscolumns - the columns in the row; may not be nullfilter - the filter that specifies whether columns in the table should be included; may be null if all columns
are to be includedmappers - the mapping functions for columns; may be null if none of the columns are to be mapped to different valuesprotected void addField(org.apache.kafka.connect.data.SchemaBuilder builder,
Column column,
ColumnMapper mapper)
SchemaBuilder a field for the column with the given information.builder - the schema builder; never nullcolumn - the column definitionmapper - the mapping function for the column; may be null if the columns is not to be mapped to different valuesprotected org.apache.kafka.connect.data.SchemaBuilder addOtherField(Column column, ColumnMapper mapper)
SchemaBuilder for a field for the column with the given information.
Subclasses that wish to override or extend the mappings of JDBC/DBMS types to Kafka Connect value types can override
this method and delegate to this method before and/or after the custom logic. Similar behavior should be addressed
in a specialized addField(SchemaBuilder, Column, ColumnMapper) as well.
column - the columnmapper - the mapping function for the column; may be null if the columns is not to be mapped to different valuesSchemaBuilder for the new field, ready to be build; may be nullprotected ValueConverter createValueConverterFor(Column column, org.apache.kafka.connect.data.Field fieldDefn)
ValueConverter that can be used to convert row values for the given column into the Kafka Connect value
object described by the field definition.
Subclasses can override this method to specialize the behavior. The subclass method should do custom checks and conversions, and then delegate to this method implementation to handle all other cases.
Alternatively, subclasses can leave this method as-is and instead override one of the lower-level type-specific methods
that this method calls (e.g., convertBinary(Column, Field, Object),
convertTinyInt(Column, Field, Object), etc.).
column - the column describing the input values; never nullfieldDefn - the definition for the field in a Kafka Connect Schema describing the output of the function;
never nullprotected Object handleUnknownData(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
field.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertTimestampWithZone(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.TIMESTAMP_WITH_TIMEZONE.
The standard ANSI to Java 8 type
mappings specify that the preferred mapping (when using JDBC's getObject(...)
methods) in Java 8 is to return OffsetDateTime for these values.
This method handles several types of objects, including OffsetDateTime, Timestamp,
Date, LocalTime, and LocalDateTime.
column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected OffsetDateTime unexpectedTimestampWithZone(Object value, org.apache.kafka.connect.data.Field fieldDefn)
Types.TIMESTAMP_WITH_TIMEZONE.value - the timestamp value for which no conversion was found; never nullfieldDefn - the field definition in the Kafka Connect schema; never nullprotected Object convertTimeWithZone(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.TIME_WITH_TIMEZONE.
The standard ANSI to Java 8 type
mappings specify that the preferred mapping (when using JDBC's getObject(...)
methods) in Java 8 is to return OffsetTime for these values.
This method handles several types of objects, including OffsetTime, Time, Date,
LocalTime, and LocalDateTime. If any of the types have date components, those date
components are ignored.
column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected OffsetTime unexpectedTimeWithZone(Object value, org.apache.kafka.connect.data.Field fieldDefn)
Types.TIME_WITH_TIMEZONE.value - the timestamp value for which no conversion was found; never nullfieldDefn - the field definition in the Kafka Connect schema; never nullprotected Object convertTimestamp(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.TIMESTAMP.
Per the JDBC specification, databases should return Timestamp instances, which have date and time info
but no time zone info. This method handles Date objects plus any other standard date-related objects such
as Date, LocalTime, and LocalDateTime.
column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Date unexpectedTimestamp(Object value, org.apache.kafka.connect.data.Field fieldDefn)
Types.TIMESTAMP.value - the timestamp value for which no conversion was found; never nullfieldDefn - the field definition in the Kafka Connect schema; never nullprotected Object convertTime(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.TIME.
Per the JDBC specification, databases should return Time instances that have no notion of date or
time zones. This method handles Date objects plus any other standard date-related objects such as
Date, LocalTime, and LocalDateTime. If any of the types might
have date components, those date components are ignored.
column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Date unexpectedTime(Object value, org.apache.kafka.connect.data.Field fieldDefn)
Types.TIME.value - the timestamp value for which no conversion was found; never nullfieldDefn - the field definition in the Kafka Connect schema; never nullprotected Object convertDate(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.DATE.
Per the JDBC specification, databases should return Date instances that have no notion of time or
time zones. This method handles Date objects plus any other standard date-related objects such as
Date, LocalDate, and LocalDateTime. If any of the types might
have time components, those time components are ignored.
column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Date unexpectedDate(Object value, org.apache.kafka.connect.data.Field fieldDefn)
Types.DATE.value - the timestamp value for which no conversion was found; never nullfieldDefn - the field definition in the Kafka Connect schema; never nullprotected Object convertBinary(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.BLOB, Types.BINARY,
Types.VARBINARY, Types.LONGVARBINARY.
Per the JDBC specification, databases should return Date instances that have no notion of time or
time zones. This method handles Date objects plus any other standard date-related objects such as
Date, LocalDate, and LocalDateTime. If any of the types might
have time components, those time components are ignored.
column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected byte[] unexpectedBinary(Object value, org.apache.kafka.connect.data.Field fieldDefn)
Types.BLOB, Types.BINARY,
Types.VARBINARY, Types.LONGVARBINARY.value - the binary value for which no conversion was found; never nullfieldDefn - the field definition in the Kafka Connect schema; never nullconvertBinary(Column, Field, Object)protected Object convertTinyInt(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.TINYINT.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertSmallInt(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.SMALLINT.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertInteger(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.INTEGER.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertBigInt(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.INTEGER.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertFloat(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.FLOAT.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertDouble(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.DOUBLE.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertReal(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.REAL.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertNumeric(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.NUMERIC.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertDecimal(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.NUMERIC.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertString(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.CHAR, Types.VARCHAR,
Types.LONGVARCHAR, Types.CLOB, Types.NCHAR, Types.NVARCHAR, Types.LONGNVARCHAR,
Types.NCLOB, Types.DATALINK, and Types.SQLXML.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertRowId(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.ROWID.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertBit(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.BIT.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullprotected Object convertBoolean(Column column, org.apache.kafka.connect.data.Field fieldDefn, Object data)
Types.BOOLEAN.column - the column definition describing the data value; never nullfieldDefn - the field definition; never nulldata - the data object to be converted into a Kafka Connect date type; never nullCopyright © 2016 JBoss by Red Hat. All rights reserved.