ruanwenjun commented on code in PR #1850:
URL: 
https://github.com/apache/incubator-seatunnel/pull/1850#discussion_r870393409


##########
seatunnel-core/seatunnel-core-flink-sql/src/main/java/org/apache/seatunnel/core/sql/classloader/CustomClassLoader.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.core.sql.classloader;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Path;
+
+public class CustomClassLoader extends URLClassLoader {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(CustomClassLoader.class);
+
+    public CustomClassLoader() {
+        super(new URL[0]);
+    }
+
+    public void addJar(Path jarPath) {
+        try {
+            this.addURL(jarPath.toUri().toURL());
+        } catch (MalformedURLException e) {
+            LOGGER.error("Failed to add jar to classloader. Jar: {}", jarPath, 
e);

Review Comment:
   I think it's better to throw the problem here, if the flink will throw the 
exception at the end. If you think the exception shouldn't be transported, you 
need to add a comment.



##########
docs/en/connector/flink-sql/Jdbc.md:
##########
@@ -0,0 +1,65 @@
+# Flink SQL JDBC Connector
+
+## Description
+
+We can use the Flink SQL JDBC Connector to connect to a JDBC database. Refer 
to the [Flink SQL JDBC 
Connector](https://nightlies.apache.org/flink/flink-docs-release-1.13/docs/connectors/table/jdbc/index.html)
 for more information.
+
+
+## Usage
+
+### 1. download driver
+A driver dependency is also required to connect to a specified database. Here 
are drivers currently supported:
+
+| Driver     | Group Id                 | Artifact Id          | JAR           
|
+|------------|-------------------|----------------------|---------------|
+| MySQL             | mysql             | mysql-connector-java | 
[Download](https://repo.maven.apache.org/maven2/mysql/mysql-connector-java/) |
+| PostgreSQL | org.postgresql   | postgresql           | 
[Download](https://jdbc.postgresql.org/download.html) |
+| Derby             | org.apache.derby  | derby                    | 
[Download](http://db.apache.org/derby/derby_downloads.html) |
+
+After downloading the driver jars, you need to place the jars into 
$FLINK_HOME/lib/.
+
+### 2. prepare data
+Start mysql server locally, and create a database named "test" and a table 
named "test_table" in the database.
+
+The table "test_table" could be created by the following SQL:
+```bash

Review Comment:
   ```suggestion
   ```sql
   ```



##########
seatunnel-core/seatunnel-core-flink-sql/src/main/java/org/apache/seatunnel/core/sql/job/Executor.java:
##########
@@ -75,12 +116,65 @@ private static StatementSet handleStatements(String 
workFlowContent, StreamTable
             if (op instanceof CatalogSinkModifyOperation) {
                 statementSet.addInsertSql(stmt);
             } else {
+                if (op instanceof CreateTableOperation) {
+                    String connectorType = ((CreateTableOperation) 
op).getCatalogTable().getOptions().get(CONNECTOR_IDENTIFIER);
+                    loadConnector(connectorType, executionEnvConfiguration);
+                }
+
                 tEnv.executeSql(stmt);
             }
         }
         return statementSet;
     }
 
+    private static void loadConnector(String connectorType, Configuration 
configuration) {
+        Iterator<Factory> factories = ServiceLoader.load(Factory.class, 
CLASSLOADER).iterator();
+        while (factories.hasNext()) {
+            Factory factory = factories.next();
+
+            /**
+             * Handle for two cases:
+             * 1. Flink built-in connectors.
+             * 2. Connectors have been placed in classpath.
+             */
+            if (factory.factoryIdentifier().equals(connectorType)) {
+                return;
+            }
+        }
+
+        Common.setDeployMode(DeployMode.CLIENT.getName());
+        File connectorDir = 
Common.connectorJarDir(SQL_CONNECTOR_PREFIX).toFile();
+        if (!connectorDir.exists() || connectorDir.listFiles() == null) {
+            return;
+        }
+
+        Optional<File> connectorFile = Arrays.stream(connectorDir.listFiles())
+            .filter(file -> file.getName().startsWith(CONNECTOR_JAR_PREFIX + 
connectorType))
+            .findFirst();
+
+        if (connectorFile.isPresent()) {
+            // handleStatements need this.
+            CLASSLOADER.addJar(connectorFile.get().toPath());
+
+            List<String> jars = configuration.get(PipelineOptions.JARS);
+            jars = jars == null ? new ArrayList<>() : jars;
+
+            List<String> classpath = 
configuration.get(PipelineOptions.CLASSPATHS);
+            classpath = classpath == null ? new ArrayList<>() : classpath;
+
+            try {
+                String connectorURL = 
connectorFile.get().toPath().toUri().toURL().toString();
+                jars.add(connectorURL);
+                classpath.add(connectorURL);
+
+                configuration.set(PipelineOptions.JARS, jars);
+                configuration.set(PipelineOptions.CLASSPATHS, classpath);
+            } catch (MalformedURLException ignored) {
+                LOGGER.error("Failed to load connector {}. Connector file: 
{}", connectorType, connectorFile.get().getAbsolutePath());
+            }

Review Comment:
   OK you need to add a comment to describe in the code.



##########
seatunnel-connectors/seatunnel-connectors-flink-sql-dist/pom.xml:
##########
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0";
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
+  <parent>
+    <artifactId>seatunnel-connectors</artifactId>
+    <groupId>org.apache.seatunnel</groupId>
+    <version>${revision}</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>seatunnel-connectors-flink-sql-dist</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.seatunnel</groupId>
+<!--      <artifactId>seatunnel-connector-flink-console</artifactId>-->

Review Comment:
   ```suggestion
   ```



##########
docs/en/connector/flink-sql/Jdbc.md:
##########
@@ -0,0 +1,65 @@
+# Flink SQL JDBC Connector
+
+## Description
+
+We can use the Flink SQL JDBC Connector to connect to a JDBC database. Refer 
to the [Flink SQL JDBC 
Connector](https://nightlies.apache.org/flink/flink-docs-release-1.13/docs/connectors/table/jdbc/index.html)
 for more information.
+
+
+## Usage
+
+### 1. download driver
+A driver dependency is also required to connect to a specified database. Here 
are drivers currently supported:
+
+| Driver     | Group Id                 | Artifact Id          | JAR           
|
+|------------|-------------------|----------------------|---------------|
+| MySQL             | mysql             | mysql-connector-java | 
[Download](https://repo.maven.apache.org/maven2/mysql/mysql-connector-java/) |
+| PostgreSQL | org.postgresql   | postgresql           | 
[Download](https://jdbc.postgresql.org/download.html) |
+| Derby             | org.apache.derby  | derby                    | 
[Download](http://db.apache.org/derby/derby_downloads.html) |
+
+After downloading the driver jars, you need to place the jars into 
$FLINK_HOME/lib/.
+
+### 2. prepare data
+Start mysql server locally, and create a database named "test" and a table 
named "test_table" in the database.
+
+The table "test_table" could be created by the following SQL:
+```bash
+CREATE TABLE IF NOT EXISTS `test_table`(
+   `id` INT UNSIGNED AUTO_INCREMENT,
+   `name` VARCHAR(100) NOT NULL,
+   PRIMARY KEY ( `id` )
+)ENGINE=InnoDB DEFAULT CHARSET=utf8;
+```
+
+Insert some data into the table "test_table".
+
+### 3. seatunnel config 
+Prepare a seatunnel config file with the following content:
+```text

Review Comment:
   ```suggestion
   ```sql
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to