You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "sandynz (via GitHub)" <gi...@apache.org> on 2023/03/16 09:06:43 UTC

[GitHub] [shardingsphere] sandynz commented on a diff in pull request #24650: Enable CDC E2E IT

sandynz commented on code in PR #24650:
URL: https://github.com/apache/shardingsphere/pull/24650#discussion_r1138328973


##########
test/e2e/pipeline/src/test/java/org/apache/shardingsphere/test/e2e/data/pipeline/cases/cdc/DataSourceRecordConsumer.java:
##########
@@ -0,0 +1,198 @@
+/*
+ * 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.shardingsphere.test.e2e.data.pipeline.cases.cdc;
+
+import com.google.common.base.Strings;
+import com.google.protobuf.InvalidProtocolBufferException;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.data.pipeline.api.datasource.PipelineDataSourceWrapper;
+import org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineColumnMetaData;
+import org.apache.shardingsphere.data.pipeline.api.metadata.model.PipelineTableMetaData;
+import org.apache.shardingsphere.data.pipeline.cdc.client.util.ProtobufAnyValueConverter;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record.DataChangeType;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.DataRecordResult.Record.MetaData;
+import org.apache.shardingsphere.data.pipeline.cdc.protocol.response.TableColumn;
+import org.apache.shardingsphere.data.pipeline.core.metadata.loader.StandardPipelineTableMetaDataLoader;
+import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.test.e2e.data.pipeline.util.SQLBuilderUtil;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.Date;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.time.LocalDate;
+import java.time.LocalTime;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+/**
+ * Data source record consumer.
+ */
+@Slf4j
+public final class DataSourceRecordConsumer implements Consumer<List<Record>> {
+    
+    private final DataSource dataSource;
+    
+    private final Map<String, PipelineTableMetaData> tableMetaDataMap;
+    
+    private final StandardPipelineTableMetaDataLoader loader;
+    
+    public DataSourceRecordConsumer(final DataSource dataSource, final DatabaseType databaseType) {
+        this.dataSource = dataSource;
+        tableMetaDataMap = new ConcurrentHashMap<>();
+        loader = new StandardPipelineTableMetaDataLoader(new PipelineDataSourceWrapper(dataSource, databaseType));
+    }
+    
+    @Override
+    public void accept(final List<Record> records) {
+        long insertCount = records.stream().filter(each -> DataChangeType.INSERT == each.getDataChangeType()).count();
+        if (insertCount == records.size()) {
+            Record firstRecord = records.get(0);
+            MetaData metaData = firstRecord.getMetaData();
+            PipelineTableMetaData tableMetaData = loadTableMetaData(metaData.getSchema(), metaData.getTable());
+            List<String> columnNames = firstRecord.getAfterList().stream().map(TableColumn::getName).collect(Collectors.toList());
+            String tableName = buildTableNameWithSchema(metaData.getSchema(), metaData.getTable());
+            String insertSQL = SQLBuilderUtil.buildInsertSQL(columnNames, tableName);
+            try (
+                    Connection connection = dataSource.getConnection();
+                    PreparedStatement preparedStatement = connection.prepareStatement(insertSQL)) {
+                for (Record each : records) {
+                    List<TableColumn> tableColumns = each.getAfterList();
+                    for (int i = 0; i < tableColumns.size(); i++) {
+                        preparedStatement.setObject(i + 1, convertValueFromAny(tableMetaData, tableColumns.get(i)));
+                    }
+                    preparedStatement.addBatch();
+                }
+                preparedStatement.executeBatch();
+            } catch (final SQLException ex) {
+                throw new RuntimeException(ex);
+            }

Review Comment:
   It's better to extract this code block to `batchInsertRecords`



-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org