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


##########
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:
   if user create a table with connector 'xx', but not use this table in 
'INSERT INTO' op, that is ok.
   
   Let me give an example.
   ```bash
   SET table.dml-sync = true;
   
   CREATE TABLE test (
     id BIGINT,
     name STRING
   ) WITH (
   'connector'='jdbc',
     'url' = 'jdbc:mysql://localhost:3306/gcs',
     'table-name' = 'test1',
     'username' = 'root',
     'password' = '123456'
   );
   
   CREATE TABLE unused_table (
     id INT,
     name STRING
   ) WITH (
     'connector' = 'unkown',
     'sink.parallelism' = '1'
   );
   
   CREATE TABLE print_table (
     id BIGINT,
     name STRING
   ) WITH (
     'connector' = 'print',
     'sink.parallelism' = '1'
   );
   
   INSERT INTO print_table SELECT * FROM test;
   ```
   
   Here we create the table `unused_table` with a not-exist connector 
`unknown`, we won't load the corresponding jar (because it does not exist). 
Because we don't use it in `INSERT INTO print_table SELECT * FROM test`, 
actually it works.
   
   I will add a comment, and refer this PR link in comment.
   
   cc @ruanwenjun 



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