-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Improve][Connector-V2] Support schema evolution for mysql-cdc and my…
…sql-jdbc (#6929)
- Loading branch information
Showing
37 changed files
with
2,517 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...in/java/org/apache/seatunnel/connectors/cdc/base/schema/AbstractSchemaChangeResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.seatunnel.connectors.cdc.base.schema; | ||
|
||
import org.apache.seatunnel.connectors.cdc.base.config.JdbcSourceConfig; | ||
import org.apache.seatunnel.connectors.cdc.base.utils.SourceRecordUtils; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.kafka.connect.data.Struct; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
|
||
import com.google.common.collect.Lists; | ||
import io.debezium.relational.history.HistoryRecord; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
public abstract class AbstractSchemaChangeResolver implements SchemaChangeResolver { | ||
|
||
protected static final List<String> SUPPORT_DDL = Lists.newArrayList("ALTER TABLE"); | ||
|
||
protected JdbcSourceConfig jdbcSourceConfig; | ||
|
||
public AbstractSchemaChangeResolver(JdbcSourceConfig jdbcSourceConfig) { | ||
this.jdbcSourceConfig = jdbcSourceConfig; | ||
} | ||
|
||
@Override | ||
public boolean support(SourceRecord record) { | ||
String ddl = SourceRecordUtils.getDdl(record); | ||
Struct value = (Struct) record.value(); | ||
List<Struct> tableChanges = value.getArray(HistoryRecord.Fields.TABLE_CHANGES); | ||
if (tableChanges == null || tableChanges.isEmpty()) { | ||
log.warn("Ignoring statement for non-captured table {}", ddl); | ||
return false; | ||
} | ||
return StringUtils.isNotBlank(ddl) | ||
&& SUPPORT_DDL.stream() | ||
.map(String::toUpperCase) | ||
.anyMatch(prefix -> ddl.toUpperCase().contains(prefix)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...org/apache/seatunnel/connectors/seatunnel/cdc/mysql/source/MySqlSchemaChangeResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.seatunnel.connectors.seatunnel.cdc.mysql.source; | ||
|
||
import org.apache.seatunnel.api.table.catalog.TableIdentifier; | ||
import org.apache.seatunnel.api.table.catalog.TablePath; | ||
import org.apache.seatunnel.api.table.event.AlterTableColumnEvent; | ||
import org.apache.seatunnel.api.table.event.AlterTableColumnsEvent; | ||
import org.apache.seatunnel.api.table.event.SchemaChangeEvent; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelDataType; | ||
import org.apache.seatunnel.connectors.cdc.base.config.JdbcSourceConfig; | ||
import org.apache.seatunnel.connectors.cdc.base.config.SourceConfig; | ||
import org.apache.seatunnel.connectors.cdc.base.schema.AbstractSchemaChangeResolver; | ||
import org.apache.seatunnel.connectors.cdc.base.utils.SourceRecordUtils; | ||
import org.apache.seatunnel.connectors.seatunnel.cdc.mysql.source.parser.CustomMySqlAntlrDdlParser; | ||
import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.DatabaseIdentifier; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.kafka.connect.source.SourceRecord; | ||
|
||
import io.debezium.relational.Tables; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class MySqlSchemaChangeResolver extends AbstractSchemaChangeResolver { | ||
private transient Tables tables; | ||
private transient CustomMySqlAntlrDdlParser customMySqlAntlrDdlParser; | ||
|
||
public MySqlSchemaChangeResolver(SourceConfig.Factory<JdbcSourceConfig> sourceConfigFactory) { | ||
super(sourceConfigFactory.create(0)); | ||
} | ||
|
||
@Override | ||
public SchemaChangeEvent resolve(SourceRecord record, SeaTunnelDataType dataType) { | ||
TablePath tablePath = SourceRecordUtils.getTablePath(record); | ||
String ddl = SourceRecordUtils.getDdl(record); | ||
if (Objects.isNull(customMySqlAntlrDdlParser)) { | ||
this.customMySqlAntlrDdlParser = | ||
new CustomMySqlAntlrDdlParser( | ||
tablePath, this.jdbcSourceConfig.getDbzConnectorConfig()); | ||
} | ||
if (Objects.isNull(tables)) { | ||
this.tables = new Tables(); | ||
} | ||
customMySqlAntlrDdlParser.setCurrentDatabase(tablePath.getDatabaseName()); | ||
customMySqlAntlrDdlParser.setCurrentSchema(tablePath.getSchemaName()); | ||
// Parse DDL statement using Debezium's Antlr parser | ||
customMySqlAntlrDdlParser.parse(ddl, tables); | ||
List<AlterTableColumnEvent> parsedEvents = | ||
customMySqlAntlrDdlParser.getAndClearParsedEvents(); | ||
AlterTableColumnsEvent alterTableColumnsEvent = | ||
new AlterTableColumnsEvent( | ||
TableIdentifier.of( | ||
StringUtils.EMPTY, | ||
tablePath.getDatabaseName(), | ||
tablePath.getSchemaName(), | ||
tablePath.getTableName()), | ||
parsedEvents); | ||
alterTableColumnsEvent.setStatement(ddl); | ||
alterTableColumnsEvent.setSourceDialectName(DatabaseIdentifier.MYSQL); | ||
return parsedEvents.isEmpty() ? null : alterTableColumnsEvent; | ||
} | ||
} |
Oops, something went wrong.