This is an automated email from the ASF dual-hosted git repository. wanghailin pushed a commit to branch dev in repository https://gitbox.apache.org/repos/asf/seatunnel.git
The following commit(s) were added to refs/heads/dev by this push: new 064fcad36d [Improve][Transform] Improve DynamicCompile transform (#7319) 064fcad36d is described below commit 064fcad36d3f0d152b2ebc2be926ec0ed40972ab Author: lizhenglei <127465317+jackyyyyys...@users.noreply.github.com> AuthorDate: Wed Aug 7 21:57:57 2024 +0800 [Improve][Transform] Improve DynamicCompile transform (#7319) --- docs/en/transform-v2/dynamic-compile.md | 47 ++++++--- docs/{en => zh}/transform-v2/dynamic-compile.md | 75 +++++++++----- .../e2e/transform/TestDynamicCompileIT.java | 80 +++++++++++++- .../dynamic_compile/conf/mockserver-config.json | 18 ++++ .../single_dynamic_http_compile_transform.conf | 115 +++++++++++++++++++++ 5 files changed, 292 insertions(+), 43 deletions(-) diff --git a/docs/en/transform-v2/dynamic-compile.md b/docs/en/transform-v2/dynamic-compile.md index 4a772e8cbf..17e3b0047e 100644 --- a/docs/en/transform-v2/dynamic-compile.md +++ b/docs/en/transform-v2/dynamic-compile.md @@ -4,6 +4,13 @@ ## Description +:::tip + +important clause +You need to ensure the security of your service and prevent attackers from uploading destructive code + +::: + Provide a programmable way to process rows, allowing users to customize any business behavior, even RPC requests based on existing row fields as parameters, or to expand fields by retrieving associated data from other data sources. To distinguish businesses, you can also define multiple transforms to combine, If the conversion is too complex, it may affect performance @@ -55,7 +62,7 @@ The data read from source is a table like this: transform { DynamicCompile { source_table_name = "fake" - result_table_name = "fake1" + result_table_name = "groovy_out" compile_language="GROOVY" compile_pattern="SOURCE_CODE" source_code=""" @@ -70,7 +77,7 @@ transform { List<Column> columns = new ArrayList<>(); PhysicalColumn destColumn = PhysicalColumn.of( - "aa", + "compile_language", BasicType.STRING_TYPE, 10, true, @@ -81,7 +88,7 @@ transform { } public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { Object[] fieldValues = new Object[1]; - fieldValues[0]="AA" + fieldValues[0]="GROOVY" return fieldValues; } };""" @@ -92,7 +99,7 @@ transform { transform { DynamicCompile { source_table_name = "fake" - result_table_name = "fake1" + result_table_name = "java_out" compile_language="JAVA" compile_pattern="SOURCE_CODE" source_code=""" @@ -106,7 +113,7 @@ transform { ArrayList<Column> columns = new ArrayList<Column>(); PhysicalColumn destColumn = PhysicalColumn.of( - "aa", + "compile_language", BasicType.STRING_TYPE, 10, true, @@ -119,7 +126,7 @@ transform { } public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { Object[] fieldValues = new Object[1]; - fieldValues[0]="AA"; + fieldValues[0]="JAVA"; return fieldValues; } """ @@ -130,7 +137,7 @@ transform { transform { DynamicCompile { source_table_name = "fake" - result_table_name = "fake1" + result_table_name = "groovy_out" compile_language="GROOVY" compile_pattern="ABSOLUTE_PATH" absolute_path="""/tmp/GroovyFile""" @@ -139,14 +146,26 @@ transform { } ``` -Then the data in result table `fake1` will like this +Then the data in result table `groovy_out` will like this + +| name | age | card | compile_language | +|----------|-----|------|------------------| +| Joy Ding | 20 | 123 | GROOVY | +| May Ding | 20 | 123 | GROOVY | +| Kin Dom | 20 | 123 | GROOVY | +| Joy Dom | 20 | 123 | GROOVY | + +Then the data in result table `java_out` will like this + +| name | age | card | compile_language | +|----------|-----|------|------------------| +| Joy Ding | 20 | 123 | JAVA | +| May Ding | 20 | 123 | JAVA | +| Kin Dom | 20 | 123 | JAVA | +| Joy Dom | 20 | 123 | JAVA | -| name | age | card | aa | -|----------|-----|------|----| -| Joy Ding | 20 | 123 | AA | -| May Ding | 20 | 123 | AA | -| Kin Dom | 20 | 123 | AA | -| Joy Dom | 20 | 123 | AA | +More complex examples can be referred to +https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf ## Changelog diff --git a/docs/en/transform-v2/dynamic-compile.md b/docs/zh/transform-v2/dynamic-compile.md similarity index 60% copy from docs/en/transform-v2/dynamic-compile.md copy to docs/zh/transform-v2/dynamic-compile.md index 4a772e8cbf..0fef5c253e 100644 --- a/docs/en/transform-v2/dynamic-compile.md +++ b/docs/zh/transform-v2/dynamic-compile.md @@ -1,13 +1,20 @@ # DynamicCompile -> DynamicCompile transform plugin +> 动态编译插件 -## Description +## 描述 -Provide a programmable way to process rows, allowing users to customize any business behavior, even RPC requests based on existing row fields as parameters, or to expand fields by retrieving associated data from other data sources. To distinguish businesses, you can also define multiple transforms to combine, -If the conversion is too complex, it may affect performance +:::tip -## Options +特别申明 +您需要确保服务的安全性,并防止攻击者上传破坏性代码 + +::: + +提供一种可编程的方式来处理行,允许用户自定义任何业务行为,甚至基于现有行字段作为参数的RPC请求,或者通过从其他数据源检索相关数据来扩展字段。为了区分业务,您还可以定义多个转换进行组合, +如果转换过于复杂,可能会影响性能 + +## 属性 | name | type | required | default value | |------------------|--------|----------|---------------| @@ -18,31 +25,31 @@ If the conversion is too complex, it may affect performance ### source_code [string] -The code must implement two methods: getInlineOutputColumns and getInlineOutputFieldValues. getInlineOutputColumns determines the columns you want to add or convert, and the original column structure can be obtained from CatalogTable -GetInlineOutputFieldValues determines your column values. You can fulfill any of your requirements, and even complete RPC requests to obtain new values based on the original columns -If there are third-party dependency packages, please place them in ${SEATUNNEL_HOME}/lib, if you use spark or flink, you need to put it under the libs of the corresponding service. +代码必须实现两个方法:getInlineOutputColumns和getInlineOutputFieldValues。getInlineOutputColumns确定要添加或转换的列,原始列结构可以从CatalogTable中获得 +GetInlineOutputFieldValues决定您的列值。您可以满足任何要求,甚至可以完成RPC请求以基于原始列获取新值 +如果有第三方依赖包,请将它们放在${SEATUNNEL_HOME}/lib中,如果您使用spark或flink,则需要将其放在相应服务的libs下。 ### common options [string] -Transform plugin common parameters, please refer to [Transform Plugin](common-options.md) for details +转换插件的常见参数, 请参考 [Transform Plugin](common-options.md) 了解详情。 ### compile_language [Enum] -Some syntax in Java may not be supported, please refer https://github.com/janino-compiler/janino -GROOVY,JAVA +Java中的某些语法可能不受支持,请参阅https://github.com/janino-compiler/janino +GROOVY,JAVA ### compile_pattern [Enum] SOURCE_CODE,ABSOLUTE_PATH -If it is a SOURCE-CODE enumeration; the SOURCE-CODE attribute is required, and the ABSOLUTE_PATH enumeration;ABSOLUTE_PATH attribute is required +选择 SOURCE_CODE,SOURCE_CODE 属性必填;选择ABSOLUTE_PATH,ABSOLUTE_PATH属性必填。 ### absolute_path [string] -The absolute path of Java or Groovy files on the server +服务器上Java或Groovy文件的绝对路径 ## Example -The data read from source is a table like this: +源端数据读取的表格如下: | name | age | card | |----------|-----|------| @@ -55,7 +62,7 @@ The data read from source is a table like this: transform { DynamicCompile { source_table_name = "fake" - result_table_name = "fake1" + result_table_name = "groovy_out" compile_language="GROOVY" compile_pattern="SOURCE_CODE" source_code=""" @@ -70,7 +77,7 @@ transform { List<Column> columns = new ArrayList<>(); PhysicalColumn destColumn = PhysicalColumn.of( - "aa", + "compile_language", BasicType.STRING_TYPE, 10, true, @@ -81,7 +88,7 @@ transform { } public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { Object[] fieldValues = new Object[1]; - fieldValues[0]="AA" + fieldValues[0]="GROOVY" return fieldValues; } };""" @@ -92,7 +99,7 @@ transform { transform { DynamicCompile { source_table_name = "fake" - result_table_name = "fake1" + result_table_name = "java_out" compile_language="JAVA" compile_pattern="SOURCE_CODE" source_code=""" @@ -106,7 +113,7 @@ transform { ArrayList<Column> columns = new ArrayList<Column>(); PhysicalColumn destColumn = PhysicalColumn.of( - "aa", + "compile_language", BasicType.STRING_TYPE, 10, true, @@ -119,7 +126,7 @@ transform { } public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { Object[] fieldValues = new Object[1]; - fieldValues[0]="AA"; + fieldValues[0]="JAVA"; return fieldValues; } """ @@ -130,7 +137,7 @@ transform { transform { DynamicCompile { source_table_name = "fake" - result_table_name = "fake1" + result_table_name = "groovy_out" compile_language="GROOVY" compile_pattern="ABSOLUTE_PATH" absolute_path="""/tmp/GroovyFile""" @@ -139,14 +146,26 @@ transform { } ``` -Then the data in result table `fake1` will like this +那么结果表 `groovy_out` 中的数据将会更新为: + +| name | age | card | compile_language | +|----------|-----|------|------------------| +| Joy Ding | 20 | 123 | GROOVY | +| May Ding | 20 | 123 | GROOVY | +| Kin Dom | 20 | 123 | GROOVY | +| Joy Dom | 20 | 123 | GROOVY | + +那么结果表 `java_out` 中的数据将会更新为: + +| name | age | card | compile_language | +|----------|-----|------|------------------| +| Joy Ding | 20 | 123 | JAVA | +| May Ding | 20 | 123 | JAVA | +| Kin Dom | 20 | 123 | JAVA | +| Joy Dom | 20 | 123 | JAVA | -| name | age | card | aa | -|----------|-----|------|----| -| Joy Ding | 20 | 123 | AA | -| May Ding | 20 | 123 | AA | -| Kin Dom | 20 | 123 | AA | -| Joy Dom | 20 | 123 | AA | +更多复杂例子可以参考 +https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf ## Changelog diff --git a/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/java/org/apache/seatunnel/e2e/transform/TestDynamicCompileIT.java b/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/java/org/apache/seatunnel/e2e/transform/TestDynamicCompileIT.java index b57b332353..2528499fc1 100644 --- a/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/java/org/apache/seatunnel/e2e/transform/TestDynamicCompileIT.java +++ b/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/java/org/apache/seatunnel/e2e/transform/TestDynamicCompileIT.java @@ -17,18 +17,89 @@ package org.apache.seatunnel.e2e.transform; +import org.apache.seatunnel.e2e.common.TestResource; +import org.apache.seatunnel.e2e.common.container.ContainerExtendedFactory; import org.apache.seatunnel.e2e.common.container.TestContainer; +import org.apache.seatunnel.e2e.common.junit.TestContainerExtension; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.TestTemplate; import org.testcontainers.containers.Container; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; +import org.testcontainers.lifecycle.Startables; +import org.testcontainers.utility.DockerImageName; +import org.testcontainers.utility.DockerLoggerFactory; +import org.testcontainers.utility.MountableFile; +import java.io.File; import java.io.IOException; +import java.net.URL; +import java.util.Optional; +import java.util.stream.Stream; -public class TestDynamicCompileIT extends TestSuiteBase { +public class TestDynamicCompileIT extends TestSuiteBase implements TestResource { private final String basePath = "/dynamic_compile/conf/"; + private static final String TMP_DIR = "/tmp"; + private GenericContainer<?> mockserverContainer; + private static final String IMAGE = "mockserver/mockserver:5.14.0"; + + @BeforeAll + @Override + public void startUp() { + Optional<URL> resource = + Optional.ofNullable( + TestDynamicCompileIT.class.getResource( + "/dynamic_compile/conf/mockserver-config.json")); + this.mockserverContainer = + new GenericContainer<>(DockerImageName.parse(IMAGE)) + .withNetwork(NETWORK) + .withNetworkAliases("mockserver") + .withExposedPorts(1080) + .withCopyFileToContainer( + MountableFile.forHostPath( + new File( + resource.orElseThrow( + () -> + new IllegalArgumentException( + "Can not get config file of mockServer")) + .getPath()) + .getAbsolutePath()), + TMP_DIR + "/mockserver-config.json") + .withEnv( + "MOCKSERVER_INITIALIZATION_JSON_PATH", + TMP_DIR + "/mockserver-config.json") + .withEnv("MOCKSERVER_LOG_LEVEL", "WARN") + .withLogConsumer(new Slf4jLogConsumer(DockerLoggerFactory.getLogger(IMAGE))) + .waitingFor(new HttpWaitStrategy().forPath("/").forStatusCode(404)); + Startables.deepStart(Stream.of(mockserverContainer)).join(); + } + + @TestContainerExtension + protected final ContainerExtendedFactory extendedFactory = + container -> { + Container.ExecResult extraCommands = + container.execInContainer( + "bash", + "-c", + "mkdir -p /tmp/seatunnel/plugins/Fake/lib && cd /tmp/seatunnel/plugins/Fake/lib && wget " + + "https://repo1.maven.org/maven2/cn/hutool/hutool-all/5.3.6/hutool-all-5.3.6.jar"); + Assertions.assertEquals(0, extraCommands.getExitCode(), extraCommands.getStderr()); + }; + + @AfterAll + @Override + public void tearDown() { + if (mockserverContainer != null) { + mockserverContainer.stop(); + } + } + @TestTemplate public void testDynamicSingleCompileGroovy(TestContainer container) throws IOException, InterruptedException { @@ -86,4 +157,11 @@ public class TestDynamicCompileIT extends TestSuiteBase { container.executeJob(basePath + "single_java_path_compile.conf"); Assertions.assertEquals(0, execResult.getExitCode()); } + + @TestTemplate + public void testHttpDynamic(TestContainer container) throws IOException, InterruptedException { + Container.ExecResult execResult = + container.executeJob(basePath + "single_dynamic_http_compile_transform.conf"); + Assertions.assertEquals(0, execResult.getExitCode()); + } } diff --git a/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf/mockserver-config.json b/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf/mockserver-config.json new file mode 100644 index 0000000000..4890409f64 --- /dev/null +++ b/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf/mockserver-config.json @@ -0,0 +1,18 @@ + +// https://www.mock-server.com/mock_server/getting_started.html#request_matchers +[ + { + "httpRequest": { + "method": "GET", + "path": "/v1/compile" + }, + "httpResponse": { + "body": { + "compile": "seatunnel-compile" + }, + "headers": { + "Content-Type": "application/json" + } + } + } +] \ No newline at end of file diff --git a/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf/single_dynamic_http_compile_transform.conf b/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf/single_dynamic_http_compile_transform.conf new file mode 100644 index 0000000000..904066d69b --- /dev/null +++ b/seatunnel-e2e/seatunnel-transforms-v2-e2e/seatunnel-transforms-v2-e2e-part-2/src/test/resources/dynamic_compile/conf/single_dynamic_http_compile_transform.conf @@ -0,0 +1,115 @@ +# +# 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 streaming processing in seatunnel config +###### + +env { + job.mode = "BATCH" +} + +source { + FakeSource { + result_table_name = "fake" + row.num = 100 + schema = { + fields { + id = "int" + name = "string" + } + } + } +} + +transform { + DynamicCompile { + source_table_name = "fake" + result_table_name = "fake1" + compile_language="GROOVY" + compile_pattern="SOURCE_CODE" + source_code=""" + import cn.hutool.http.HttpUtil; + import org.apache.seatunnel.api.table.catalog.Column + import org.apache.seatunnel.transform.common.SeaTunnelRowAccessor + import org.apache.seatunnel.api.table.catalog.CatalogTable + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.*; + class HttpDemo { + + public Column[] getInlineOutputColumns(CatalogTable inputCatalogTable) { + List<Column> columns = new ArrayList<>(); + PhysicalColumn destColumn = + PhysicalColumn.of( + "DynamicCompile", + BasicType.STRING_TYPE, + 10, + true, + "", + ""); + columns.add(destColumn); + return columns.toArray(new Column[0]); + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { + + String body= HttpUtil.get("http://mockserver:1080/v1/compile"); + Object[] fieldValues = new Object[1]; + fieldValues[0]=body + return fieldValues; + } + };""" + + } +} + +sink { + Console { +Assert { + source_table_name = "fake1" + rules = + { + row_rules = [ + { + rule_type = MIN_ROW + rule_value = 100 + } + ], + field_rules = [ + { + field_name = id + field_type = int + field_value = [ + { + rule_type = NOT_NULL + } + ] + }, + { + field_name = DynamicCompile + field_type = string + field_value = [ + { + rule_type = NOT_NULL + + } + + ] + } + ] + } + } + } +} \ No newline at end of file