@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 | Class and Description |
|---|---|
protected static interface |
TableSchemaBuilder.ValueConverter
A function that converts from a column data value into a Kafka Connect object that is compliant with the Kafka Connect
Schema's corresponding Field. |
| 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)
Add to the supplied
SchemaBuilder a field for the column with the given information. |
protected void |
addField(org.apache.kafka.connect.data.SchemaBuilder builder,
String columnName,
int jdbcType,
String typeName,
int columnLength,
int columnScale,
boolean optional)
Add to the supplied
SchemaBuilder a field for the column with the given information. |
protected void |
addOtherField(org.apache.kafka.connect.data.SchemaBuilder builder,
String columnName,
int jdbcType,
String typeName,
int columnLength,
int columnScale,
boolean optional)
Add to the supplied
SchemaBuilder a field for the column with the given information. |
protected Object |
convertDate(org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.DATE. |
protected TableSchemaBuilder.ValueConverter[] |
convertersForColumns(org.apache.kafka.connect.data.Schema schema,
List<Column> columns) |
protected Object |
convertTime(org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TIME. |
protected Object |
convertTimestamp(org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TIMESTAMP. |
protected Object |
convertTimestampWithZone(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(org.apache.kafka.connect.data.Field fieldDefn,
Object data)
Converts a value object for an expected JDBC type of
Types.TIME_WITH_TIMEZONE. |
TableSchema |
create(ResultSet resultSet,
String name)
Create a
TableSchema from the given JDBC ResultSet. |
TableSchema |
create(Table table)
Create a
TableSchema from the given table definition. |
protected Function<Object[],Object> |
createKeyGenerator(org.apache.kafka.connect.data.Schema schema,
String columnSetName,
List<Column> columns)
Creates the function that produces a Kafka Connect key object for a row of data.
|
protected TableSchemaBuilder.ValueConverter |
createValueConverterFor(Column column,
org.apache.kafka.connect.data.Field fieldDefn)
Create a
TableSchemaBuilder.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,
String columnSetName,
List<Column> columns)
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 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(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).
table - the table definition; may not be nullprotected Function<Object[],Object> createKeyGenerator(org.apache.kafka.connect.data.Schema schema, String 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, String columnSetName, List<Column> columns)
schema - the Kafka Connect schema for the value; 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 org.apache.kafka.connect.data.Field[] fieldsForColumns(org.apache.kafka.connect.data.Schema schema,
List<Column> columns)
protected TableSchemaBuilder.ValueConverter[] convertersForColumns(org.apache.kafka.connect.data.Schema schema, List<Column> columns)
protected void addField(org.apache.kafka.connect.data.SchemaBuilder builder,
Column column)
SchemaBuilder a field for the column with the given information.builder - the schema builder; never nullcolumn - the column definitionprotected void addField(org.apache.kafka.connect.data.SchemaBuilder builder,
String columnName,
int jdbcType,
String typeName,
int columnLength,
int columnScale,
boolean optional)
SchemaBuilder 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 createValueConverterFor(Column, Field) as well.
builder - the schema builder; never nullcolumnName - the name of the columnjdbcType - the column's JDBC typetypeName - the column's DBMS-specific type namecolumnLength - the length of the columncolumnScale - the scale of the column values, or 0 if not a decimal valueoptional - true if the column is optional, or false if the column is known to always have a valueprotected void addOtherField(org.apache.kafka.connect.data.SchemaBuilder builder,
String columnName,
int jdbcType,
String typeName,
int columnLength,
int columnScale,
boolean optional)
SchemaBuilder 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, String, int, String, int, int, boolean) as well.
builder - the schema builder; never nullcolumnName - the name of the columnjdbcType - the column's JDBC typetypeName - the column's DBMS-specific type namecolumnLength - the length of the columncolumnScale - the scale of the column values, or 0 if not a decimal valueoptional - true if the column is optional, or false if the column is known to always have a valueprotected TableSchemaBuilder.ValueConverter createValueConverterFor(Column column, org.apache.kafka.connect.data.Field fieldDefn)
TableSchemaBuilder.ValueConverter that can be used to convert row values for the given column into the Kafka Connect value
object described by the field definition.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(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.
fieldDefn - 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(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.
fieldDefn - 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(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.
fieldDefn - 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(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.
fieldDefn - 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(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.
fieldDefn - 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 nullCopyright © 2016 JBoss by Red Hat. All rights reserved.