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


##########
seatunnel-connectors/seatunnel-connectors-flink-sql/flink-sql-connector-jdbc/pom.xml:
##########
@@ -0,0 +1,35 @@
+<?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-flink-sql</artifactId>
+    <groupId>org.apache.seatunnel</groupId>
+    <version>${revision}</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>flink-sql-connector-jdbc</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.flink</groupId>
+      <artifactId>flink-connector-jdbc_2.11</artifactId>

Review Comment:
   ```suggestion
         <artifactId>flink-connector-jdbc_${scala.binary.version}</artifactId>
   ```



##########
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 {

Review Comment:
   It's better to add a `todo` here, we need to add a unified plugin load 
module to manage the plugin, we have this kind of code in several places.



##########
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:
   It better to throw exception here, the upstream need to judge if the program 
will stop.



##########
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:
   Why ignore this exception, please add comment to explain this.



##########
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());

Review Comment:
   If there are multiple jars with the same name, how do you handle this? Since 
you use `startWith` to filter the connector, so this case will happen, it's 
better to add a warning here.



##########
seatunnel-connectors/seatunnel-connectors-flink-sql/pom.xml:
##########
@@ -0,0 +1,34 @@
+<?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</artifactId>
+  <packaging>pom</packaging>
+
+  <modules>
+    <module>flink-sql-connector-jdbc</module>
+  </modules>
+
+

Review Comment:
   ```suggestion
   ```



##########
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
   ```
   Please remove this.



-- 
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