Skip to content

Commit

Permalink
[Improve][Transform] Improve DynamicCompile transform (apache#7319)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyyyyyssss authored Aug 7, 2024
1 parent 16eeb1c commit 064fcad
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 15 deletions.
47 changes: 33 additions & 14 deletions docs/en/transform-v2/dynamic-compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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="""
Expand All @@ -70,7 +77,7 @@ transform {
List<Column> columns = new ArrayList<>();
PhysicalColumn destColumn =
PhysicalColumn.of(
"aa",
"compile_language",
BasicType.STRING_TYPE,
10,
true,
Expand All @@ -81,7 +88,7 @@ transform {
}
public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) {
Object[] fieldValues = new Object[1];
fieldValues[0]="AA"
fieldValues[0]="GROOVY"
return fieldValues;
}
};"""
Expand All @@ -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="""
Expand All @@ -106,7 +113,7 @@ transform {
ArrayList<Column> columns = new ArrayList<Column>();
PhysicalColumn destColumn =
PhysicalColumn.of(
"aa",
"compile_language",
BasicType.STRING_TYPE,
10,
true,
Expand All @@ -119,7 +126,7 @@ transform {
}
public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) {
Object[] fieldValues = new Object[1];
fieldValues[0]="AA";
fieldValues[0]="JAVA";
return fieldValues;
}
"""
Expand All @@ -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"""
Expand All @@ -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

171 changes: 171 additions & 0 deletions docs/zh/transform-v2/dynamic-compile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# DynamicCompile

> 动态编译插件
## 描述

:::tip

特别申明
您需要确保服务的安全性,并防止攻击者上传破坏性代码

:::

提供一种可编程的方式来处理行,允许用户自定义任何业务行为,甚至基于现有行字段作为参数的RPC请求,或者通过从其他数据源检索相关数据来扩展字段。为了区分业务,您还可以定义多个转换进行组合,
如果转换过于复杂,可能会影响性能

## 属性

| name | type | required | default value |
|------------------|--------|----------|---------------|
| source_code | string | no | |
| compile_language | Enum | yes | |
| compile_pattern | Enum | no | SOURCE_CODE |
| absolute_path | string | no | |

### source_code [string]

代码必须实现两个方法:getInlineOutputColumns和getInlineOutputFieldValues。getInlineOutputColumns确定要添加或转换的列,原始列结构可以从CatalogTable中获得
GetInlineOutputFieldValues决定您的列值。您可以满足任何要求,甚至可以完成RPC请求以基于原始列获取新值
如果有第三方依赖包,请将它们放在${SEATUNNEL_HOME}/lib中,如果您使用spark或flink,则需要将其放在相应服务的libs下。

### common options [string]

转换插件的常见参数, 请参考 [Transform Plugin](common-options.md) 了解详情。

### compile_language [Enum]

Java中的某些语法可能不受支持,请参阅https://github.com/janino-compiler/janino
GROOVY,JAVA

### compile_pattern [Enum]

SOURCE_CODE,ABSOLUTE_PATH
选择 SOURCE_CODE,SOURCE_CODE 属性必填;选择ABSOLUTE_PATH,ABSOLUTE_PATH属性必填。

### absolute_path [string]

服务器上Java或Groovy文件的绝对路径

## Example

源端数据读取的表格如下:

| name | age | card |
|----------|-----|------|
| Joy Ding | 20 | 123 |
| May Ding | 20 | 123 |
| Kin Dom | 20 | 123 |
| Joy Dom | 20 | 123 |

```
transform {
DynamicCompile {
source_table_name = "fake"
result_table_name = "groovy_out"
compile_language="GROOVY"
compile_pattern="SOURCE_CODE"
source_code="""
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.*;
import java.util.ArrayList;
class demo {
public Column[] getInlineOutputColumns(CatalogTable inputCatalogTable) {
List<Column> columns = new ArrayList<>();
PhysicalColumn destColumn =
PhysicalColumn.of(
"compile_language",
BasicType.STRING_TYPE,
10,
true,
"",
"");
columns.add(destColumn);
return columns.toArray(new Column[0]);
}
public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) {
Object[] fieldValues = new Object[1];
fieldValues[0]="GROOVY"
return fieldValues;
}
};"""
}
}
transform {
DynamicCompile {
source_table_name = "fake"
result_table_name = "java_out"
compile_language="JAVA"
compile_pattern="SOURCE_CODE"
source_code="""
import org.apache.seatunnel.api.table.catalog.Column;
import org.apache.seatunnel.transform.common.SeaTunnelRowAccessor;
import org.apache.seatunnel.api.table.catalog.*;
import org.apache.seatunnel.api.table.type.*;
import java.util.ArrayList;
public Column[] getInlineOutputColumns(CatalogTable inputCatalogTable) {
ArrayList<Column> columns = new ArrayList<Column>();
PhysicalColumn destColumn =
PhysicalColumn.of(
"compile_language",
BasicType.STRING_TYPE,
10,
true,
"",
"");
return new Column[]{
destColumn
};
}
public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) {
Object[] fieldValues = new Object[1];
fieldValues[0]="JAVA";
return fieldValues;
}
"""
}
}
transform {
DynamicCompile {
source_table_name = "fake"
result_table_name = "groovy_out"
compile_language="GROOVY"
compile_pattern="ABSOLUTE_PATH"
absolute_path="""/tmp/GroovyFile"""
}
}
```

那么结果表 `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 |

更多复杂例子可以参考
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

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -86,4 +157,11 @@ public void testDynamicSinglePathJava(TestContainer container)
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());
}
}
Loading

0 comments on commit 064fcad

Please sign in to comment.