-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jdbc support copy in statement.
- Loading branch information
Showing
11 changed files
with
329 additions
and
3 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
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
182 changes: 182 additions & 0 deletions
182
...tunnel/connectors/seatunnel/jdbc/internal/executor/CopyManagerBatchStatementExecutor.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,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); | ||
} | ||
} | ||
return csvRecord; | ||
} | ||
|
||
@Override | ||
public void executeBatch() throws SQLException { | ||
try { | ||
this.csvPrinter.flush(); | ||
this.copyManagerProxy.doCopy( | ||
copySql, new StringReader(this.csvPrinter.getOut().toString())); | ||
|
||
} catch (InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
try { | ||
this.csvPrinter.close(); | ||
this.csvPrinter = new CSVPrinter(new StringBuilder(), csvFormat); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void closeStatements() throws SQLException { | ||
this.copyManagerProxy = null; | ||
try { | ||
this.csvPrinter.close(); | ||
this.csvPrinter = null; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...va/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/executor/CopyManagerProxy.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,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); | ||
} | ||
} |
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
Oops, something went wrong.