@NotThreadSafe public class MySqlSchema extends Object
table definitions and the Kafka Connect Schemas for each table, where the
Schema excludes any columns that have been specified in the
configuration.
The history is changed by applying DDL
statements, and every change is persisted as defined in the supplied MySQL
connector configuration. This component can be reconstructed (e.g., on connector restart) and the history
loaded from persisted storage.
Note that when applying DDL statements, the
caller is able to supply a consumer function that will be called with the DDL
statements and the database to which they apply, grouped by database names. However, these will only be called based when the
databases are included by the database filters defined in the MySQL connector configuration.
| Modifier and Type | Field and Description |
|---|---|
private DatabaseHistory |
dbHistory |
private DdlChanges |
ddlChanges |
private MySqlDdlParser |
ddlParser |
private Filters |
filters |
private HistoryRecordComparator |
historyComparator |
private Set<String> |
ignoredQueryStatements |
private org.slf4j.Logger |
logger |
private TableSchemaBuilder |
schemaBuilder |
private AvroValidator |
schemaNameValidator |
private String |
schemaPrefix |
private String |
serverName |
private Tables |
tables |
private Map<TableId,TableSchema> |
tableSchemaByTableId |
| Constructor and Description |
|---|
MySqlSchema(Configuration config,
String serverName,
Predicate<String> gtidFilter)
Create a schema component given the supplied
MySQL connector configuration. |
| Modifier and Type | Method and Description |
|---|---|
protected void |
appendCreateTableStatement(StringBuilder sb,
Table table) |
protected void |
appendDropTableStatement(StringBuilder sb,
TableId tableId) |
boolean |
applyDdl(SourceInfo source,
String databaseName,
String ddlStatements,
DdlChanges.DatabaseStatementStringConsumer statementConsumer)
Apply the supplied DDL statements to this database schema and record the history.
|
protected void |
changeTablesAndRecordInHistory(SourceInfo source,
Callable<Void> changeFunction)
Apply the given function to change or alter the current set of table definitions, and record the new state of the table
definitions in the database history by dropping tables that were removed and dropping and re-creating tables that were
changed.
|
Filters |
filters()
Get the
database and table filters defined by the configuration. |
protected HistoryRecordComparator |
historyComparator() |
String |
historyLocation()
Get the information about where the DDL statement history is recorded.
|
void |
loadFromDatabase(JdbcConnection jdbc,
SourceInfo source)
Load the schema for the databases using JDBC database metadata.
|
void |
loadHistory(SourceInfo startingPoint)
Load the database schema information using the previously-recorded history, and stop reading the history when the
the history reaches the supplied starting point.
|
protected void |
refreshSchemas()
Discard any currently-cached schemas and rebuild them using the filters.
|
TableSchema |
schemaFor(TableId id)
Get the
Schema information for the table with the given identifier, if that table exists and is
included by the filter. |
void |
setSystemVariables(Map<String,String> variables)
Set the system variables on the DDL parser.
|
void |
shutdown()
Stop recording history and release any resources acquired since
start(). |
void |
start()
Start by acquiring resources needed to persist the database history
|
MySqlSystemVariables |
systemVariables()
Get the system variables as known by the DDL parser.
|
Table |
tableFor(TableId id)
Get the
Schema information for the table with the given identifier, if that table exists and is
included by the filter. |
Tables |
tables()
Get all of the table definitions for all database tables as defined by
applied DDL statements, including those
that have been excluded by the filters. |
private final org.slf4j.Logger logger
private final AvroValidator schemaNameValidator
private final MySqlDdlParser ddlParser
private final Map<TableId,TableSchema> tableSchemaByTableId
private final Filters filters
private final DatabaseHistory dbHistory
private final TableSchemaBuilder schemaBuilder
private final DdlChanges ddlChanges
private final String serverName
private final String schemaPrefix
private final HistoryRecordComparator historyComparator
private Tables tables
public MySqlSchema(Configuration config, String serverName, Predicate<String> gtidFilter)
MySQL connector configuration.config - the connector configuration, which is presumed to be validserverName - the name of the servergtidFilter - the predicate function that should be applied to GTID sets in database history, and which
returns true if a GTID source is to be included, or false if a GTID source is to be excluded;
may be null if not neededprotected HistoryRecordComparator historyComparator()
public void start()
public void shutdown()
start().public Filters filters()
database and table filters defined by the configuration.public Tables tables()
applied DDL statements, including those
that have been excluded by the filters.public Table tableFor(TableId id)
Schema information for the table with the given identifier, if that table exists and is
included by the filter.id - the fully-qualified table identifier; may be nullpublic TableSchema schemaFor(TableId id)
Schema information for the table with the given identifier, if that table exists and is
included by the filter.
Note that the Schema will not contain any columns that have been filtered out.
id - the fully-qualified table identifier; may be nullpublic String historyLocation()
public void setSystemVariables(Map<String,String> variables)
variables - the system variables; may not be null but may be emptypublic MySqlSystemVariables systemVariables()
public void loadFromDatabase(JdbcConnection jdbc, SourceInfo source) throws SQLException
schemas for the affected tables are updated.jdbc - the JDBC connection; may not be nullsource - the source information that should be recorded with the history; may not be nullSQLException - if there is a failure reading the JDBC database metadataprotected void changeTablesAndRecordInHistory(SourceInfo source, Callable<Void> changeFunction) throws SQLException
This method is written this way so that the complex logic can be easily tested without actually requiring a database.
source - the source information that should be recorded with the history; may not be nullchangeFunction - the function that changes the table definitions and returns true if at least one table
definition was modified in some way; may not be nullSQLException - if there is a failure reading the JDBC database metadataprotected void appendDropTableStatement(StringBuilder sb, TableId tableId)
protected void appendCreateTableStatement(StringBuilder sb, Table table)
public void loadHistory(SourceInfo startingPoint)
startingPoint - the source information with the current SourceInfo.partition() and offset at which the database schemas are to reflect; may not be nullprotected void refreshSchemas()
public boolean applyDdl(SourceInfo source, String databaseName, String ddlStatements, DdlChanges.DatabaseStatementStringConsumer statementConsumer)
statementConsumer is
supplied, then call it for each sub-sequence of the DDL statements that all apply to the same database.
Typically DDL statements are applied using a connection to a single database, and unless the statements use fully-qualified names, the DDL statements apply to this database.
source - the current SourceInfo.partition() and offset at which these changes are
found; may not be nulldatabaseName - the name of the default database under which these statements are applied; may not be nullddlStatements - the ;-separated DDL statements; may be null or emptystatementConsumer - the consumer that should be called with each sub-sequence of DDL statements that apply to
a single database; may be null if no action is to be performed with the changestrue if changes were made to the database schema, or false if the DDL statements had no
effect on the database schemaCopyright © 2017 JBoss by Red Hat. All rights reserved.