Hisoka-X commented on code in PR #6443:
URL: https://github.com/apache/seatunnel/pull/6443#discussion_r1513978932
##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/config/JdbcOptions.java:
##########
@@ -151,6 +151,12 @@ public interface JdbcOptions {
.defaultValue(false)
.withDescription("support upsert by insert only");
+ Option<Boolean> USE_COPY_STATEMENT =
+ Options.key("use_copy_statement")
+ .booleanType()
+ .defaultValue(false)
+ .withDescription("support copy in statement ( postgresql
)");
Review Comment:
```suggestion
.withDescription("support copy in statement
(postgresql)");
```
##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/executor/CopyManagerBatchStatementExecutor.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.jdbc.internal.executor;
+
+import org.apache.seatunnel.api.table.catalog.TableSchema;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
+import
org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorException;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVPrinter;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.lang.reflect.InvocationTargetException;
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CopyManagerBatchStatementExecutor implements
JdbcBatchStatementExecutor<SeaTunnelRow> {
+
+ private final String copySql;
+ private final TableSchema tableSchema;
+ CopyManagerProxy copyManagerProxy;
+ CSVFormat csvFormat = CSVFormat.POSTGRESQL_CSV;
Review Comment:
Just a tentative question, can we implement CSV splicing ourselves without
adding new dependencies?
##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/executor/CopyManagerBatchStatementExecutor.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * 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.jdbc.internal.executor;
+
+import org.apache.seatunnel.api.table.catalog.TableSchema;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated;
+import
org.apache.seatunnel.connectors.seatunnel.jdbc.exception.JdbcConnectorException;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.CSVPrinter;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.lang.reflect.InvocationTargetException;
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.util.ArrayList;
+import java.util.List;
+
+public class CopyManagerBatchStatementExecutor implements
JdbcBatchStatementExecutor<SeaTunnelRow> {
+
+ private final String copySql;
+ private final TableSchema tableSchema;
+ CopyManagerProxy copyManagerProxy;
+ CSVFormat csvFormat = CSVFormat.POSTGRESQL_CSV;
+ CSVPrinter csvPrinter;
+
+ public CopyManagerBatchStatementExecutor(String copySql, TableSchema
tableSchema) {
+ this.copySql = copySql;
+ this.tableSchema = tableSchema;
+ }
+
+ @Override
+ public void prepareStatements(Connection connection) throws SQLException {
+ try {
+ this.copyManagerProxy = new CopyManagerProxy(connection);
+ this.csvPrinter = new CSVPrinter(new StringBuilder(), csvFormat);
+ } catch (NoSuchMethodException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void addToBatch(SeaTunnelRow record) throws SQLException {
+ try {
+ this.csvPrinter.printRecord(toExtract(record));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private List<Object> toExtract(SeaTunnelRow record) {
+ SeaTunnelRowType rowType = tableSchema.toPhysicalRowDataType();
+ List<Object> csvRecord = new ArrayList<>();
+ for (int fieldIndex = 0; fieldIndex < rowType.getTotalFields();
fieldIndex++) {
+ SeaTunnelDataType<?> seaTunnelDataType =
rowType.getFieldType(fieldIndex);
+ Object fieldValue = record.getField(fieldIndex);
+ if (fieldValue == null) {
+ csvRecord.add(null);
+ continue;
+ }
+ switch (seaTunnelDataType.getSqlType()) {
+ case STRING:
+ csvRecord.add((String) record.getField(fieldIndex));
+ break;
+ case BOOLEAN:
+ csvRecord.add((Boolean) record.getField(fieldIndex));
+ break;
+ case TINYINT:
+ csvRecord.add((Byte) record.getField(fieldIndex));
+ break;
+ case SMALLINT:
+ csvRecord.add((Short) record.getField(fieldIndex));
+ break;
+ case INT:
+ csvRecord.add((Integer) record.getField(fieldIndex));
+ break;
+ case BIGINT:
+ csvRecord.add((Long) record.getField(fieldIndex));
+ break;
+ case FLOAT:
+ csvRecord.add((Float) record.getField(fieldIndex));
+ break;
+ case DOUBLE:
+ csvRecord.add((Double) record.getField(fieldIndex));
+ break;
+ case DECIMAL:
+ csvRecord.add((BigDecimal) record.getField(fieldIndex));
+ break;
+ case DATE:
+ LocalDate localDate = (LocalDate)
record.getField(fieldIndex);
+ csvRecord.add((java.sql.Date)
java.sql.Date.valueOf(localDate));
+ break;
+ case TIME:
+ LocalTime localTime = (LocalTime)
record.getField(fieldIndex);
+ csvRecord.add((java.sql.Time)
java.sql.Time.valueOf(localTime));
+ break;
+ case TIMESTAMP:
+ LocalDateTime localDateTime = (LocalDateTime)
record.getField(fieldIndex);
+ csvRecord.add((java.sql.Timestamp)
java.sql.Timestamp.valueOf(localDateTime));
+ break;
+ case BYTES:
+ csvRecord.add(
+
org.apache.commons.codec.binary.Base64.encodeBase64String(
+ (byte[]) record.getField(fieldIndex)));
+ break;
+ case NULL:
+ csvRecord.add(null);
+ break;
+ case MAP:
+ case ARRAY:
+ case ROW:
+ default:
+ throw new JdbcConnectorException(
+ CommonErrorCodeDeprecated.UNSUPPORTED_DATA_TYPE,
+ "Unexpected value: " + seaTunnelDataType);
Review Comment:
Please add notice into doc we do not supported those type in copy statement
feature.
##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/JdbcOutputFormatBuilder.java:
##########
@@ -63,7 +64,13 @@ public JdbcOutputFormat build() {
jdbcSinkConfig.getDatabase() + "." +
jdbcSinkConfig.getTable()));
final List<String> primaryKeys = jdbcSinkConfig.getPrimaryKeys();
- if (StringUtils.isNotBlank(jdbcSinkConfig.getSimpleSql())) {
+ if (jdbcSinkConfig.isUseCopyStatement()) {
+ statementExecutorFactory =
Review Comment:
please add check to make sure only pg driver can use this feature. Otherwise
throw exception to user.
##########
seatunnel-connectors-v2/connector-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/executor/CopyManagerProxy.java:
##########
@@ -0,0 +1,48 @@
+/*
+ * 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.jdbc.internal.executor;
+
+import java.io.Reader;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+class CopyManagerProxy {
+ Object connection;
+ Object copyManager;
+ Class<?> connectionClazz;
+ Class<?> copyManagerClazz;
+ Method getCopyAPIMethod;
+ Method copyInMethod;
+
+ CopyManagerProxy(Connection connection)
+ throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException,
+ SQLException {
+ this.connection = connection.unwrap(Connection.class);
+ this.connectionClazz = this.connection.getClass();
+ this.getCopyAPIMethod = this.connectionClazz.getMethod("getCopyAPI");
+ this.copyManager = this.getCopyAPIMethod.invoke(this.connection);
+ this.copyManagerClazz = this.copyManager.getClass();
+ this.copyInMethod = this.copyManagerClazz.getMethod("copyIn",
String.class, Reader.class);
+ }
+
+ long doCopy(String sql, Reader reader)
+ throws InvocationTargetException, IllegalAccessException {
+ return (long) this.copyInMethod.invoke(this.copyManager, sql, reader);
+ }
Review Comment:
At now. This feature only support Postgres driver. That's meaning the
connection should be `PgConnection`. Why not just use
`((PgConnection)connection).getCopyAPI().copyIn(sql, reader)`?
--
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]