lvyanquan commented on code in PR #3360:
URL: https://github.com/apache/flink-cdc/pull/3360#discussion_r1839804631


##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseUtils.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.event.TableId;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.types.BigIntType;
+import org.apache.flink.cdc.common.types.BooleanType;
+import org.apache.flink.cdc.common.types.CharType;
+import org.apache.flink.cdc.common.types.DataType;
+import org.apache.flink.cdc.common.types.DataTypeDefaultVisitor;
+import org.apache.flink.cdc.common.types.DateType;
+import org.apache.flink.cdc.common.types.DecimalType;
+import org.apache.flink.cdc.common.types.DoubleType;
+import org.apache.flink.cdc.common.types.FloatType;
+import org.apache.flink.cdc.common.types.IntType;
+import org.apache.flink.cdc.common.types.LocalZonedTimestampType;
+import org.apache.flink.cdc.common.types.SmallIntType;
+import org.apache.flink.cdc.common.types.TimestampType;
+import org.apache.flink.cdc.common.types.TinyIntType;
+import org.apache.flink.cdc.common.types.VarCharType;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseColumn;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseTable;
+
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** Utilities for conversion from source table to OceanBase table. */
+public class OceanBaseUtils {
+
+    /** Convert a source table to {@link OceanBaseTable}. */
+    public static OceanBaseTable toOceanBaseTable(TableId tableId, Schema 
schema) {
+
+        List<Column> columns = schema.getColumns();
+        List<String> primaryKeys = schema.primaryKeys();
+        List<OceanBaseColumn> oceanBaseColumns = new ArrayList<>();
+        for (int i = 0; i < columns.size(); i++) {
+            Column column = columns.get(i);
+            OceanBaseColumn.Builder builder =
+                    new OceanBaseColumn.Builder()
+                            .setColumnName(column.getName())
+                            .setOrdinalPosition(i)
+                            .setColumnComment(column.getComment());
+            toOceanBaseDataType(column, 
primaryKeys.contains(column.getName()), builder);
+            oceanBaseColumns.add(builder.build());
+        }
+
+        OceanBaseTable.Builder tableBuilder =
+                new OceanBaseTable.Builder()
+                        .setDatabaseName(tableId.getSchemaName())
+                        .setTableName(tableId.getTableName())
+                        .setTableType(
+                                
CollectionUtils.isNotEmpty(schema.primaryKeys())
+                                        ? OceanBaseTable.TableType.PRIMARY_KEY
+                                        : 
OceanBaseTable.TableType.DUPLICATE_KEY)
+                        .setColumns(oceanBaseColumns)
+                        .setTableKeys(schema.primaryKeys())
+                        .setPartitionKeys(schema.partitionKeys())

Review Comment:
   I'm not sure here, do we need to add partitionKeys to primaryKeys? 



##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseDataSinkOptions.java:
##########
@@ -0,0 +1,173 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.annotation.Experimental;
+import org.apache.flink.cdc.common.configuration.ConfigOption;
+import org.apache.flink.cdc.common.configuration.ConfigOptions;
+
+import 
com.alipay.oceanbase.rpc.protocol.payload.impl.direct_load.ObLoadDupActionType;
+
+import java.time.Duration;
+
+/** Options for {@link OceanBaseDataSink}. */
+public class OceanBaseDataSinkOptions {
+    // 
------------------------------------------------------------------------------------------
+    // Options for sink connector
+    // 
------------------------------------------------------------------------------------------
+    public static final ConfigOption<String> URL =
+            ConfigOptions.key("url")
+                    .stringType()
+                    .noDefaultValue()
+                    .withDescription("The connection URL.");
+
+    public static final ConfigOption<String> USERNAME =
+            ConfigOptions.key("username")
+                    .stringType()
+                    .noDefaultValue()
+                    .withDescription("The username.");
+
+    public static final ConfigOption<String> PASSWORD =
+            ConfigOptions.key("password")
+                    .stringType()
+                    .noDefaultValue()
+                    .withDescription("The password.");
+
+    public static final ConfigOption<String> DRIVER_CLASS_NAME =
+            ConfigOptions.key("driver-class-name")
+                    .stringType()
+                    .defaultValue("com.mysql.cj.jdbc.Driver")
+                    .withDescription(
+                            "JDBC driver class name, use 
'com.mysql.cj.jdbc.Driver' by default.");
+
+    public static final ConfigOption<String> DRUID_PROPERTIES =
+            ConfigOptions.key("druid-properties")
+                    .stringType()
+                    .noDefaultValue()
+                    .withDescription("Properties for specific connection 
pool.");
+
+    public static final ConfigOption<Boolean> MEMSTORE_CHECK_ENABLED =
+            ConfigOptions.key("memstore-check.enabled")
+                    .booleanType()
+                    .defaultValue(true)
+                    .withDescription("Whether enable memstore check. Default 
value is 'true'");
+
+    public static final ConfigOption<Double> MEMSTORE_THRESHOLD =
+            ConfigOptions.key("memstore-check.threshold")
+                    .doubleType()
+                    .defaultValue(0.9)
+                    .withDescription(
+                            "Memory usage threshold ratio relative to the 
limit value. Default value is '0.9'.");
+
+    public static final ConfigOption<Duration> MEMSTORE_CHECK_INTERVAL =
+            ConfigOptions.key("memstore-check.interval")
+                    .durationType()
+                    .defaultValue(Duration.ofSeconds(30))
+                    .withDescription(
+                            "The check interval, over this time, the writer 
will check if memstore reaches threshold. Default value is '30s'.");
+
+    public static final ConfigOption<Boolean> PARTITION_ENABLED =
+            ConfigOptions.key("partition.enabled")
+                    .booleanType()
+                    .defaultValue(false)
+                    .withDescription(
+                            "Whether to enable partition calculation and flush 
records by partitions. Default value is 'false'.");
+
+    @Experimental
+    public static final ConfigOption<Boolean> DIRECT_LOAD_ENABLED =
+            ConfigOptions.key("direct-load.enabled")
+                    .booleanType()
+                    .defaultValue(false)
+                    .withDescription("Whether to enable direct load.");
+
+    @Experimental
+    public static final ConfigOption<String> DIRECT_LOAD_HOST =
+            ConfigOptions.key("direct-load.host")
+                    .stringType()
+                    .noDefaultValue()
+                    .withDescription("Hostname used in direct load.");
+
+    @Experimental
+    public static final ConfigOption<Integer> DIRECT_LOAD_PORT =
+            ConfigOptions.key("direct-load.port")
+                    .intType()
+                    .defaultValue(2882)
+                    .withDescription("Rpc port number used in direct load.");
+
+    @Experimental
+    public static final ConfigOption<Integer> DIRECT_LOAD_PARALLEL =
+            ConfigOptions.key("direct-load.parallel")
+                    .intType()
+                    .defaultValue(8)
+                    .withDescription("Parallelism of direct load.");
+
+    @Experimental
+    public static final ConfigOption<Long> DIRECT_LOAD_MAX_ERROR_ROWS =
+            ConfigOptions.key("direct-load.max-error-rows")
+                    .longType()
+                    .defaultValue(0L)
+                    .withDescription("Maximum tolerable number of error 
rows.");
+
+    @Experimental
+    public static final ConfigOption<ObLoadDupActionType> 
DIRECT_LOAD_DUP_ACTION =
+            ConfigOptions.key("direct-load.dup-action")
+                    .enumType(ObLoadDupActionType.class)
+                    .defaultValue(ObLoadDupActionType.REPLACE)
+                    .withDescription("Action when there is duplicated record 
in direct load.");
+
+    @Experimental
+    public static final ConfigOption<Duration> DIRECT_LOAD_TIMEOUT =
+            ConfigOptions.key("direct-load.timeout")
+                    .durationType()
+                    .defaultValue(Duration.ofDays(7))
+                    .withDescription("Timeout for direct load task.");
+
+    @Experimental
+    public static final ConfigOption<Duration> DIRECT_LOAD_HEARTBEAT_TIMEOUT =
+            ConfigOptions.key("direct-load.heartbeat-timeout")
+                    .durationType()
+                    .defaultValue(Duration.ofSeconds(30))
+                    .withDescription("Client heartbeat timeout in direct load 
task.");
+
+    public static final ConfigOption<Boolean> SYNC_WRITE =
+            ConfigOptions.key("sync-write")
+                    .booleanType()
+                    .defaultValue(false)
+                    .withDescription("Whether to write synchronously.");
+
+    public static final ConfigOption<Duration> BUFFER_FLUSH_INTERVAL =
+            ConfigOptions.key("buffer-flush.interval")
+                    .durationType()
+                    .defaultValue(Duration.ofSeconds(1))
+                    .withDescription(
+                            "The flush interval, over this time, asynchronous 
threads will flush data. Default value is '1s'. "
+                                    + "If it's set to zero value like '0', 
scheduled flushing will be disabled.");
+
+    public static final ConfigOption<Integer> BUFFER_SIZE =
+            ConfigOptions.key("buffer-flush.buffer-size")
+                    .intType()
+                    .defaultValue(1000)
+                    .withDescription("Buffer size. Default value is '1000'.");
+
+    public static final ConfigOption<Integer> MAX_RETRIES =
+            ConfigOptions.key("max-retries")

Review Comment:
   I didn't see any place of using this option, then I found it was used in 
fink-connector-oceanbase-1.2, so can we use 
`ConfigOptions.key(ConnectorOptions.MAX_RETRIES.key())` or add some comments to 
trace it.



##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseUtils.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.event.TableId;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.types.BigIntType;
+import org.apache.flink.cdc.common.types.BooleanType;
+import org.apache.flink.cdc.common.types.CharType;
+import org.apache.flink.cdc.common.types.DataType;
+import org.apache.flink.cdc.common.types.DataTypeDefaultVisitor;
+import org.apache.flink.cdc.common.types.DateType;
+import org.apache.flink.cdc.common.types.DecimalType;
+import org.apache.flink.cdc.common.types.DoubleType;
+import org.apache.flink.cdc.common.types.FloatType;
+import org.apache.flink.cdc.common.types.IntType;
+import org.apache.flink.cdc.common.types.LocalZonedTimestampType;
+import org.apache.flink.cdc.common.types.SmallIntType;
+import org.apache.flink.cdc.common.types.TimestampType;
+import org.apache.flink.cdc.common.types.TinyIntType;
+import org.apache.flink.cdc.common.types.VarCharType;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseColumn;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseTable;
+
+import org.apache.commons.collections.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/** Utilities for conversion from source table to OceanBase table. */
+public class OceanBaseUtils {
+
+    /** Convert a source table to {@link OceanBaseTable}. */
+    public static OceanBaseTable toOceanBaseTable(TableId tableId, Schema 
schema) {
+
+        List<Column> columns = schema.getColumns();
+        List<String> primaryKeys = schema.primaryKeys();
+        List<OceanBaseColumn> oceanBaseColumns = new ArrayList<>();
+        for (int i = 0; i < columns.size(); i++) {
+            Column column = columns.get(i);
+            OceanBaseColumn.Builder builder =
+                    new OceanBaseColumn.Builder()
+                            .setColumnName(column.getName())
+                            .setOrdinalPosition(i)
+                            .setColumnComment(column.getComment());

Review Comment:
   Currently, We've support default value in Source, so we can add default 
value of column here now.



##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseMetadataApplier.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.event.AddColumnEvent;
+import org.apache.flink.cdc.common.event.AlterColumnTypeEvent;
+import org.apache.flink.cdc.common.event.CreateTableEvent;
+import org.apache.flink.cdc.common.event.DropColumnEvent;
+import org.apache.flink.cdc.common.event.RenameColumnEvent;
+import org.apache.flink.cdc.common.event.SchemaChangeEvent;
+import org.apache.flink.cdc.common.event.TableId;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.sink.MetadataApplier;
+import org.apache.flink.cdc.common.utils.Preconditions;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseCatalog;
+import 
org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseCatalogException;
+import 
org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseCatalogFactory;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseColumn;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseTable;
+
+import com.oceanbase.connector.flink.OceanBaseConnectorOptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/** Supports {@link OceanBaseDataSink} to schema evolution. */
+public class OceanBaseMetadataApplier implements MetadataApplier {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(OceanBaseMetadataApplier.class);
+
+    private final OceanBaseCatalog catalog;
+
+    public OceanBaseMetadataApplier(OceanBaseConnectorOptions 
connectorOptions) {
+        try {
+            this.catalog = 
OceanBaseCatalogFactory.createOceanBaseCatalog(connectorOptions);
+            catalog.open();
+        } catch (Exception e) {
+            throw new OceanBaseCatalogException("Fail to init 
OceanBaseMetadataApplier.", e);
+        }
+    }
+
+    @Override
+    public void applySchemaChange(SchemaChangeEvent event) {

Review Comment:
   Could you add a test for this method?



##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseEventSerializationSchema.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.data.RecordData;
+import org.apache.flink.cdc.common.event.CreateTableEvent;
+import org.apache.flink.cdc.common.event.DataChangeEvent;
+import org.apache.flink.cdc.common.event.Event;
+import org.apache.flink.cdc.common.event.OperationType;
+import org.apache.flink.cdc.common.event.SchemaChangeEvent;
+import org.apache.flink.cdc.common.event.TableId;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.utils.Preconditions;
+import org.apache.flink.cdc.common.utils.SchemaUtils;
+
+import org.apache.flink.shaded.guava31.com.google.common.collect.Lists;
+
+import com.oceanbase.connector.flink.table.DataChangeRecord;
+import com.oceanbase.connector.flink.table.Record;
+import com.oceanbase.connector.flink.table.RecordSerializationSchema;
+import com.oceanbase.connector.flink.table.TableInfo;
+
+import java.time.ZoneId;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/** A serializer for Event to Record. */
+public class OceanBaseEventSerializationSchema implements 
RecordSerializationSchema<Event> {
+
+    private final Map<TableId, Schema> schemaMaps = new HashMap<>();
+
+    /** ZoneId from pipeline config to support timestamp with local time zone. 
*/
+    public final ZoneId pipelineZoneId;
+
+    public OceanBaseEventSerializationSchema(ZoneId zoneId) {
+        pipelineZoneId = zoneId;
+    }
+
+    @Override
+    public Record serialize(Event event) {
+        if (event instanceof DataChangeEvent) {
+            return applyDataChangeEvent((DataChangeEvent) event);
+        } else if (event instanceof SchemaChangeEvent) {
+            SchemaChangeEvent schemaChangeEvent = (SchemaChangeEvent) event;
+            TableId tableId = schemaChangeEvent.tableId();
+            if (event instanceof CreateTableEvent) {
+                schemaMaps.put(tableId, ((CreateTableEvent) 
event).getSchema());
+            } else {
+                if (!schemaMaps.containsKey(tableId)) {
+                    throw new RuntimeException("schema of " + tableId + " is 
not existed.");
+                }
+                schemaMaps.put(
+                        tableId,
+                        SchemaUtils.applySchemaChangeEvent(
+                                schemaMaps.get(tableId), schemaChangeEvent));
+            }
+        }
+        return null;
+    }
+
+    private Record applyDataChangeEvent(DataChangeEvent event) {
+        TableId tableId = event.tableId();
+        Schema schema = schemaMaps.get(tableId);
+        Preconditions.checkNotNull(schema, event.tableId() + " is not 
existed");
+        Object[] values;
+        OperationType op = event.op();
+        boolean isDelete = false;
+        switch (op) {
+            case INSERT:
+            case UPDATE:
+            case REPLACE:
+                values = serializerRecord(event.after(), schema);
+                break;
+            case DELETE:
+                values = serializerRecord(event.before(), schema);
+                isDelete = true;
+                break;
+            default:
+                throw new UnsupportedOperationException("Unsupported Operation 
" + op);
+        }
+        return buildDataChangeRecord(tableId, schema, values, isDelete);
+    }
+
+    private DataChangeRecord buildDataChangeRecord(
+            TableId tableId, Schema schema, Object[] values, boolean isDelete) 
{
+        Preconditions.checkState(
+                Objects.nonNull(tableId.getSchemaName()), "Schema name cannot 
be null or empty.");
+        com.oceanbase.connector.flink.table.TableId oceanBaseTableId =
+                new com.oceanbase.connector.flink.table.TableId(
+                        tableId.getSchemaName(), tableId.getTableName());
+        TableInfo tableInfo =
+                new TableInfo(
+                        oceanBaseTableId,
+                        schema.primaryKeys(),
+                        schema.getColumnNames(),
+                        Lists.newArrayList(),
+                        null);
+
+        return new DataChangeRecord(
+                tableInfo,
+                isDelete ? DataChangeRecord.Type.DELETE : 
DataChangeRecord.Type.UPSERT,
+                values);
+    }
+
+    /** serializer RecordData to oceanbase data change record. */
+    public Object[] serializerRecord(RecordData recordData, Schema schema) {
+        List<Column> columns = schema.getColumns();
+        Preconditions.checkState(
+                columns.size() == recordData.getArity(),
+                "Column size does not match the data size");
+        Object[] values = new Object[columns.size()];
+
+        for (int i = 0; i < recordData.getArity(); i++) {
+            OceanBaseRowConvert.SerializationConverter converter =

Review Comment:
   We should cache the List of OceanBaseRowConvert.SerializationConverter of 
specific Schema to avoid creating them for each recordData.



##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseEventSerializationSchema.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.data.RecordData;
+import org.apache.flink.cdc.common.event.CreateTableEvent;
+import org.apache.flink.cdc.common.event.DataChangeEvent;
+import org.apache.flink.cdc.common.event.Event;
+import org.apache.flink.cdc.common.event.OperationType;
+import org.apache.flink.cdc.common.event.SchemaChangeEvent;
+import org.apache.flink.cdc.common.event.TableId;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.utils.Preconditions;
+import org.apache.flink.cdc.common.utils.SchemaUtils;
+
+import org.apache.flink.shaded.guava31.com.google.common.collect.Lists;
+
+import com.oceanbase.connector.flink.table.DataChangeRecord;
+import com.oceanbase.connector.flink.table.Record;
+import com.oceanbase.connector.flink.table.RecordSerializationSchema;
+import com.oceanbase.connector.flink.table.TableInfo;
+
+import java.time.ZoneId;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/** A serializer for Event to Record. */
+public class OceanBaseEventSerializationSchema implements 
RecordSerializationSchema<Event> {
+
+    private final Map<TableId, Schema> schemaMaps = new HashMap<>();
+
+    /** ZoneId from pipeline config to support timestamp with local time zone. 
*/
+    public final ZoneId pipelineZoneId;
+
+    public OceanBaseEventSerializationSchema(ZoneId zoneId) {
+        pipelineZoneId = zoneId;
+    }
+
+    @Override
+    public Record serialize(Event event) {
+        if (event instanceof DataChangeEvent) {
+            return applyDataChangeEvent((DataChangeEvent) event);
+        } else if (event instanceof SchemaChangeEvent) {
+            SchemaChangeEvent schemaChangeEvent = (SchemaChangeEvent) event;
+            TableId tableId = schemaChangeEvent.tableId();
+            if (event instanceof CreateTableEvent) {
+                schemaMaps.put(tableId, ((CreateTableEvent) 
event).getSchema());
+            } else {
+                if (!schemaMaps.containsKey(tableId)) {
+                    throw new RuntimeException("schema of " + tableId + " is 
not existed.");
+                }
+                schemaMaps.put(
+                        tableId,
+                        SchemaUtils.applySchemaChangeEvent(
+                                schemaMaps.get(tableId), schemaChangeEvent));
+            }
+        }
+        return null;
+    }
+
+    private Record applyDataChangeEvent(DataChangeEvent event) {
+        TableId tableId = event.tableId();
+        Schema schema = schemaMaps.get(tableId);
+        Preconditions.checkNotNull(schema, event.tableId() + " is not 
existed");
+        Object[] values;
+        OperationType op = event.op();
+        boolean isDelete = false;
+        switch (op) {
+            case INSERT:
+            case UPDATE:
+            case REPLACE:
+                values = serializerRecord(event.after(), schema);
+                break;
+            case DELETE:
+                values = serializerRecord(event.before(), schema);
+                isDelete = true;
+                break;
+            default:
+                throw new UnsupportedOperationException("Unsupported Operation 
" + op);
+        }
+        return buildDataChangeRecord(tableId, schema, values, isDelete);
+    }
+
+    private DataChangeRecord buildDataChangeRecord(
+            TableId tableId, Schema schema, Object[] values, boolean isDelete) 
{
+        Preconditions.checkState(
+                Objects.nonNull(tableId.getSchemaName()), "Schema name cannot 
be null or empty.");
+        com.oceanbase.connector.flink.table.TableId oceanBaseTableId =
+                new com.oceanbase.connector.flink.table.TableId(
+                        tableId.getSchemaName(), tableId.getTableName());
+        TableInfo tableInfo =
+                new TableInfo(
+                        oceanBaseTableId,
+                        schema.primaryKeys(),
+                        schema.getColumnNames(),
+                        Lists.newArrayList(),
+                        null);
+
+        return new DataChangeRecord(
+                tableInfo,
+                isDelete ? DataChangeRecord.Type.DELETE : 
DataChangeRecord.Type.UPSERT,
+                values);
+    }
+
+    /** serializer RecordData to oceanbase data change record. */
+    public Object[] serializerRecord(RecordData recordData, Schema schema) {

Review Comment:
   Please add a test for this method to ensure that add support types are 
converted as expected (especially for timestamp related type). 



##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseMetadataApplier.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.event.AddColumnEvent;
+import org.apache.flink.cdc.common.event.AlterColumnTypeEvent;
+import org.apache.flink.cdc.common.event.CreateTableEvent;
+import org.apache.flink.cdc.common.event.DropColumnEvent;
+import org.apache.flink.cdc.common.event.RenameColumnEvent;
+import org.apache.flink.cdc.common.event.SchemaChangeEvent;
+import org.apache.flink.cdc.common.event.TableId;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.sink.MetadataApplier;
+import org.apache.flink.cdc.common.utils.Preconditions;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseCatalog;
+import 
org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseCatalogException;
+import 
org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseCatalogFactory;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseColumn;
+import org.apache.flink.cdc.connectors.oceanbase.catalog.OceanBaseTable;
+
+import com.oceanbase.connector.flink.OceanBaseConnectorOptions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/** Supports {@link OceanBaseDataSink} to schema evolution. */
+public class OceanBaseMetadataApplier implements MetadataApplier {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(OceanBaseMetadataApplier.class);
+
+    private final OceanBaseCatalog catalog;
+
+    public OceanBaseMetadataApplier(OceanBaseConnectorOptions 
connectorOptions) {
+        try {
+            this.catalog = 
OceanBaseCatalogFactory.createOceanBaseCatalog(connectorOptions);
+            catalog.open();
+        } catch (Exception e) {
+            throw new OceanBaseCatalogException("Fail to init 
OceanBaseMetadataApplier.", e);
+        }
+    }
+
+    @Override
+    public void applySchemaChange(SchemaChangeEvent event) {
+        try {
+            if (event instanceof CreateTableEvent) {
+                applyCreateTableEvent((CreateTableEvent) event);
+            } else if (event instanceof AddColumnEvent) {
+                applyAddColumnEvent((AddColumnEvent) event);
+            } else if (event instanceof DropColumnEvent) {
+                applyDropColumnEvent((DropColumnEvent) event);
+            } else if (event instanceof RenameColumnEvent) {
+                applyRenameColumnEvent((RenameColumnEvent) event);
+            } else if (event instanceof AlterColumnTypeEvent) {

Review Comment:
   We‘ve added `DropTableEvent` and `TruncateTableEvent`, can you help to check 
if any adjustments should be added here?



##########
flink-cdc-connect/flink-cdc-pipeline-connectors/flink-cdc-pipeline-connector-oceanbase/src/main/java/org/apache/flink/cdc/connectors/oceanbase/sink/OceanBaseEventSerializationSchema.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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.flink.cdc.connectors.oceanbase.sink;
+
+import org.apache.flink.cdc.common.data.RecordData;
+import org.apache.flink.cdc.common.event.CreateTableEvent;
+import org.apache.flink.cdc.common.event.DataChangeEvent;
+import org.apache.flink.cdc.common.event.Event;
+import org.apache.flink.cdc.common.event.OperationType;
+import org.apache.flink.cdc.common.event.SchemaChangeEvent;
+import org.apache.flink.cdc.common.event.TableId;
+import org.apache.flink.cdc.common.schema.Column;
+import org.apache.flink.cdc.common.schema.Schema;
+import org.apache.flink.cdc.common.utils.Preconditions;
+import org.apache.flink.cdc.common.utils.SchemaUtils;
+
+import org.apache.flink.shaded.guava31.com.google.common.collect.Lists;
+
+import com.oceanbase.connector.flink.table.DataChangeRecord;
+import com.oceanbase.connector.flink.table.Record;
+import com.oceanbase.connector.flink.table.RecordSerializationSchema;
+import com.oceanbase.connector.flink.table.TableInfo;
+
+import java.time.ZoneId;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/** A serializer for Event to Record. */
+public class OceanBaseEventSerializationSchema implements 
RecordSerializationSchema<Event> {

Review Comment:
   Please add a test for this class to ensure that it can work as expected when 
schema evolution happen.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to