-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature][Transform] DynamicCompile add transform (#7170)
- Loading branch information
1 parent
1e7c78d
commit 821dfc8
Showing
24 changed files
with
1,447 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...-v2-e2e-part-2/src/test/java/org/apache/seatunnel/e2e/transform/TestDynamicCompileIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
...art-2/src/test/resources/dynamic_compile/mixed_dynamic_groovy_java_compile_transform.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
} | ||
|
||
] | ||
} | ||
] | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.