JingsongLi commented on a change in pull request #11381: 
[FLINK-16294][connectors /jdbc]Support to create non-existed table in database 
automatically when writing data to JDBC connector.
URL: https://github.com/apache/flink/pull/11381#discussion_r398288100
 
 

 ##########
 File path: 
flink-connectors/flink-jdbc/src/main/java/org/apache/flink/api/java/io/jdbc/CreateJdbcTableUtil.java
 ##########
 @@ -0,0 +1,86 @@
+/*
+ * 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.dialect.JDBCDialect;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * Util to create jdbc table.
+ */
+public class CreateJdbcTableUtil {
+
+       private static final Logger LOG = 
LoggerFactory.getLogger(CreateJdbcTableUtil.class);
+       private static final int DEFAULT_MAX_RETRY_TIMES = 3;
+
+       /**
+        * Create jdbc table if the table not exists.
+        */
+       public static void createTableIfNotExists(JDBCOptions options, String[] 
fieldNames, int[] fieldTypes,
+                       String[] keyFields) throws RuntimeException {
+               JDBCDialect dialect = options.getDialect();
+               String createTableSql = 
dialect.getCreateTableStatement(options.getTableName(), fieldNames, fieldTypes, 
keyFields);
+
+               Connection conn = null;
+               Statement st;
+               int retryTime = 0;
+               while (retryTime++ < DEFAULT_MAX_RETRY_TIMES) {
+                       try {
+                               conn = new 
SimpleJdbcConnectionProvider(options).getConnection();
+                               st = conn.createStatement();
+                               st.execute(createTableSql);
+                               break;
+                       } catch (ClassNotFoundException e) {
+                               throw new RuntimeException(
+                                       String.format("Jdbc Driver class %s can 
not found.", options.getDriverName()), e);
+                       } catch (SQLException e) {
+                               if ("X0Y32".equals(e.getSQLState())) {
 
 Review comment:
   Can we remove this judge?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to