You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by ki...@apache.org on 2022/02/23 13:42:55 UTC

[incubator-seatunnel] branch dev updated: [Feature][sql] Support variable in sql job conf (#1314)

This is an automated email from the ASF dual-hosted git repository.

kirs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new 851a066  [Feature][sql] Support variable in sql job conf  (#1314)
851a066 is described below

commit 851a066d83fd7676fe126b5fbc3c8948a41a1a23
Author: Simon <zh...@cvte.com>
AuthorDate: Wed Feb 23 21:42:48 2022 +0800

    [Feature][sql] Support variable in sql job conf  (#1314)
    
    * sqlVariable
---
 .../seatunnel/config/command/CommandFlinkArgs.java |  3 ++
 .../seatunnel/config/command/CommandLineArgs.java  |  9 +++-
 .../seatunnel/config/command/CommandLineUtils.java |  3 +-
 .../apache/seatunnel/core/sql/SeatunnelSql.java    |  4 +-
 .../org/apache/seatunnel/core/sql/job/JobInfo.java | 13 +++++-
 .../core/sql/SqlVariableSubstitutionTest.java}     | 22 ++++++----
 .../src/test/resources/flink.sql.conf.template     | 49 ++++++++++++++++++++++
 .../seatunnel/example/flink/LocalFlinkExample.java |  2 +-
 .../seatunnel/example/spark/LocalSparkExample.java |  2 +-
 9 files changed, 92 insertions(+), 15 deletions(-)

diff --git a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandFlinkArgs.java b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandFlinkArgs.java
index 4d5a52b..58f17b8 100644
--- a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandFlinkArgs.java
+++ b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandFlinkArgs.java
@@ -42,4 +42,7 @@ public class CommandFlinkArgs {
         return testConfig;
     }
 
+    public String getVariable() {
+        return variable;
+    }
 }
diff --git a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java
index 2990fdf..009dc0f 100644
--- a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java
+++ b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineArgs.java
@@ -22,10 +22,12 @@ public class CommandLineArgs {
     private String deployMode = "client";
     private final String configFile;
     private final boolean testConfig;
+    private String variable;
 
-    public CommandLineArgs(String configFile, boolean testConfig) {
+    public CommandLineArgs(String configFile, boolean testConfig, String variable) {
         this.configFile = configFile;
         this.testConfig = testConfig;
+        this.variable = variable;
     }
 
     public CommandLineArgs(String deployMode, String configFile, boolean testConfig) {
@@ -45,4 +47,9 @@ public class CommandLineArgs {
     public boolean isTestConfig() {
         return testConfig;
     }
+
+    public String getVariable() {
+        return variable;
+    }
+
 }
diff --git a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineUtils.java b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineUtils.java
index 0dab928..41d7751 100644
--- a/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineUtils.java
+++ b/seatunnel-core/seatunnel-core-base/src/main/java/org/apache/seatunnel/config/command/CommandLineUtils.java
@@ -47,7 +47,8 @@ public final class CommandLineUtils {
 
         return new CommandLineArgs(
             commandFlinkArgs.getConfigFile(),
-            commandFlinkArgs.isTestConfig()
+            commandFlinkArgs.isTestConfig(),
+            commandFlinkArgs.getVariable()
         );
     }
 
diff --git a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/SeatunnelSql.java b/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/SeatunnelSql.java
index 0e4e5a2..7b09e5a 100644
--- a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/SeatunnelSql.java
+++ b/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/SeatunnelSql.java
@@ -39,7 +39,9 @@ public class SeatunnelSql {
         CommandLineArgs flinkArgs = CommandLineUtils.parseFlinkArgs(args);
         String configFilePath = flinkArgs.getConfigFile();
         String jobContent = FileUtils.readFileToString(new File(configFilePath), StandardCharsets.UTF_8);
-        return new JobInfo(jobContent);
+        JobInfo jobInfo = new JobInfo(jobContent);
+        jobInfo.substitute(flinkArgs.getVariable());
+        return jobInfo;
     }
 
 }
diff --git a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/job/JobInfo.java b/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/job/JobInfo.java
index 849e38f..0ed5a6f 100644
--- a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/job/JobInfo.java
+++ b/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/job/JobInfo.java
@@ -20,9 +20,11 @@ package org.apache.seatunnel.core.sql.job;
 public class JobInfo {
 
     private static final String JOB_NAME = "default sql job";
+    private static final String VAR_REGEX = "\\$\\{%s}";
+    private static final String DELIMITER = "=";
 
     private final String jobName;
-    private final String jobContent;
+    private String jobContent;
 
     public JobInfo(String jobContent) {
         this.jobName = JOB_NAME;
@@ -42,4 +44,13 @@ public class JobInfo {
         return jobContent;
     }
 
+    public void substitute(String variable) {
+        if (variable != null) {
+            String[] s = variable.split(DELIMITER);
+            if (s.length == 2) {
+                jobContent = jobContent.replaceAll(String.format(VAR_REGEX, s[0].trim()), s[1].trim());
+            }
+        }
+    }
+
 }
diff --git a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/SeatunnelSql.java b/seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/SqlVariableSubstitutionTest.java
similarity index 67%
copy from seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/SeatunnelSql.java
copy to seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/SqlVariableSubstitutionTest.java
index 0e4e5a2..b639a73 100644
--- a/seatunnel-core/seatunnel-core-sql/src/main/java/org/apache/seatunnel/core/sql/SeatunnelSql.java
+++ b/seatunnel-core/seatunnel-core-sql/src/test/java/org/apache/seatunnel/core/sql/SqlVariableSubstitutionTest.java
@@ -19,27 +19,31 @@ package org.apache.seatunnel.core.sql;
 
 import org.apache.seatunnel.config.command.CommandLineArgs;
 import org.apache.seatunnel.config.command.CommandLineUtils;
-import org.apache.seatunnel.core.sql.job.Executor;
 import org.apache.seatunnel.core.sql.job.JobInfo;
 
 import org.apache.commons.io.FileUtils;
+import org.junit.Assert;
+import org.junit.Test;
 
 import java.io.File;
-import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 
-public class SeatunnelSql {
+public class SqlVariableSubstitutionTest {
+    public static final String TEST_RESOURCE_DIR = "/src/test/resources/";
 
-    public static void main(String[] args) throws Exception {
-        JobInfo jobInfo = parseJob(args);
-        Executor.runJob(jobInfo);
-    }
+    @Test
+    public void normalizeStatementsWithMultiSqls() throws Exception {
+        String[] args = {"-c", System.getProperty("user.dir") + TEST_RESOURCE_DIR + "flink.sql.conf.template",
+            "-t", "-i", "table_name=events"};
 
-    private static JobInfo parseJob(String[] args) throws IOException {
         CommandLineArgs flinkArgs = CommandLineUtils.parseFlinkArgs(args);
         String configFilePath = flinkArgs.getConfigFile();
         String jobContent = FileUtils.readFileToString(new File(configFilePath), StandardCharsets.UTF_8);
-        return new JobInfo(jobContent);
+        JobInfo jobInfo = new JobInfo(jobContent);
+        Assert.assertFalse(jobInfo.getJobContent().contains("events"));
+
+        jobInfo.substitute(flinkArgs.getVariable());
+        Assert.assertTrue(jobInfo.getJobContent().contains("events"));
     }
 
 }
diff --git a/seatunnel-core/seatunnel-core-sql/src/test/resources/flink.sql.conf.template b/seatunnel-core/seatunnel-core-sql/src/test/resources/flink.sql.conf.template
new file mode 100644
index 0000000..2d7f9d4
--- /dev/null
+++ b/seatunnel-core/seatunnel-core-sql/src/test/resources/flink.sql.conf.template
@@ -0,0 +1,49 @@
+--
+-- 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.
+--
+
+--
+-- This config file is a demonstration of sql processing in SeaTunnel config
+--
+--
+
+
+SET 'table.dml-sync' = 'true';
+
+CREATE TABLE ${table_name} (
+  f_type INT,
+  f_uid INT,
+  ts AS localtimestamp,
+  WATERMARK FOR ts AS ts
+) WITH (
+  'connector' = 'datagen',
+  'rows-per-second'='5',
+  'fields.f_type.min'='1',
+  'fields.f_type.max'='5',
+  'fields.f_uid.min'='1',
+  'fields.f_uid.max'='1000'
+);
+
+CREATE TABLE print_table (
+  type INT,
+  uid INT,
+  lstmt TIMESTAMP
+) WITH (
+  'connector' = 'print',
+  'sink.parallelism' = '1'
+);
+
+INSERT INTO print_table SELECT * FROM ${table_name} where f_type = 1;
diff --git a/seatunnel-examples/seatunnel-flink-examples/src/main/java/org/apache/seatunnel/example/flink/LocalFlinkExample.java b/seatunnel-examples/seatunnel-flink-examples/src/main/java/org/apache/seatunnel/example/flink/LocalFlinkExample.java
index f0ac14a..25316e4 100644
--- a/seatunnel-examples/seatunnel-flink-examples/src/main/java/org/apache/seatunnel/example/flink/LocalFlinkExample.java
+++ b/seatunnel-examples/seatunnel-flink-examples/src/main/java/org/apache/seatunnel/example/flink/LocalFlinkExample.java
@@ -28,7 +28,7 @@ public class LocalFlinkExample {
 
     public static void main(String[] args) throws Exception {
         String configFile = getTestConfigFile("fake_to_console.conf");
-        CommandLineArgs flinkArgs = new CommandLineArgs(configFile, false);
+        CommandLineArgs flinkArgs = new CommandLineArgs(configFile, false, null);
         Seatunnel.run(flinkArgs, FLINK);
     }
 
diff --git a/seatunnel-examples/seatunnel-spark-examples/src/main/java/org/apache/seatunnel/example/spark/LocalSparkExample.java b/seatunnel-examples/seatunnel-spark-examples/src/main/java/org/apache/seatunnel/example/spark/LocalSparkExample.java
index 6610c99..d4b1e5e 100644
--- a/seatunnel-examples/seatunnel-spark-examples/src/main/java/org/apache/seatunnel/example/spark/LocalSparkExample.java
+++ b/seatunnel-examples/seatunnel-spark-examples/src/main/java/org/apache/seatunnel/example/spark/LocalSparkExample.java
@@ -28,7 +28,7 @@ public class LocalSparkExample {
 
     public static void main(String[] args) throws Exception {
         String configFile = getTestConfigFile("spark.batch.conf.template");
-        CommandLineArgs sparkArgs = new CommandLineArgs(configFile, false);
+        CommandLineArgs sparkArgs = new CommandLineArgs(configFile, false, null);
         Seatunnel.run(sparkArgs, SPARK);
     }