You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/06/12 14:44:45 UTC

[GitHub] [incubator-seatunnel] ic4y opened a new pull request, #2009: Add SeaTunnel jdbc sink (#1946)

ic4y opened a new pull request, #2009:
URL: https://github.com/apache/incubator-seatunnel/pull/2009

   <!--
   
   Thank you for contributing to SeaTunnel! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [GITHUB issue](https://github.com/apache/incubator-seatunnel/issues).
   
     - Name the pull request in the form "[Feature] [component] Title of the pull request", where *Feature* can be replaced by `Hotfix`, `Bug`, etc.
   
     - Minor fixes should be named following this pattern: `[hotfix] [docs] Fix typo in README.md doc`.
   
   -->
   
   ## Purpose of this pull request
   
   <!-- Describe the purpose of this pull request. For example: This pull request adds checkstyle plugin.-->
   
   ## Check list
   
   * [ ] Code changed are covered with tests, or it does not need tests for reason:
   * [ ] If any new Jar binary package adding in your PR, please add License Notice according
     [New License Guide](https://github.com/apache/incubator-seatunnel/blob/dev/docs/en/contribution/new-license.md)
   * [ ] If necessary, please update the documentation to describe the new feature. https://github.com/apache/incubator-seatunnel/tree/dev/docs
   
   related to #1946
   
   I have tested `mysql` and `postgres` on flink. Pass the test below
   
   * [x] batch model open exactly_once
   * [x] batch model close exactly_once
   * [x] streaming model open exactly_once
   * [x] streaming model close exactly_once
   
   example mysql config:
   ```
   sink {
       jdbc {
   
           url = "jdbc:mysql://localhost/test"
           driver = "com.mysql.cj.jdbc.Driver"
           connection_check_timeout_sec = 10
           max_retries = 0
           user = "root"
           password = "123456"
           query = "insert into test_table(name,age) values(?,?)"
           parallelism = "2"
   
           batch_size = 10
           batch_interval_ms = 10000
   
           is_exactly_once = "true"
   
           xa_data_source_class_name = "com.mysql.cj.jdbc.MysqlXADataSource"
           max_commit_attempts = 10
           transaction_timeout_sec = 1000
            }
   } 
   ```
   
   
   example postgres config:
   ```
   sink {
       jdbc {
           url = "jdbc:postgresql://localhost:5432/test"
           driver = "org.postgresql.Driver"
           connection_check_timeout_sec = 10
           max_retries = 0
           user = "postgres"
           password = "123456"
           query = "insert into test_table(name,age) values(?,?)"
           parallelism = "2"
   
           batch_size = 10
           batch_interval_ms = 10000
   
           #Postgres needs to set this parameter : ALTER SYSTEM set max_prepared_transactions to 10;
           is_exactly_once = "true"
   
           xa_data_source_class_name = "org.postgresql.xa.PGXADataSource"
           max_commit_attempts = 10
           transaction_timeout_sec = 1000
       }
   }```


-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] CalvinKirs commented on a diff in pull request #2009: [api-draft][connector] Add SeaTunnel jdbc sink (#1946)

Posted by GitBox <gi...@apache.org>.
CalvinKirs commented on code in PR #2009:
URL: https://github.com/apache/incubator-seatunnel/pull/2009#discussion_r895376646


##########
seatunnel-connectors/seatunnel-connectors-seatunnel/seatunnel-connector-seatunnel-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/connection/SimpleJdbcConnectionProvider.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.connection;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.options.JdbcConnectorOptions;
+
+import lombok.NonNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Enumeration;
+import java.util.Properties;
+
+/**
+ * Simple JDBC connection provider.
+ */
+public class SimpleJdbcConnectionProvider
+    implements JdbcConnectionProvider, Serializable {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SimpleJdbcConnectionProvider.class);
+
+    private static final long serialVersionUID = 1L;
+
+    private final JdbcConnectorOptions jdbcOptions;
+
+    private transient Driver loadedDriver;
+    private transient Connection connection;
+
+    static {
+        // Load DriverManager first to avoid deadlock between DriverManager's
+        // static initialization block and specific driver class's static
+        // initialization block when two different driver classes are loading
+        // concurrently using Class.forName while DriverManager is uninitialized
+        // before.
+        //
+        // This could happen in JDK 8 but not above as driver loading has been
+        // moved out of DriverManager's static initialization block since JDK 9.
+        DriverManager.getDrivers();
+    }
+
+    public SimpleJdbcConnectionProvider(@NonNull JdbcConnectorOptions jdbcOptions) {
+        this.jdbcOptions = jdbcOptions;
+    }
+
+    @Override
+    public Connection getConnection() {
+        return connection;
+    }
+
+    @Override
+    public boolean isConnectionValid()
+        throws SQLException {
+        return connection != null
+            && connection.isValid(jdbcOptions.getConnectionCheckTimeoutSeconds());
+    }
+
+    private static Driver loadDriver(String driverName)
+        throws SQLException, ClassNotFoundException {
+        checkNotNull(driverName);
+        Enumeration<Driver> drivers = DriverManager.getDrivers();
+        while (drivers.hasMoreElements()) {
+            Driver driver = drivers.nextElement();
+            if (driver.getClass().getName().equals(driverName)) {
+                return driver;
+            }
+        }
+
+        // We could reach here for reasons:
+        // * Class loader hell of DriverManager(see JDK-8146872).
+        // * driver is not installed as a service provider.
+        Class<?> clazz =
+            Class.forName(driverName, true, Thread.currentThread().getContextClassLoader());
+        try {
+            return (Driver) clazz.newInstance();

Review Comment:
   Don't use Deprecated method



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] CalvinKirs merged pull request #2009: [api-draft][connector] Add SeaTunnel jdbc sink (#1946)

Posted by GitBox <gi...@apache.org>.
CalvinKirs merged PR #2009:
URL: https://github.com/apache/incubator-seatunnel/pull/2009


-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] CalvinKirs commented on a diff in pull request #2009: [api-draft][connector] Add SeaTunnel jdbc sink (#1946)

Posted by GitBox <gi...@apache.org>.
CalvinKirs commented on code in PR #2009:
URL: https://github.com/apache/incubator-seatunnel/pull/2009#discussion_r895405160


##########
seatunnel-connectors/seatunnel-connectors-seatunnel/seatunnel-connector-seatunnel-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/state/XidInfo.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.state;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import javax.transaction.xa.Xid;
+
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+public class XidInfo implements Serializable {
+
+    final Xid xid;

Review Comment:
   Sorry, I didn't notice, XidImpl implements serialization



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] CalvinKirs commented on a diff in pull request #2009: [api-draft][connector] Add SeaTunnel jdbc sink (#1946)

Posted by GitBox <gi...@apache.org>.
CalvinKirs commented on code in PR #2009:
URL: https://github.com/apache/incubator-seatunnel/pull/2009#discussion_r895379459


##########
seatunnel-connectors/seatunnel-connectors-seatunnel/seatunnel-connector-seatunnel-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/state/XidInfo.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.state;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import javax.transaction.xa.Xid;
+
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+public class XidInfo implements Serializable {
+
+    final Xid xid;

Review Comment:
   It's better add `transient`



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] ic4y commented on a diff in pull request #2009: [api-draft][connector] Add SeaTunnel jdbc sink (#1946)

Posted by GitBox <gi...@apache.org>.
ic4y commented on code in PR #2009:
URL: https://github.com/apache/incubator-seatunnel/pull/2009#discussion_r895391538


##########
seatunnel-connectors/seatunnel-connectors-seatunnel/seatunnel-connector-seatunnel-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/state/XidInfo.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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.state;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+import javax.transaction.xa.Xid;
+
+import java.io.Serializable;
+
+@Data
+@AllArgsConstructor
+public class XidInfo implements Serializable {
+
+    final Xid xid;

Review Comment:
   I don't think so. Because the Xid is part of the transfer that needs to be serialized.



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-seatunnel] ic4y commented on a diff in pull request #2009: [api-draft][connector] Add SeaTunnel jdbc sink (#1946)

Posted by GitBox <gi...@apache.org>.
ic4y commented on code in PR #2009:
URL: https://github.com/apache/incubator-seatunnel/pull/2009#discussion_r895406455


##########
seatunnel-connectors/seatunnel-connectors-seatunnel/seatunnel-connector-seatunnel-jdbc/src/main/java/org/apache/seatunnel/connectors/seatunnel/jdbc/internal/connection/SimpleJdbcConnectionProvider.java:
##########
@@ -0,0 +1,154 @@
+/*
+ * 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.connection;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.options.JdbcConnectorOptions;
+
+import lombok.NonNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Serializable;
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Enumeration;
+import java.util.Properties;
+
+/**
+ * Simple JDBC connection provider.
+ */
+public class SimpleJdbcConnectionProvider
+    implements JdbcConnectionProvider, Serializable {
+
+    private static final Logger LOG = LoggerFactory.getLogger(SimpleJdbcConnectionProvider.class);
+
+    private static final long serialVersionUID = 1L;
+
+    private final JdbcConnectorOptions jdbcOptions;
+
+    private transient Driver loadedDriver;
+    private transient Connection connection;
+
+    static {
+        // Load DriverManager first to avoid deadlock between DriverManager's
+        // static initialization block and specific driver class's static
+        // initialization block when two different driver classes are loading
+        // concurrently using Class.forName while DriverManager is uninitialized
+        // before.
+        //
+        // This could happen in JDK 8 but not above as driver loading has been
+        // moved out of DriverManager's static initialization block since JDK 9.
+        DriverManager.getDrivers();
+    }
+
+    public SimpleJdbcConnectionProvider(@NonNull JdbcConnectorOptions jdbcOptions) {
+        this.jdbcOptions = jdbcOptions;
+    }
+
+    @Override
+    public Connection getConnection() {
+        return connection;
+    }
+
+    @Override
+    public boolean isConnectionValid()
+        throws SQLException {
+        return connection != null
+            && connection.isValid(jdbcOptions.getConnectionCheckTimeoutSeconds());
+    }
+
+    private static Driver loadDriver(String driverName)
+        throws SQLException, ClassNotFoundException {
+        checkNotNull(driverName);
+        Enumeration<Driver> drivers = DriverManager.getDrivers();
+        while (drivers.hasMoreElements()) {
+            Driver driver = drivers.nextElement();
+            if (driver.getClass().getName().equals(driverName)) {
+                return driver;
+            }
+        }
+
+        // We could reach here for reasons:
+        // * Class loader hell of DriverManager(see JDK-8146872).
+        // * driver is not installed as a service provider.
+        Class<?> clazz =
+            Class.forName(driverName, true, Thread.currentThread().getContextClassLoader());
+        try {
+            return (Driver) clazz.newInstance();

Review Comment:
   thanks i fixed it



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org