This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-13292-7029c0969a9f6ea9add7a11f9262f42fe2ce9da9 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit efecb179a100d0a621566ba1b0c015357f55c168 Author: Yohanes <[email protected]> AuthorDate: Sat Nov 21 17:20:40 2026 +0000 [Docs][Transform-V2] Rewrite DynamicCompile configuration guide (#13292) --- docs/en/transforms/dynamic-compile.md | 474 +++++++++++++++++---------------- docs/zh/transforms/dynamic-compile.md | 476 ++++++++++++++++++---------------- 2 files changed, 511 insertions(+), 439 deletions(-) diff --git a/docs/en/transforms/dynamic-compile.md b/docs/en/transforms/dynamic-compile.md index 3a0ec33a03..b940cd2df0 100644 --- a/docs/en/transforms/dynamic-compile.md +++ b/docs/en/transforms/dynamic-compile.md @@ -1,283 +1,317 @@ # DynamicCompile -> DynamicCompile transform plugin +> Compile custom Java, Groovy or Scala code to add or replace row fields. -## Description +## Support Those Engines + +SeaTunnel Zeta, Flink and Spark. Scala compilation is supported only on Zeta. -:::tip +## Description -important clause -You need to ensure the security of your service and prevent attackers from uploading destructive code +DynamicCompile computes output columns and their values using user-supplied source code. It keeps input columns that are not replaced and appends new columns to the output schema. Use it when an existing transform cannot express the required row calculation. Code runs inside the job process; expensive calculations or remote calls can reduce throughput. +:::caution Trusted code only +This plugin executes code with the permissions of the SeaTunnel process. It is not a sandbox. Restrict who can submit source code or change a source file, and review that code before running the job. ::: -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 +## Key Features + +- Compile inline code or read it from an absolute file path. +- Add columns or replace existing columns by name. +- Apply a shared rule to multiple tables or select complete per-table rules. ## Options -| name | type | required | default value | -|------------------|--------|----------|---------------| -| source_code | string | no | | -| compile_language | Enum | yes | | -| compile_pattern | Enum | no | SOURCE_CODE | -| absolute_path | string | no | | -| table_match_regex | string | no | `.*` | -| table_transform | array | no | - | -| rule_match_mode | string | no | `FIRST_MATCH` | +| Name | Type | Required | Default | Description | +| --- | --- | --- | --- | --- | +| compile_language | Enum | Yes, in each configured rule | - | `JAVA`, `GROOVY` or `SCALA`; Scala requires Zeta. | +| compile_pattern | Enum | No | `SOURCE_CODE` | Read inline code or a source file. | +| source_code | String | Conditional | - | Nonblank code required for `SOURCE_CODE`, including when `compile_pattern` is omitted. | +| absolute_path | String | Conditional | - | Nonblank file path required for `ABSOLUTE_PATH`. | +| table_match_regex | String | No | `.*` | Select tables for the transform-level rule. | +| table_transform | Array<Map> | No | `[]` | Complete per-table rules with an exact `table_path`. | +| rule_match_mode | Enum | No | `FIRST_MATCH` | Select the first or all entries for a repeated table path; see the explicit-setting requirement below. | +| common options | - | No | - | See [Transform Plugin](common-options/common-options.md). | +### compile_language [Enum] -### common options [string] +Choose `JAVA`, `GROOVY` or `SCALA`. Each configured rule needs its own language. A `table_transform`-only configuration does not require a transform-level language. -Transform plugin common parameters, please refer to [Transform Plugin](common-options/common-options.md) for details +Java uses Janino's `ClassBodyEvaluator`: provide imports and public methods as a class body, without a surrounding class declaration. Janino does not support every feature of the current Java language; see [Janino](https://github.com/janino-compiler/janino). -### compile_language [Enum] +Groovy code defines a class containing the required public methods. Scala code defines a class with equivalent JVM method signatures and uses the Scala REPL compiler; it is supported only on Zeta. -Some syntax in Java may not be supported, please refer https://github.com/janino-compiler/janino -GROOVY,JAVA,SCALA(Only Support Zeta) +### compile_pattern [Enum] -**Note**: SCALA support uses the Scala REPL for dynamic compilation and requires proper Scala syntax. +- `SOURCE_CODE` (default): compile `source_code`; it must be present and nonblank. +- `ABSOLUTE_PATH`: read and compile the file at `absolute_path`; the path must be present and nonblank. -### compile_pattern [Enum] +Each per-table entry uses its own pattern, defaulting to `SOURCE_CODE`. It does not inherit the transform-level pattern. + +### source_code [String] + +Inline source code, usually enclosed in a HOCON triple-quoted string. Implement these two public methods, with matching output order: -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 +```java +Column[] getInlineOutputColumns(CatalogTable inputCatalogTable) +Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) +``` -### absolute_path [string] +The first method defines the columns to produce. For an existing name, the callback replaces the row value and may change the column's data type; the column keeps its position and other metadata, such as nullability. A new name appends a column using the returned definition. Other input columns remain. -The absolute path of the Java, Groovy, or Scala source file on the server +The second method computes the values for one row. Return one value per declared output column, in the same order and with compatible types. Read input fields by their input-schema indexes through `SeaTunnelRowAccessor`. -### source_code [string] +Use `org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor` in new code. If the code needs third-party libraries, make them available in `${SEATUNNEL_HOME}/lib` on Zeta, or the corresponding Flink/Spark service library directory, and restart the service to load them. -The source code. +### absolute_path [String] -### table_match_regex / table_transform / rule_match_mode +An absolute path to the source file, read when the transform is constructed. Make the file available at that path wherever the transform is initialized; a file present only on the submission machine is not automatically distributed. The file must follow the same language and method contract as inline code. Changing it is not a hot-reload mechanism. -Multi-table options. When the upstream source outputs several tables, one DynamicCompile transform -can handle all of them: `table_match_regex` selects the tables the transform level rule applies to -(default `.*`), `table_transform` holds complete per-table rules (`table_path` plus the options -above), and `rule_match_mode` (`FIRST_MATCH` or `ALL_MATCH`) controls how duplicate `table_path` -entries are evaluated. See [Multi-Table Transform](transform-multi-table.md). +For example, replace the inline-code settings of a rule with: -#### Details about the source code +```hocon +compile_language = "JAVA" +compile_pattern = "ABSOLUTE_PATH" +absolute_path = "/opt/seatunnel/transforms/TagTransform.java" +``` -In the source code, you must implement two method: -- `Column[] getInlineOutputColumns(CatalogTable inputCatalogTable)` -- `Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow)` +### table_match_regex [String] -`getInlineOutputColumns` method, input parameter is `CatalogTable`, return type is `Column[]`. -you can get the current table's schema from `CatalogTable`. -if the return column exist in current schema, then it will overwrite by returned value (field type, comment, ...), if it's a new column, it will add into current schema. +A Java regular expression matched against the whole input table path. It selects tables for a configured transform-level rule and defaults to `.*`. It neither limits matching `table_transform` entries nor creates a transform-level rule when none is configured. -`getInlineOutputFieldValues` method, input parameter is `SeaTunnelRowAccessor`, return type is `Object[]` -You can get the record from `SeaTunnelRowAccessor`, do you own customized data process logical. -The return `Object[]` array length should match with `getInlineOutputColumns` method result's length. and the order also need be match. +### table_transform [Array<Map>] -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. -You need restart the server to load the lib file. +Each entry contains an exact, nonblank `table_path` and a complete compilation rule: `compile_language`, optional `compile_pattern`, and the code or path required by that pattern. Entries do not inherit compilation settings from the transform level. +A matching entry takes precedence over the transform-level rule, even if the table does not match `table_match_regex`. + +### rule_match_mode [Enum] + +For entries with the same exact `table_path`: + +- Explicit `FIRST_MATCH`: use the first entry in list order. +- Explicit `ALL_MATCH`: apply all entries in list order. Each rule receives the schema and row produced by the previous rule. + +Although the default is `FIRST_MATCH`, omitting this option rejects duplicate table paths during transform initialization. Set it explicitly if repeated paths are intentional. + +### common options + +Use `plugin_input` and `plugin_output` to connect this transform to other plugins. See [Transform Plugin](common-options/common-options.md). + +## Multi-Table Configuration + +The effective rule for each input table is: + +1. Matching `table_transform` entry or entries. +2. Otherwise, the transform-level rule, if configured and selected by `table_match_regex`. +3. Otherwise, unchanged rows and schema. + +A transform-level rule must be complete if provided. With only `table_transform`, unlisted tables pass through unchanged. See [Multi-Table Transform](transform-multi-table.md). ## Example -The data read from source is a table like this: +These complete batch configurations use two FakeSource tables, each with one `id` column. The Java code adds a constant `tag` column and keeps `id`. Console row order and generated IDs can vary; the expected table schemas and tags below do not depend on them. -| name | age | card | -|----------|-----|------| -| Joy Ding | 20 | 123 | -| May Ding | 20 | 123 | -| Kin Dom | 30 | 123 | -| Joy Dom | 30 | 123 | +| Table | Input schema | Transform-level rule only | With a per-table override | Per-table rules only | +| --- | --- | --- | --- | --- | +| `demo.orders` | `id INT` | `id INT, tag STRING` (`default`) | `id INT, tag STRING` (`orders`) | `id INT, tag STRING` (`orders`) | +| `demo.returns` | `id INT` | `id INT, tag STRING` (`default`) | `id INT, tag STRING` (`default`) | `id INT` (unchanged) | -Use this DynamicCompile to add a new column `compile_language`, and update the `age` field by its original value (if age = 20, update to 40) +### Transform-level rule only +Both `demo.orders` and `demo.returns` produce `id, tag`, with `tag = "default"`. -- use groovy ```hocon +env { job.mode = "BATCH" } +source { + FakeSource { + plugin_output = "input" + tables_configs = [ + { row.num = 1, schema = { table = "demo.orders", fields { id = "int" } } }, + { row.num = 1, schema = { table = "demo.returns", fields { id = "int" } } } + ] + } +} transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "groovy_out" - compile_language="GROOVY" - compile_pattern="SOURCE_CODE" - source_code=""" - import org.apache.seatunnel.api.table.catalog.Column - import org.apache.seatunnel.api.table.type.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) { - PhysicalColumn col1 = - PhysicalColumn.of( - "compile_language", - BasicType.STRING_TYPE, - 10L, - true, - "", - ""); - PhysicalColumn col2 = - PhysicalColumn.of( - "age", - BasicType.INT_TYPE, - 0L, - false, - false, - "" - ); - return new Column[]{ - col1, col2 - }; - } - - - public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { - Object[] fieldValues = new Object[2]; - // get age - Object ageField = inputRow.getField(1); - fieldValues[0] = "GROOVY"; - if (Integer.parseInt(ageField.toString()) == 20) { - fieldValues[1] = 40; - } else { - fieldValues[1] = ageField; - } - return fieldValues; - } - };""" - + DynamicCompile { + plugin_input = "input" + plugin_output = "output" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"default"}; + } + """ } } +sink { + Console { plugin_input = "output" } +} ``` -- use java +### Transform-level rule with a per-table override + +`demo.orders` gets `tag = "orders"`; `demo.returns` gets `tag = "default"`. The orders rule replaces the shared rule; it is not applied after it. + ```hocon +env { job.mode = "BATCH" } +source { + FakeSource { + plugin_output = "input" + tables_configs = [ + { row.num = 1, schema = { table = "demo.orders", fields { id = "int" } } }, + { row.num = 1, schema = { table = "demo.returns", fields { id = "int" } } } + ] + } +} transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "java_out" - compile_language="JAVA" - compile_pattern="SOURCE_CODE" - source_code=""" - import org.apache.seatunnel.api.table.catalog.Column; - import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; - import org.apache.seatunnel.api.table.catalog.*; - import org.apache.seatunnel.api.table.type.*; - import java.util.ArrayList; - public Column[] getInlineOutputColumns(CatalogTable inputCatalogTable) { - PhysicalColumn col1 = - PhysicalColumn.of( - "compile_language", - BasicType.STRING_TYPE, - 10L, - true, - "", - ""); - PhysicalColumn col2 = - PhysicalColumn.of( - "age", - BasicType.INT_TYPE, - 0L, - false, - false, - "" - ); - return new Column[]{ - col1, col2 - }; - } - - - public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { - Object[] fieldValues = new Object[2]; - // get age - Object ageField = inputRow.getField(1); - fieldValues[0] = "JAVA"; - if (Integer.parseInt(ageField.toString()) == 20) { - fieldValues[1] = 40; - } else { - fieldValues[1] = ageField; - } - return fieldValues; - } - """ - + DynamicCompile { + plugin_input = "input" + plugin_output = "output" + table_match_regex = "demo[.].*" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"default"}; + } + """ + table_transform = [ + { + table_path = "demo.orders" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"orders"}; + } + """ + } + ] } - } - ``` -- use absolute path to read code -```hocon - transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "groovy_out" - compile_language="GROOVY" - compile_pattern="ABSOLUTE_PATH" - absolute_path="""/tmp/GroovyFile""" +} +sink { + Console { plugin_input = "output" } +} +``` + +### Per-table rules only + +Only `demo.orders` gains `tag = "orders"`. `demo.returns` keeps its original single-column `id` schema, even though the default `table_match_regex` matches it. +```hocon +env { job.mode = "BATCH" } +source { + FakeSource { + plugin_output = "input" + tables_configs = [ + { row.num = 1, schema = { table = "demo.orders", fields { id = "int" } } }, + { row.num = 1, schema = { table = "demo.returns", fields { id = "int" } } } + ] + } +} +transform { + DynamicCompile { + plugin_input = "input" + plugin_output = "output" + table_transform = [ + { + table_path = "demo.orders" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"orders"}; + } + """ + } + ] } } +sink { + Console { plugin_input = "output" } +} ``` -Then the data in result table `groovy_out` will like this +### Groovy and Scala source shapes + +To use Groovy, set `compile_language = "GROOVY"` and replace `source_code` in a rule with: -| name | age | card | compile_language | -|----------|-----|------|------------------| -| Joy Ding | 40 | 123 | GROOVY | -| May Ding | 40 | 123 | GROOVY | -| Kin Dom | 30 | 123 | GROOVY | -| Joy Dom | 30 | 123 | GROOVY | +```groovy +import org.apache.seatunnel.api.table.catalog.Column +import org.apache.seatunnel.api.table.catalog.CatalogTable +import org.apache.seatunnel.api.table.catalog.PhysicalColumn +import org.apache.seatunnel.api.table.type.BasicType +import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor -Then the data in result table `java_out` will like this +class GroovyTag { + public Column[] getInlineOutputColumns(CatalogTable table) { + return [PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "")] as Column[] + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return ["GROOVY"] as Object[] + } +} +``` -| name | age | card | compile_language | -|----------|-----|------|------------------| -| Joy Ding | 40 | 123 | JAVA | -| May Ding | 40 | 123 | JAVA | -| Kin Dom | 30 | 123 | JAVA | -| Joy Dom | 30 | 123 | JAVA | +For Scala on Zeta, set `compile_language = "SCALA"` and use: -- use scala -```hocon -transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "scala_out" - compile_language="SCALA" - compile_pattern="SOURCE_CODE" - source_code=""" - import org.apache.seatunnel.api.table.catalog.Column - import org.apache.seatunnel.api.table.catalog.CatalogTable - import org.apache.seatunnel.api.table.catalog.PhysicalColumn - import org.apache.seatunnel.api.table.`type`.SeaTunnelRowAccessor - import org.apache.seatunnel.api.table.`type`.BasicType - import java.util.ArrayList - - class ScalaDemo { - def getInlineOutputColumns(inputCatalogTable: CatalogTable): Array[Column] = { - val columns = new ArrayList[Column]() - val destColumn = PhysicalColumn.of( - "compile_language", - BasicType.STRING_TYPE, - 10L, - true, - "", - "" - ) - columns.add(destColumn) - columns.toArray(new Array[Column](0)) - } - - def getInlineOutputFieldValues(inputRow: SeaTunnelRowAccessor): Array[Object] = { - Array[Object]("SCALA") - } - } - """ +```scala +import org.apache.seatunnel.api.table.catalog.{Column, CatalogTable, PhysicalColumn} +import org.apache.seatunnel.api.table.`type`.{BasicType, SeaTunnelRowAccessor} + +class ScalaTag { + def getInlineOutputColumns(table: CatalogTable): Array[Column] = { + Array[Column](PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "")) + } + def getInlineOutputFieldValues(row: SeaTunnelRowAccessor): Array[Object] = { + Array[Object]("SCALA") } } ``` -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 +Both variants preserve `id` and add `tag` with the language name. For additional configurations, see the [DynamicCompile E2E fixtures](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/zh/transforms/dynamic-compile.md b/docs/zh/transforms/dynamic-compile.md index d7afbd881b..0a8b3a0e37 100644 --- a/docs/zh/transforms/dynamic-compile.md +++ b/docs/zh/transforms/dynamic-compile.md @@ -1,278 +1,316 @@ -# 动态编译转换 +# DynamicCompile -> DynamicCompile:在运行时编译并执行自定义代码,完成灵活的数据处理 +> 动态编译 Java、Groovy 或 Scala 代码,新增或替换行字段。 -## 描述 - -:::tip +## 支持的引擎 -特别声明 -您需要确保服务的安全性,并防止攻击者上传破坏性代码 +SeaTunnel Zeta、Flink 和 Spark。Scala 编译仅支持 Zeta。 -::: +## 描述 -DynamicCompile 转换插件提供一种可编程的方式来处理行,允许用户自定义任何业务行为,甚至基于现有行字段作为参数的 RPC 请求,或者通过从其他数据源检索相关数据来扩展字段。为了区分业务,您还可以定义多个转换进行组合, -如果转换过于复杂,可能会影响性能 +DynamicCompile 使用用户提供的源码定义输出列并计算字段值。未被替换的输入列会保留,新列追加到输出表结构。当现有转换插件无法表达所需的行计算时,可以使用此插件。代码在作业进程中执行,复杂计算或远程调用可能降低吞吐量。 -## 属性 +:::caution 仅执行可信代码 +插件以 SeaTunnel 进程的权限执行代码,不提供沙箱隔离。请限制源码提交和源码文件的修改权限,并在运行作业前审核代码。 +::: -| name | type | required | default value | -|------------------|--------|----------|---------------| -| source_code | string | no | | -| compile_language | Enum | yes | | -| compile_pattern | Enum | no | SOURCE_CODE | -| absolute_path | string | no | | -| table_match_regex | string | no | `.*` | -| table_transform | array | no | - | -| rule_match_mode | string | no | `FIRST_MATCH` | +## 主要特性 +- 编译内联源码,或从绝对路径读取源码文件。 +- 根据列名新增列或替换现有列。 +- 对多张表应用公共规则,或配置完整的单表规则。 -### 通用选项 [string] +## 选项 -转换插件的常见参数, 请参考 [Transform Plugin](common-options/common-options.md) 了解详情。 +| 名称 | 类型 | 是否必填 | 默认值 | 说明 | +| --- | --- | --- | --- | --- | +| compile_language | Enum | 每个已配置的规则必填 | - | `JAVA`、`GROOVY` 或 `SCALA`;Scala 需要 Zeta。 | +| compile_pattern | Enum | 否 | `SOURCE_CODE` | 选择内联源码或源码文件。 | +| source_code | String | 条件必填 | - | `SOURCE_CODE` 模式下必须提供非空白源码,省略 `compile_pattern` 时同样必填。 | +| absolute_path | String | 条件必填 | - | `ABSOLUTE_PATH` 模式下必须提供非空白路径。 | +| table_match_regex | String | 否 | `.*` | 选择应用转换级别规则的表。 | +| table_transform | Array<Map> | 否 | `[]` | 包含精确 `table_path` 的完整单表规则。 | +| rule_match_mode | Enum | 否 | `FIRST_MATCH` | 对重复表路径使用首个规则或全部规则;显式配置要求见下文。 | +| common options | - | 否 | - | 参见[转换插件](common-options/common-options.md)。 | ### compile_language [Enum] -Java中的某些语法可能不受支持,请参阅https://github.com/janino-compiler/janino -GROOVY,JAVA,SCALA(目前支持 Zeta) +可选 `JAVA`、`GROOVY`、`SCALA`。每个已配置的规则都需要自己的语言设置。仅配置 `table_transform` 时,转换级别不需要设置语言。 + +Java 使用 Janino 的 `ClassBodyEvaluator`:源码应为 import 语句和 public 方法组成的类体,不要再包裹一层 class 声明。Janino 不支持当前 Java 语言的所有特性,参见 [Janino](https://github.com/janino-compiler/janino)。 -**注意**:SCALA 支持使用 Scala REPL 进行动态编译,需要编写符合 Scala 语法的代码。 +Groovy 源码应定义包含所需 public 方法的类。Scala 源码应定义具有等效 JVM 方法签名的类,通过 Scala REPL 编译器进行编译,仅支持 Zeta。 ### compile_pattern [Enum] -SOURCE_CODE,ABSOLUTE_PATH -选择 SOURCE_CODE,SOURCE_CODE 属性必填;选择ABSOLUTE_PATH,ABSOLUTE_PATH属性必填。 +- `SOURCE_CODE`(默认):编译 `source_code`,源码必须存在且不能全为空白。 +- `ABSOLUTE_PATH`:读取并编译 `absolute_path` 指定的文件,路径必须存在且不能全为空白。 + +每个单表条目使用自己的模式,默认为 `SOURCE_CODE`,不会继承转换级别的模式。 + +### source_code [String] + +内联源码,通常使用 HOCON 三引号字符串。实现以下两个 public 方法,并保持输出顺序一致: + +```java +Column[] getInlineOutputColumns(CatalogTable inputCatalogTable) +Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) +``` + +第一个方法定义输出列。对于现有列名,回调会替换行中的字段值,并可改变该列的数据类型;列的位置和其他元数据(例如是否可为空)保持不变。新名称的列按返回的列定义追加到表结构,其他输入列保持不变。 + +第二个方法计算单行的输出值。返回值数量必须与声明的输出列数一致,顺序和类型也必须匹配。通过 `SeaTunnelRowAccessor` 按输入表结构的字段索引读取输入值。 -### absolute_path [string] +新代码请使用 `org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor`。如果源码需要第三方依赖,请将依赖部署到 Zeta 的 `${SEATUNNEL_HOME}/lib`,或对应 Flink/Spark 服务的库目录,并重启服务以加载依赖。 -服务器上Java、Groovy或Scala源码文件的绝对路径 +### absolute_path [String] -### source_code [string] -源代码 +源码文件的绝对路径,在构造转换时读取。应确保所有初始化该转换的位置都可以通过该路径访问文件;提交机器上的本地文件不会自动分发。文件内容应满足与内联源码相同的语言和方法约定。修改文件不代表代码会热更新。 -### table_match_regex / table_transform / rule_match_mode +例如,将某个规则的内联源码设置替换为: -多表选项。当上游数据源输出多张表时,一个 DynamicCompile 转换即可处理所有这些表:`table_match_regex` 选择转换级别规则适用的表(默认 `.*`),`table_transform` 保存完整的单表规则(`table_path` 加上上述选项),`rule_match_mode`(`FIRST_MATCH` 或 `ALL_MATCH`)控制相同 `table_path` 条目的求值方式。参见 [多表转换](transform-multi-table.md)。 +```hocon +compile_language = "JAVA" +compile_pattern = "ABSOLUTE_PATH" +absolute_path = "/opt/seatunnel/transforms/TagTransform.java" +``` + +### table_match_regex [String] + +对完整输入表路径进行匹配的 Java 正则表达式,默认 `.*`。它选择应用已配置的转换级别规则的表,不限制匹配的 `table_transform` 条目,也不会在未配置时自动生成转换级别规则。 + +### table_transform [Array<Map>] + +每个条目包含精确且非空白的 `table_path`,以及完整的编译规则:`compile_language`、可选的 `compile_pattern`,以及该模式要求的源码或路径。条目不会继承转换级别的编译设置。 + +匹配的条目优先于转换级别规则,即使该表不匹配 `table_match_regex`,条目仍然生效。 -#### 关于source_code -在代码中,你必须实现两个方法 -- `Column[] getInlineOutputColumns(CatalogTable inputCatalogTable)` -- `Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow)` +### rule_match_mode [Enum] -`getInlineOutputColumns`方法中,入参类型为`CatalogTable`,返回结果为`Column[]`。 -你可以从入参的`CatalogTable`获取当前表的表结构。 -在返回结果中,如果字段已经存在,则会根据返回结果进行覆盖,如果不存在,则会添加到现有表结构中。 +当多个条目具有相同的精确 `table_path` 时: -`getInlineOutputFieldValues`方法,入参类型为`SeaTunnelRowAccessor`,返回结果为`Object[]` -你可以从`SeaTunnelRowAccessor`获取到当前行的数据,进行自己的定制化数据处理逻辑。 -返回结果中,数组长度需要与`getInlineOutputColumns`方法返回的长度一致,并且里面的字段值顺序也需要保持一致。 +- 显式设置 `FIRST_MATCH`:按列表顺序使用首个条目。 +- 显式设置 `ALL_MATCH`:按列表顺序应用全部条目。每个规则接收上一个规则产生的表结构和行。 -如果有第三方依赖包,请将它们放在${SEATUNNEL_HOME}/lib中,如果您使用spark或flink,则需要将其放在相应服务的libs下。 -你需要重启集群服务,才能重新加载这些依赖。 +虽然默认值为 `FIRST_MATCH`,但省略该选项时,转换初始化会拒绝重复表路径。如果确实需要重复路径,请显式设置该选项。 +### common options + +通过 `plugin_input` 和 `plugin_output` 连接其他插件。参见[转换插件](common-options/common-options.md)。 + +## 多表配置 + +每张输入表按以下顺序选择实际规则: + +1. 匹配的 `table_transform` 条目。 +2. 否则,使用已配置且被 `table_match_regex` 选中的转换级别规则。 +3. 否则,表结构和行保持不变。 + +如提供转换级别规则,规则必须完整。仅配置 `table_transform` 时,未列出的表保持不变。参见[多表转换](transform-multi-table.md)。 ## 示例 -源端数据读取的表格如下: +以下完整批作业配置使用两张 FakeSource 表,每张表只有一个 `id` 列。Java 代码保留 `id` 并新增常量 `tag` 列。Console 的行顺序和生成的 ID 可能变化,下文的预期表结构及标签不依赖这些值。 + +| 表 | 输入结构 | 仅转换级别规则 | 包含单表覆盖规则 | 仅单表规则 | +| --- | --- | --- | --- | --- | +| `demo.orders` | `id INT` | `id INT, tag STRING`(`default`) | `id INT, tag STRING`(`orders`) | `id INT, tag STRING`(`orders`) | +| `demo.returns` | `id INT` | `id INT, tag STRING`(`default`) | `id INT, tag STRING`(`default`) | `id INT`(不变) | -| name | age | card | -|----------|-----|------| -| Joy Ding | 20 | 123 | -| May Ding | 20 | 123 | -| Kin Dom | 30 | 123 | -| Joy Dom | 30 | 123 | +### 仅配置转换级别规则 -我们将使用`DynamicCompile`对数据进行修改,添加一列`compile_language`字段,并且将`age`字段更新,当`age=20`时将其更新为`40` +`demo.orders` 和 `demo.returns` 均输出 `id, tag`,其中 `tag = "default"`。 -- 使用groovy ```hocon +env { job.mode = "BATCH" } +source { + FakeSource { + plugin_output = "input" + tables_configs = [ + { row.num = 1, schema = { table = "demo.orders", fields { id = "int" } } }, + { row.num = 1, schema = { table = "demo.returns", fields { id = "int" } } } + ] + } +} transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "groovy_out" - compile_language="GROOVY" - compile_pattern="SOURCE_CODE" - source_code=""" - import org.apache.seatunnel.api.table.catalog.Column - import org.apache.seatunnel.api.table.type.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) { - PhysicalColumn col1 = - PhysicalColumn.of( - "compile_language", - BasicType.STRING_TYPE, - 10L, - true, - "", - ""); - PhysicalColumn col2 = - PhysicalColumn.of( - "age", - BasicType.INT_TYPE, - 0L, - false, - false, - "" - ); - return new Column[]{ - col1, col2 - }; - } - - - public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { - Object[] fieldValues = new Object[2]; - // get age - Object ageField = inputRow.getField(1); - fieldValues[0] = "GROOVY"; - if (Integer.parseInt(ageField.toString()) == 20) { - fieldValues[1] = 40; - } else { - fieldValues[1] = ageField; - } - return fieldValues; - } - };""" - + DynamicCompile { + plugin_input = "input" + plugin_output = "output" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"default"}; + } + """ } } +sink { + Console { plugin_input = "output" } +} ``` -- 使用java +### 转换级别规则与单表覆盖规则 + +`demo.orders` 的 `tag = "orders"`,`demo.returns` 的 `tag = "default"`。orders 的规则替代公共规则,而不是在公共规则之后再执行。 + ```hocon +env { job.mode = "BATCH" } +source { + FakeSource { + plugin_output = "input" + tables_configs = [ + { row.num = 1, schema = { table = "demo.orders", fields { id = "int" } } }, + { row.num = 1, schema = { table = "demo.returns", fields { id = "int" } } } + ] + } +} transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "java_out" - compile_language="JAVA" - compile_pattern="SOURCE_CODE" - source_code=""" - import org.apache.seatunnel.api.table.catalog.Column; - import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; - import org.apache.seatunnel.api.table.catalog.*; - import org.apache.seatunnel.api.table.type.*; - import java.util.ArrayList; - public Column[] getInlineOutputColumns(CatalogTable inputCatalogTable) { - PhysicalColumn col1 = - PhysicalColumn.of( - "compile_language", - BasicType.STRING_TYPE, - 10L, - true, - "", - ""); - PhysicalColumn col2 = - PhysicalColumn.of( - "age", - BasicType.INT_TYPE, - 0L, - false, - false, - "" - ); - return new Column[]{ - col1, col2 - }; - } - - - public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor inputRow) { - Object[] fieldValues = new Object[2]; - // get age - Object ageField = inputRow.getField(1); - fieldValues[0] = "JAVA"; - if (Integer.parseInt(ageField.toString()) == 20) { - fieldValues[1] = 40; - } else { - fieldValues[1] = ageField; - } - return fieldValues; - } - """ - + DynamicCompile { + plugin_input = "input" + plugin_output = "output" + table_match_regex = "demo[.].*" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"default"}; + } + """ + table_transform = [ + { + table_path = "demo.orders" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"orders"}; + } + """ + } + ] } - } - ``` -- 指定源码文件路径 -```hocon - transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "groovy_out" - compile_language="GROOVY" - compile_pattern="ABSOLUTE_PATH" - absolute_path="""/tmp/GroovyFile""" +} +sink { + Console { plugin_input = "output" } +} +``` +### 仅配置单表规则 + +只有 `demo.orders` 新增 `tag = "orders"`。`demo.returns` 保留原来的单列 `id` 表结构,即使默认的 `table_match_regex` 匹配该表也不会新增列。 + +```hocon +env { job.mode = "BATCH" } +source { + FakeSource { + plugin_output = "input" + tables_configs = [ + { row.num = 1, schema = { table = "demo.orders", fields { id = "int" } } }, + { row.num = 1, schema = { table = "demo.returns", fields { id = "int" } } } + ] + } +} +transform { + DynamicCompile { + plugin_input = "input" + plugin_output = "output" + table_transform = [ + { + table_path = "demo.orders" + compile_language = "JAVA" + source_code = """ + import org.apache.seatunnel.api.table.catalog.Column; + import org.apache.seatunnel.api.table.catalog.CatalogTable; + import org.apache.seatunnel.api.table.catalog.PhysicalColumn; + import org.apache.seatunnel.api.table.type.BasicType; + import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor; + + public Column[] getInlineOutputColumns(CatalogTable table) { + return new Column[] { + PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "") + }; + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return new Object[] {"orders"}; + } + """ + } + ] } } +sink { + Console { plugin_input = "output" } +} ``` -那么结果表 `groovy_out` 中的数据将会更新为: +### Groovy 和 Scala 源码形式 -| name | age | card | compile_language | -|----------|-----|------|------------------| -| Joy Ding | 40 | 123 | GROOVY | -| May Ding | 40 | 123 | GROOVY | -| Kin Dom | 30 | 123 | GROOVY | -| Joy Dom | 30 | 123 | GROOVY | +使用 Groovy 时,将规则中的 `compile_language` 设为 `"GROOVY"`,并将 `source_code` 替换为: -那么结果表 `java_out` 中的数据将会更新为: +```groovy +import org.apache.seatunnel.api.table.catalog.Column +import org.apache.seatunnel.api.table.catalog.CatalogTable +import org.apache.seatunnel.api.table.catalog.PhysicalColumn +import org.apache.seatunnel.api.table.type.BasicType +import org.apache.seatunnel.api.table.type.SeaTunnelRowAccessor -| name | age | card | compile_language | -|----------|-----|------|------------------| -| Joy Ding | 40 | 123 | JAVA | -| May Ding | 40 | 123 | JAVA | -| Kin Dom | 30 | 123 | JAVA | -| Joy Dom | 30 | 123 | JAVA | +class GroovyTag { + public Column[] getInlineOutputColumns(CatalogTable table) { + return [PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "")] as Column[] + } + public Object[] getInlineOutputFieldValues(SeaTunnelRowAccessor row) { + return ["GROOVY"] as Object[] + } +} +``` -- 使用 Scala +在 Zeta 上使用 Scala 时,将 `compile_language` 设为 `"SCALA"`,并使用: -下面的 Scala 示例演示如何新增 `compile_language` 字段。 +```scala +import org.apache.seatunnel.api.table.catalog.{Column, CatalogTable, PhysicalColumn} +import org.apache.seatunnel.api.table.`type`.{BasicType, SeaTunnelRowAccessor} -```hocon -transform { - DynamicCompile { - plugin_input = "fake" - plugin_output = "scala_out" - compile_language="SCALA" - compile_pattern="SOURCE_CODE" - source_code=""" - import org.apache.seatunnel.api.table.catalog.Column - import org.apache.seatunnel.api.table.catalog.CatalogTable - import org.apache.seatunnel.api.table.catalog.PhysicalColumn - import org.apache.seatunnel.api.table.`type`.SeaTunnelRowAccessor - import org.apache.seatunnel.api.table.`type`.BasicType - import java.util.ArrayList - - class ScalaDemo { - def getInlineOutputColumns(inputCatalogTable: CatalogTable): Array[Column] = { - val columns = new ArrayList[Column]() - val destColumn = PhysicalColumn.of( - "compile_language", - BasicType.STRING_TYPE, - 10L, - true, - "", - "" - ) - columns.add(destColumn) - columns.toArray(new Array[Column](0)) - } - - def getInlineOutputFieldValues(inputRow: SeaTunnelRowAccessor): Array[Object] = { - Array[Object]("SCALA") - } - } - """ +class ScalaTag { + def getInlineOutputColumns(table: CatalogTable): Array[Column] = { + Array[Column](PhysicalColumn.of("tag", BasicType.STRING_TYPE, 20L, true, "", "")) + } + def getInlineOutputFieldValues(row: SeaTunnelRowAccessor): Array[Object] = { + Array[Object]("SCALA") } } ``` -更多复杂例子可以参考 -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 +两个版本均保留 `id`,并新增值为语言名称的 `tag`。更多配置可参考 [DynamicCompile E2E 配置](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
