Skip to content

Commit

Permalink
[Feature][Transform] DynamicCompile add transform (#7170)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackyyyyyssss authored Jul 20, 2024
1 parent 1e7c78d commit 821dfc8
Show file tree
Hide file tree
Showing 24 changed files with 1,447 additions and 2 deletions.
128 changes: 128 additions & 0 deletions docs/en/transform-v2/dynamic-compile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# 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

## Options

| name | type | required | default value |
|------------------|--------|----------|---------------|
| source_code | string | yes | |
| compile_language | string | yes | |

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

### common options [string]

Transform plugin common parameters, please refer to [Transform Plugin](common-options.md) for details

### compile_language [string]

Some syntax in Java may not be supported, please refer https://github.com/janino-compiler/janino
GROOVY,JAVA

## Example

The data read from source is a table like this:

| 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 = "fake1"
compile_language="GROOVY"
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(
"aa",
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]="AA"
return fieldValues;
}
};"""
}
}
transform {
DynamicCompile {
source_table_name = "fake"
result_table_name = "fake1"
compile_language="JAVA"
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(
"aa",
BasicType.STRING_TYPE,
10,
true,
"",
"");
return new Column[]{
destColumn
};
}
public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) {
Object[] fieldValues = new Object[1];
fieldValues[0]="AA";
return fieldValues;
}
"""
}
}
```

Then the data in result table `fake1` will like this

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

## Changelog

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<spotless.version>2.29.0</spotless.version>
<jsqlparser.version>4.5</jsqlparser.version>
<json-path.version>2.7.0</json-path.version>
<groovy.version>4.0.16</groovy.version>
<!-- Option args -->
<skipUT>false</skipUT>
<skipIT>true</skipIT>
Expand Down
8 changes: 7 additions & 1 deletion seatunnel-e2e/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<properties>
<maven-jar-plugin.version>2.4</maven-jar-plugin.version>
<rest-assured.version>4.3.1</rest-assured.version>
<rest-assured.version>5.4.0</rest-assured.version>
</properties>
<dependencies>
<dependency>
Expand All @@ -60,6 +60,12 @@
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.e2e.transform;

import org.apache.seatunnel.e2e.common.container.TestContainer;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.TestTemplate;
import org.testcontainers.containers.Container;

import java.io.IOException;

public class TestDynamicCompileIT extends TestSuiteBase {

@TestTemplate
public void testDynamicSingleCompileGroovy(TestContainer container)
throws IOException, InterruptedException {
Container.ExecResult execResult =
container.executeJob(
"/dynamic_compile/single_dynamic_groovy_compile_transform.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}

@TestTemplate
public void testDynamicSingleCompileJava(TestContainer container)
throws IOException, InterruptedException {
Container.ExecResult execResult =
container.executeJob("/dynamic_compile/single_dynamic_java_compile_transform.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}

@TestTemplate
public void testDynamicMultipleCompileGroovy(TestContainer container)
throws IOException, InterruptedException {
Container.ExecResult execResult =
container.executeJob(
"/dynamic_compile/multiple_dynamic_groovy_compile_transform.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}

@TestTemplate
public void testDynamicMultipleCompileJava(TestContainer container)
throws IOException, InterruptedException {
Container.ExecResult execResult =
container.executeJob(
"/dynamic_compile/multiple_dynamic_java_compile_transform.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}

@TestTemplate
public void testDynamicMixedCompileJavaAndGroovy(TestContainer container)
throws IOException, InterruptedException {
Container.ExecResult execResult =
container.executeJob(
"/dynamic_compile/mixed_dynamic_groovy_java_compile_transform.conf");
Assertions.assertEquals(0, execResult.getExitCode());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#
# 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 {
parallelism = 1
job.mode = "BATCH"
}

source {
# This is a example source plugin **only for test and demonstrate the feature source plugin**
FakeSource {
result_table_name = "fake"
row.num = 100
parallelism = 1
schema = {
fields {
name = "string"
age = "int"
}
}
}
}

transform {
DynamicCompile {
source_table_name = "fake"
result_table_name = "fake1"
compile_language="JAVA"
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(
"col1",
BasicType.STRING_TYPE,
10,
true,
"",
"");
return new Column[]{
destColumn
};

}
public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) {

Object[] fieldValues = new Object[1];
fieldValues[0]="test1";
return fieldValues;
}
"""

}
DynamicCompile {
source_table_name = "fake1"
result_table_name = "fake2"
compile_language="GROOVY"
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(
"col2",
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]="test2"
return fieldValues;
}
};"""

}

}


sink {
Assert {
source_table_name = "fake2"
rules =
{
row_rules = [
{
rule_type = MIN_ROW
rule_value = 100
}
],
field_rules = [
{
field_name = col1
field_type = string
field_value = [
{
rule_type = NOT_NULL
equals_to = "test1"

}
]
},
{
field_name = col2
field_type = string
field_value = [
{
rule_type = NOT_NULL
equals_to = "test2"

}

]
}
]
}
}

}
Loading

0 comments on commit 821dfc8

Please sign in to comment.