wuchong commented on a change in pull request #8867: [FLINK-12956][jdbc] 
Introduce upsert table sink for JDBC
URL: https://github.com/apache/flink/pull/8867#discussion_r298569122
 
 

 ##########
 File path: 
flink-connectors/flink-jdbc/src/main/java/org/apache/flink/api/java/io/jdbc/JDBCUpsertOutputFormat.java
 ##########
 @@ -0,0 +1,361 @@
+/*
+ * 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.api.java.io.jdbc;
+
+import org.apache.flink.api.java.io.jdbc.upsert.QueryExecutor;
+import org.apache.flink.api.java.io.jdbc.upsert.QueryExecutorFactories;
+import org.apache.flink.api.java.tuple.Tuple2;
+import org.apache.flink.types.Row;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * An upsert OutputFormat for JDBC.
+ */
+public class JDBCUpsertOutputFormat extends 
AbstractJDBCOutputFormat<Tuple2<Boolean, Row>> {
+
+       private static final long serialVersionUID = 1L;
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(JDBCUpsertOutputFormat.class);
+
+       static final int DEFAULT_MAX_RETRY_TIMES = 3;
+
+       private final String tableName;
+       private QueryExecutor appendExec;
+       private QueryExecutor replaceExec;
+       private QueryExecutor deleteExec;
+       private final int batchInterval;
+       private final int maxRetryTime;
+       private final String[] fieldNames;
+       private final int[] typesArray;
+       private final int[] pkFields;
+       private final boolean noPks;
+
+       private transient List<Row> appendRows;
+       private transient Map<Row, Tuple2<Boolean, Row>> keyToRows;
+       private transient int batchCount = 0;
+       private transient boolean objectReuse;
+
+       private JDBCUpsertOutputFormat(
+                       String tableName, String username, String password, 
String drivername, String dbURL,
+                       QueryExecutor appendExec, QueryExecutor replaceExec, 
QueryExecutor deleteExec,
+                       int batchInterval, int maxRetryTime, String[] 
fieldNames, int[] typesArray, int[] pkFields) {
+               super(username, password, drivername, dbURL);
+               this.tableName = tableName;
+               this.appendExec = appendExec;
+               this.replaceExec = replaceExec;
+               this.deleteExec = deleteExec;
+               this.batchInterval = batchInterval;
+               this.maxRetryTime = maxRetryTime;
+               this.fieldNames = fieldNames;
+               this.typesArray = typesArray;
+               this.pkFields = pkFields;
+               this.noPks = pkFields == null || pkFields.length == 0;
+       }
+
+       /**
+        * Connects to the target database and initializes the prepared 
statement.
+        *
+        * @param taskNumber The number of the parallel instance.
+        * @throws IOException Thrown, if the output could not be opened due to 
an
+        * I/O problem.
+        */
+       @Override
+       public void open(int taskNumber, int numTasks) throws IOException {
+               this.objectReuse = 
getRuntimeContext().getExecutionConfig().isObjectReuseEnabled();
+               try {
+                       establishConnection();
+                       if (noPks) {
+                               this.appendRows = new ArrayList<>();
+                               this.appendExec.open(dbConn, tableName, 
fieldNames, typesArray, pkFields);
+                       } else {
+                               this.keyToRows = new HashMap<>();
+                               this.replaceExec.open(dbConn, tableName, 
fieldNames, typesArray, pkFields);
+                               this.deleteExec.open(dbConn, tableName, 
fieldNames, typesArray, pkFields);
+                       }
+               } catch (SQLException sqe) {
+                       throw new IllegalArgumentException("open() failed.", 
sqe);
+               } catch (ClassNotFoundException cnfe) {
+                       throw new IllegalArgumentException("JDBC driver class 
not found.", cnfe);
+               }
+       }
+
+       @Override
+       public void writeRecord(Tuple2<Boolean, Row> tuple2) throws IOException 
{
+               // we don't need perform a deep copy, because jdbc field are 
immutable object.
+               if (noPks) {
+                       Row row = objectReuse ? Row.copy(tuple2.f1) : tuple2.f1;
+                       appendRows.add(row);
+               } else {
+                       tuple2 = objectReuse ? new Tuple2<>(tuple2.f0, 
Row.copy(tuple2.f1)) : tuple2;
+                       keyToRows.put(getPrimaryKeys(tuple2.f1), tuple2);
+               }
+               if (batchCount++ >= batchInterval) {
 
 Review comment:
   Interval is usually used as time interval. I think `maxBatchSize` might be 
more suitable for this.
   
   And we should also provide a `flushInterval` to make sure the `flush()` will 
be invoked even if we can't reach batchSize.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to