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_r298524951
########## File path: flink-connectors/flink-jdbc/src/main/java/org/apache/flink/api/java/io/jdbc/AbstractJDBCOutputFormat.java ########## @@ -0,0 +1,212 @@ +/* + * 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.common.io.RichOutputFormat; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.types.Row; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * OutputFormat to write Rows into a JDBC database. + * + * @see Row + * @see DriverManager + */ +public abstract class AbstractJDBCOutputFormat<T> extends RichOutputFormat<T> { + + private static final long serialVersionUID = 1L; + static final int DEFAULT_BATCH_INTERVAL = 5000; + + private static final Logger LOG = LoggerFactory.getLogger(AbstractJDBCOutputFormat.class); + + private final String username; + private final String password; + private final String drivername; + private final String dbURL; + + protected Connection dbConn; + + public AbstractJDBCOutputFormat(String username, String password, String drivername, String dbURL) { + this.username = username; + this.password = password; + this.drivername = drivername; + this.dbURL = dbURL; + } + + @Override + public void configure(Configuration parameters) { + } + + protected void establishConnection() throws SQLException, ClassNotFoundException { + Class.forName(drivername); + if (username == null) { + dbConn = DriverManager.getConnection(dbURL); + } else { + dbConn = DriverManager.getConnection(dbURL, username, password); + } + } + + protected void closeDbConnection() throws IOException { + if (dbConn != null) { + try { + dbConn.close(); + } catch (SQLException se) { + LOG.info("JDBC connection could not be closed: " + se.getMessage()); + } finally { + dbConn = null; + } + } + } + + /** + * Adds a record to the prepared statement. It will invoke addBatch. + */ + public static void writeRowToBatch(PreparedStatement upload, int[] typesArray, Row row) throws IOException, SQLException { + writeRecordToStatement(upload, typesArray, row); + upload.addBatch(); + } + + /** + * Adds a record to the prepared statement. + * + * <p>When this method is called, the output format is guaranteed to be opened. + * + * <p>WARNING: this may fail when no column types specified (because a best effort approach is attempted in order to + * insert a null value but it's not guaranteed that the JDBC driver handles PreparedStatement.setObject(pos, null)) + * + * @param upload The prepared statement. + * @param typesArray The jdbc types of the row. + * @param row The records to add to the output. + * @see PreparedStatement + * @throws IOException Thrown, if the records could not be added due to an I/O problem. + */ + public static void writeRecordToStatement(PreparedStatement upload, int[] typesArray, Row row) throws IOException, SQLException { + if (typesArray != null && typesArray.length > 0 && typesArray.length != row.getArity()) { + LOG.warn("Column SQL types array doesn't match arity of passed Row! Check the passed array..."); + } + if (typesArray == null) { + // no types provided + for (int index = 0; index < row.getArity(); index++) { + LOG.warn("Unknown column type for column {}. Best effort approach to set its value: {}.", index + 1, row.getField(index)); + upload.setObject(index + 1, row.getField(index)); + } + } else { + // types provided + for (int i = 0; i < row.getArity(); i++) { + setField(upload, typesArray[i], row, i); + } + } + } + + public static void setField(PreparedStatement upload, int type, Row row, int index) throws SQLException { Review comment: can be `protected` ---------------------------------------------------------------- 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
