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-13186-ed683640863fafd617695061093638636e0b9e0b in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit dfdee68897d5f85b5e39d9c9dd8b698d77dc1fd6 Author: Kangyu Lu <[email protected]> AuthorDate: Tue Nov 24 08:23:27 2026 +0000 [Docs][Transform-V2] Rewrite FieldRename documentation (#13186) Signed-off-by: Kangyu Lu <[email protected]> Co-authored-by: Claude Sonnet 5 <[email protected]> --- docs/en/transforms/field-rename.md | 348 ++++++++++++++++++++++++++---------- docs/zh/transforms/field-rename.md | 350 +++++++++++++++++++++++++++---------- 2 files changed, 511 insertions(+), 187 deletions(-) diff --git a/docs/en/transforms/field-rename.md b/docs/en/transforms/field-rename.md index 07f4cf2c07..632e4f4835 100644 --- a/docs/en/transforms/field-rename.md +++ b/docs/en/transforms/field-rename.md @@ -2,150 +2,312 @@ > FieldRename transform plugin +## Support Those Engines + +> Spark<br/> +> Flink<br/> +> Seatunnel Zeta<br/> + ## Description -FieldRename transform plugin for rename field name. +The FieldRename transform renames the columns of a table without changing the table name or the row data. It can convert the case of field names, add a prefix or suffix, apply regex or exact-match replacement rules, and rename specific fields explicitly. Primary key and constraint key columns are renamed together with their fields. FieldRename supports multi-table configuration, see [Multi-Table Configuration](#multi-table-configuration). + +## Key Features + +- Convert every field name to `UPPER` or `LOWER` case +- Add a prefix and/or a suffix to every field name +- Replace every match of a regex, or replace a field name by exact match +- Rename specific fields explicitly; `specific` rules take precedence over all other rename options +- Rename primary key and constraint key columns together with their fields +- Handle multiple tables in one transform, see [Multi-Table Transform](transform-multi-table.md) ## Options -| name | type | required | default value | Description | -|:-----------------------:|--------|----------|---------------|-----------------------------------------------------------------------------------------------------------------------| -| convert_case | enum | no | | The case conversion type. The options can be `UPPER`, `LOWER` | -| prefix | string | no | | The prefix to be added to the field name | -| suffix | string | no | | The suffix to be added to the field name | -| replacements_with_regex | array | no | | The array of replacement rules. Each rule is a map with `replace_from`, `replace_to`, and optional `is_regex` (default `true`). When `is_regex=false`, `replace_from` is treated as an exact field name (full match). | -| specific | array | no | | Specific rename rules. Each rule is a map with `field_name` and `target_name`. When matched, it will rename the field directly and skip other rename rules. | -| table_match_regex | string | no | `.*` | Regex matched against the upstream table path. The transform level rule only applies to matching tables. | -| table_transform | array | no | - | Per-table rules, see [Multi-Table Transform](transform-multi-table.md). | -| rule_match_mode | string | no | `FIRST_MATCH` | How multiple `table_transform` entries with the same `table_path` are evaluated. | +| Name | Type | Required | Default | Description | +|------|------|----------|---------|-------------| +| convert_case | Enum | no | - | Case conversion for field names. Supported values: `UPPER`, `LOWER`. | +| prefix | String | no | - | Prefix added to every field name. | +| suffix | String | no | - | Suffix added to every field name. | +| replacements_with_regex | Array<Map> | no | - | Replacement rules. Each entry contains `replace_from`, `replace_to`, and optional `is_regex` (default `true`). | +| specific | Array<Map> | no | - | Explicit rename rules. Each entry contains `field_name` and `target_name`. A matching rule wins over all other rename options. | +| table_match_regex | String | no | `.*` | Regex of the table paths that the transform level rule applies to. | +| table_transform | Array<Map> | no | - | Per-table rules, see [Multi-Table Transform](transform-multi-table.md). | +| rule_match_mode | String | no | `FIRST_MATCH` | How multiple `table_transform` entries with the same `table_path` are evaluated. | +| common options | - | no | - | Transform plugin common parameters, see [Transform Plugin](common-options/common-options.md). | -## Examples +### convert_case [Enum] -### Convert field to uppercase +Converts every field name to uppercase (`UPPER`) or lowercase (`LOWER`). When it is not set, the case of the field name is unchanged. -``` +### prefix [String] + +Adds a prefix to every output field name. The prefix is added after `convert_case` and `replacements_with_regex` are applied, and is not case-converted itself. Leading and trailing whitespace in the value is ignored. + +### suffix [String] + +Adds a suffix to every output field name. The suffix is added after `convert_case` and `replacements_with_regex` are applied, and is not case-converted itself. Leading and trailing whitespace in the value is ignored. + +### replacements_with_regex [Array<Map>] + +A list of replacement rules. Each entry supports the following fields: + +| Field | Type | Required | Default | Description | +|-------|------|----------|---------|-------------| +| replace_from | String | yes | - | A Java regular expression, or an exact field name when `is_regex` is `false`. | +| replace_to | String | yes | - | The replacement text. Every match of `replace_from` is replaced with this text. | +| is_regex | Boolean | no | `true` | When `true`, `replace_from` is a regular expression. When `false`, `replace_from` must match the entire field name. | + +Every match of `replace_from` in the field name is replaced with `replace_to`. Matching is performed against the original field name, before `convert_case` is applied. The replacement text is spliced in after `convert_case` runs, so it is not case-converted itself: with `convert_case = "UPPER"` and `replace_to = "_x"`, the output keeps the lowercase `_x`. If several rules match the same field name, the last matching rule in the list is used. Leading and trailing whitespace in `replace_to [...] + +:::caution + +`replace_from` and `replace_to` must be set on every entry. An entry with an empty `replace_from` is ignored. If an entry matches a field but `replace_to` is missing, the job fails during initialization. + +::: + +### specific [Array<Map>] + +A list of explicit rename rules. Each entry contains `field_name` and `target_name`: + +| Field | Type | Required | Description | +|-------|------|----------|-------------| +| field_name | String | yes | The input field name to match. The comparison is case-sensitive. | +| target_name | String | yes | The output field name. | + +When a field matches a rule, it is renamed to `target_name` and `convert_case`, `replacements_with_regex`, `prefix`, and `suffix` are not applied to that field. If several rules match the same field name, the first one is used. + +### table_match_regex [String] + +A regular expression matched against the upstream table path. The transform level rule (the options outside `table_transform`) is applied only to the tables that match. The default `.*` matches every table. Tables that match neither this regex nor a `table_transform` entry are passed through unchanged. + +Note that this refers to the real upstream table name, not to `plugin_output`. + +### table_transform [Array<Map>] + +A list of per-table rules. Each entry must contain `table_path` in the form `databaseName[.schemaName].tableName` (matched exactly), plus the FieldRename options to apply to that table. + +When a table has an entry in `table_transform`, the transform level rule is not applied to it. See [Multi-Table Configuration](#multi-table-configuration). + +### rule_match_mode [String] + +Controls what happens when several `table_transform` entries have the same `table_path`. Supported values are `FIRST_MATCH` (apply only the first matching entry) and `ALL_MATCH` (apply all matching entries in order, each one receiving the output of the previous one). + +If `rule_match_mode` is not set, duplicate `table_path` entries are rejected. Set it explicitly to `FIRST_MATCH` or `ALL_MATCH` when several entries use the same `table_path`. + +### common options + +Transform plugin common parameters, please refer to [Transform Plugin](common-options/common-options.md) for details. + +## Multi-Table Configuration + +When the upstream source outputs several tables (for example JDBC or MySQL-CDC), one FieldRename transform can handle all of them. + +- Entries in `table_transform` take precedence over the transform level rule. A table that has an entry is never touched by the transform level rule. +- Every entry must contain `table_path` and the FieldRename options for that table. +- The transform level rule applies only to tables matching `table_match_regex`. + +The configuration below uses a transform level rule for `test.a.*` tables, a per-table rule for `test.xyz`, and leaves `test.www` unchanged: + +```hocon env { - parallelism = 1 - job.mode = "STREAMING" + parallelism = 1 + job.mode = "BATCH" } source { - MySQL-CDC { - plugin_output = "customers_mysql_cdc" - - username = "root" - password = "123456" - table-names = ["source.user_shop", "source.user_order"] - url = "jdbc:mysql://localhost:3306/source" - } + FakeSource { + plugin_output = "fake" + tables_configs = [ + { + row.num = 2 + schema = { + table = "test.abc" + fields { id = int, name = string, age = int } + } + }, + { + row.num = 2 + schema = { + table = "test.abcd" + fields { id = int, name = string, age = int } + } + }, + { + row.num = 2 + schema = { + table = "test.xyz" + fields { id = int, name = string, age = int } + } + }, + { + row.num = 2 + schema = { + table = "test.www" + fields { id = int, name = string, age = int } + } + } + ] + } } transform { FieldRename { - plugin_input = "customers_mysql_cdc" - plugin_output = "trans_result" - + plugin_input = "fake" + plugin_output = "fake1" + + table_match_regex = "test.a.*" convert_case = "UPPER" prefix = "F_" - suffix = "_S" - replacements_with_regex = [ + + table_transform = [ { - replace_from = "create_time" - replace_to = "SOURCE_CREATE_TIME" + table_path = "test.xyz" + convert_case = "LOWER" + suffix = "_S" } ] } } sink { - Jdbc { - plugin_input = "trans_result" - - driver="oracle.jdbc.OracleDriver" - url="jdbc:oracle:thin:@oracle-host:1521/ORCLCDB" - user="myuser" - password="mypwd" - - generate_sink_sql = true - database = "ORCLCDB" - table = "${database_name}.${table_name}" - primary_keys = ["${primary_key}"] - - schema_save_mode = "CREATE_SCHEMA_WHEN_NOT_EXIST" - data_save_mode = "APPEND_DATA" + Console { + plugin_input = "fake1" } } ``` -### Rename specific fields +Result schema of each table: -``` -transform { - FieldRename { - plugin_input = "input" - plugin_output = "output" +| Table | Rule applied | Output fields | +|-------|--------------|---------------| +| test.abc | transform level rule (matches `test.a.*`) | F_ID, F_NAME, F_AGE | +| test.abcd | transform level rule (matches `test.a.*`) | F_ID, F_NAME, F_AGE | +| test.xyz | `table_transform` entry | id_S, name_S, age_S | +| test.www | none | id, name, age | - specific = [ - { field_name = "InvoiceNum", target_name = "invoice_num" } - ] - } -} -``` +## Example -### Convert field name to lowercase +### Single table -``` +The data read from source is a table like this: + +| InvoiceNum | VendorID | +|------------|----------| +| 1001 | Vendor A | +| 1002 | Vendor B | + +Convert camelCase field names to snake_case: + +```hocon env { - parallelism = 1 - job.mode = "STREAMING" + parallelism = 1 + job.mode = "BATCH" } source { - Oracle-CDC { - plugin_output = "customers_oracle_cdc" - - url = "jdbc:oracle:thin:@localhost:1521/ORCLCDB" - username = "dbzuser" - password = "dbz" - database-names = ["ORCLCDB"] - schema-names = ["DEBEZIUM"] - table-names = ["SOURCE.USER_SHOP", "SOURCE.USER_ORDER"] + FakeSource { + plugin_output = "fake" + schema = { + columns = [ + { name = "InvoiceNum", type = "bigint" }, + { name = "VendorID", type = "string" } + ] + } + rows = [ + { kind = INSERT, fields = [1001, "Vendor A"] }, + { kind = INSERT, fields = [1002, "Vendor B"] } + ] } } transform { FieldRename { - plugin_input = "customers_oracle_cdc" - plugin_output = "trans_result" - + plugin_input = "fake" + plugin_output = "fake1" + convert_case = "LOWER" - prefix = "f_" - suffix = "_s" replacements_with_regex = [ { - replace_from = "CREATE_TIME" - replace_to = "source_create_time" + replace_from = "(?<=[a-z0-9])(?=[A-Z])" + replace_to = "_" } ] } } sink { - Jdbc { - plugin_input = "trans_result" - - url = "jdbc:mysql://localhost:3306/test" - driver = "com.mysql.cj.jdbc.Driver" - user = "st_user_sink" - password = "mysqlpw" - - generate_sink_sql = true - database = "${schema_name}" - table = "${table_name}" - primary_keys = ["${primary_key}"] - - schema_save_mode = "CREATE_SCHEMA_WHEN_NOT_EXIST" - data_save_mode = "APPEND_DATA" + Console { + plugin_input = "fake1" + } +} +``` + +The data in the result table `fake1` is: + +| invoice_num | vendor_id | +|-------------|-----------| +| 1001 | Vendor A | +| 1002 | Vendor B | + +The regex matches the position between a lowercase letter or digit and an uppercase letter. The underscore is inserted at that position, so `InvoiceNum` becomes `invoice_num` and `VendorID` becomes `vendor_id`. + +### Add a prefix and suffix + +```hocon +transform { + FieldRename { + plugin_input = "fake" + plugin_output = "fake1" + prefix = "f_" + suffix = "_s" + } +} +``` + +The fields become `f_InvoiceNum_s` and `f_VendorID_s`. + +### Rename specific fields + +```hocon +transform { + FieldRename { + plugin_input = "fake" + plugin_output = "fake1" + specific = [ + { field_name = "InvoiceNum", target_name = "invoice_number" } + ] + } +} +``` + +`InvoiceNum` becomes `invoice_number` and `VendorID` is unchanged. Because `specific` takes precedence, any `convert_case`, `replacements_with_regex`, `prefix`, or `suffix` option is ignored for a matching field. + +### Replace a field name by exact match + +```hocon +transform { + FieldRename { + plugin_input = "fake" + plugin_output = "fake1" + replacements_with_regex = [ + { + replace_from = "VendorID" + replace_to = "vendor_id" + is_regex = false + } + ] } } ``` + +`VendorID` becomes `vendor_id`; `InvoiceNum` is unchanged. With `is_regex = false`, only a field whose full name equals `replace_from` is replaced. + +### Multiple tables + +See [Multi-Table Configuration](#multi-table-configuration). + +## Changelog + +### new version + +- Add FieldRename Transform Connector diff --git a/docs/zh/transforms/field-rename.md b/docs/zh/transforms/field-rename.md index 1c81c0acca..1625abe2e6 100644 --- a/docs/zh/transforms/field-rename.md +++ b/docs/zh/transforms/field-rename.md @@ -1,151 +1,313 @@ # 字段重命名 -> FieldRename:批量重命名字段名,统一输出字段命名 +> FieldRename 转换插件 + +## 支持的引擎 + +> Spark<br/> +> Flink<br/> +> Seatunnel Zeta<br/> ## 描述 -FieldRename 转换插件用于批量重命名字段名。 +FieldRename 转换用于在不改变表名和行数据的前提下重命名表中的字段。它可以将字段名转换为大写或小写、添加前缀或后缀、按正则或精确匹配替换字段名,也可以显式重命名指定字段。主键和约束键的列会随字段一起重命名。FieldRename 支持多表配置,参见 [多表配置](#多表配置)。 + +## 主要特性 + +- 将所有字段名转换为 `UPPER` 或 `LOWER` 大小写 +- 为所有字段名添加前缀和/或后缀 +- 替换正则表达式的所有匹配,或按字段名精确匹配替换 +- 显式重命名指定字段;`specific` 规则优先于其他所有重命名选项 +- 主键和约束键的列会随字段一起重命名 +- 一个转换即可处理多张表,参见 [多表转换](transform-multi-table.md) ## 选项 -| 参数 | 类型 | 必选 | 默认值 | 说明 | -|:-----------------------:|--------|------|--------|---------------------------------------------------------------------------------------------------------| -| convert_case | enum | 否 | | 字母大小写转换类型,可选 `UPPER`、`LOWER` | -| prefix | string | 否 | | 追加到字段名前的前缀 | -| suffix | string | 否 | | 追加到字段名后的后缀 | -| replacements_with_regex | array | 否 | | 替换规则数组,元素为包含 `replace_from`、`replace_to` 以及可选 `is_regex`(默认 `true`)的映射;当 `is_regex=false` 时,`replace_from` 按字段名精确匹配(全匹配) | -| specific | array | 否 | | 指定字段重命名规则,元素为包含 `field_name` 和 `target_name` 的映射;命中后会直接重命名并跳过其他规则 | -| table_match_regex | string | 否 | `.*` | 与上游表路径进行匹配的正则表达式,转换级别规则只应用于匹配的表 | -| table_transform | array | 否 | - | 针对单表的规则,参见 [多表转换](transform-multi-table.md)。 | -| rule_match_mode | string | 否 | `FIRST_MATCH` | 当多个 `table_transform` 条目的 `table_path` 相同时,决定它们的匹配方式。 | +| 名称 | 类型 | 是否必须 | 默认值 | 描述 | +|------|------|----------|--------|------| +| convert_case | Enum | 否 | - | 字段名大小写转换。支持 `UPPER`、`LOWER`。 | +| prefix | String | 否 | - | 添加到每个字段名前的前缀。 | +| suffix | String | 否 | - | 添加到每个字段名后的后缀。 | +| replacements_with_regex | Array<Map> | 否 | - | 替换规则。每个条目包含 `replace_from`、`replace_to` 以及可选的 `is_regex`(默认 `true`)。 | +| specific | Array<Map> | 否 | - | 显式重命名规则。每个条目包含 `field_name` 和 `target_name`。匹配后优先于其他所有重命名选项。 | +| table_match_regex | String | 否 | `.*` | 转换级别规则所适用的表路径的正则表达式。 | +| table_transform | Array<Map> | 否 | - | 针对单表的规则,参见 [多表转换](transform-multi-table.md)。 | +| rule_match_mode | String | 否 | `FIRST_MATCH` | 当多个 `table_transform` 条目的 `table_path` 相同时,决定它们的匹配方式。 | +| common options | - | 否 | - | 转换插件的通用参数,参见 [Transform Plugin](common-options/common-options.md)。 | -## 示例 +### convert_case [Enum] -### 将字段名转为大写 +将每个字段名转换为大写(`UPPER`)或小写(`LOWER`)。未设置时,字段名大小写保持不变。 -``` +### prefix [String] + +为每个输出字段名添加前缀。前缀在应用 `convert_case` 和 `replacements_with_regex` 之后添加,本身不参与大小写转换。值首尾的空白会被忽略。 + +### suffix [String] + +为每个输出字段名添加后缀。后缀在应用 `convert_case` 和 `replacements_with_regex` 之后添加,本身不参与大小写转换。值首尾的空白会被忽略。 + +### replacements_with_regex [Array<Map>] + +替换规则列表。每个条目支持以下字段: + +| 字段 | 类型 | 是否必须 | 默认值 | 描述 | +|------|------|----------|--------|------| +| replace_from | String | 是 | - | Java 正则表达式;当 `is_regex` 为 `false` 时,填写完整的字段名。 | +| replace_to | String | 是 | - | 替换文本。字段名中每个 `replace_from` 的匹配都会被替换为该文本。 | +| is_regex | Boolean | 否 | `true` | 为 `true` 时,`replace_from` 按正则表达式处理;为 `false` 时,`replace_from` 必须与完整字段名匹配。 | + +字段名中每个 `replace_from` 的匹配都会被替换为 `replace_to`。匹配针对原始字段名进行,`convert_case` 不会影响哪些规则匹配。替换文本在 `convert_case` 之后拼接,本身不参与大小写转换:例如 `convert_case = "UPPER"`、`replace_to = "_x"` 时,输出中保留小写的 `_x`。如果多个规则匹配同一个字段名,使用列表中最后一个匹配的规则。`replace_to` 首尾的空白会被忽略。 + +:::caution + +每个条目都必须设置 `replace_from` 和 `replace_to`。`replace_from` 为空的条目会被忽略;如果某个条目匹配到字段但缺少 `replace_to`,作业会在初始化阶段失败。 + +::: + +### specific [Array<Map>] + +显式重命名规则列表。每个条目包含 `field_name` 和 `target_name`: + +| 字段 | 类型 | 是否必须 | 描述 | +|------|------|----------|------| +| field_name | String | 是 | 要匹配的输入字段名。比较区分大小写。 | +| target_name | String | 是 | 输出字段名。 | + +当字段匹配某条规则时,会被重命名为 `target_name`,并且不会对该字段应用 `convert_case`、`replacements_with_regex`、`prefix` 和 `suffix`。如果多个规则匹配同一个字段名,使用第一个规则。 + +### table_match_regex [String] + +与上游表路径进行匹配的正则表达式。转换级别规则(位于 `table_transform` 之外的选项)只会应用于匹配的表。默认值 `.*` 匹配所有表。既不匹配该正则表达式、也没有 `table_transform` 条目的表,将保持不变地传递下去。 + +注意,这里匹配的是真实的上游表名,而不是 `plugin_output`。 + +### table_transform [Array<Map>] + +针对单表的规则列表。每个条目必须包含 `table_path`(格式为 `databaseName[.schemaName].tableName`,精确匹配),以及要应用到该表的 FieldRename 选项。 + +当某张表在 `table_transform` 中有对应条目时,转换级别规则不会应用于该表。参见 [多表配置](#多表配置)。 + +### rule_match_mode [String] + +控制当多个 `table_transform` 条目的 `table_path` 相同时的处理方式。支持的值为 `FIRST_MATCH`(只应用第一个匹配的条目)和 `ALL_MATCH`(按顺序应用所有匹配的条目,每个条目接收前一个条目的输出)。 + +如果未设置 `rule_match_mode`,重复的 `table_path` 条目会被拒绝。当多个条目使用相同的 `table_path` 时,请显式设置为 `FIRST_MATCH` 或 `ALL_MATCH`。 + +### common options + +转换插件的通用参数,请参考 [Transform Plugin](common-options/common-options.md) 了解详情。 + +## 多表配置 + +当上游数据源输出多张表时(例如 JDBC 或 MySQL-CDC),一个 FieldRename 转换就可以处理所有这些表。 + +- `table_transform` 中的条目优先于转换级别规则。有对应条目的表不会再受转换级别规则影响。 +- 每个条目必须包含 `table_path` 以及该表的 FieldRename 选项。 +- 转换级别规则只应用于匹配 `table_match_regex` 的表。 + +下面的配置对 `test.a.*` 表使用转换级别规则,对 `test.xyz` 使用单表规则,并保持 `test.www` 不变: + +```hocon env { - parallelism = 1 - job.mode = "STREAMING" + parallelism = 1 + job.mode = "BATCH" } source { - MySQL-CDC { - plugin_output = "customers_mysql_cdc" - - username = "root" - password = "123456" - table-names = ["source.user_shop", "source.user_order"] - url = "jdbc:mysql://localhost:3306/source" - } + FakeSource { + plugin_output = "fake" + tables_configs = [ + { + row.num = 2 + schema = { + table = "test.abc" + fields { id = int, name = string, age = int } + } + }, + { + row.num = 2 + schema = { + table = "test.abcd" + fields { id = int, name = string, age = int } + } + }, + { + row.num = 2 + schema = { + table = "test.xyz" + fields { id = int, name = string, age = int } + } + }, + { + row.num = 2 + schema = { + table = "test.www" + fields { id = int, name = string, age = int } + } + } + ] + } } transform { FieldRename { - plugin_input = "customers_mysql_cdc" - plugin_output = "trans_result" - + plugin_input = "fake" + plugin_output = "fake1" + + table_match_regex = "test.a.*" convert_case = "UPPER" prefix = "F_" - suffix = "_S" - replacements_with_regex = [ + + table_transform = [ { - replace_from = "create_time" - replace_to = "SOURCE_CREATE_TIME" + table_path = "test.xyz" + convert_case = "LOWER" + suffix = "_S" } ] } } sink { - Jdbc { - plugin_input = "trans_result" - - driver="oracle.jdbc.OracleDriver" - url="jdbc:oracle:thin:@oracle-host:1521/ORCLCDB" - user="myuser" - password="mypwd" - - generate_sink_sql = true - database = "ORCLCDB" - table = "${database_name}.${table_name}" - primary_keys = ["${primary_key}"] - - schema_save_mode = "CREATE_SCHEMA_WHEN_NOT_EXIST" - data_save_mode = "APPEND_DATA" + Console { + plugin_input = "fake1" } } ``` -### 指定字段重命名 +每张表的结果结构: -``` -transform { - FieldRename { - plugin_input = "input" - plugin_output = "output" +| 表 | 应用的规则 | 输出字段 | +|----|------------|----------| +| test.abc | 转换级别规则(匹配 `test.a.*`) | F_ID, F_NAME, F_AGE | +| test.abcd | 转换级别规则(匹配 `test.a.*`) | F_ID, F_NAME, F_AGE | +| test.xyz | `table_transform` 条目 | id_S, name_S, age_S | +| test.www | 无 | id, name, age | - specific = [ - { field_name = "InvoiceNum", target_name = "invoice_num" } - ] - } -} -``` +## 示例 -### 将字段名转为小写 +### 单表 -``` +源端读取的数据是这样的一张表: + +| InvoiceNum | VendorID | +|------------|----------| +| 1001 | Vendor A | +| 1002 | Vendor B | + +将驼峰式字段名转换为下划线式: + +```hocon env { - parallelism = 1 - job.mode = "STREAMING" + parallelism = 1 + job.mode = "BATCH" } source { - Oracle-CDC { - plugin_output = "customers_oracle_cdc" - - url = "jdbc:oracle:thin:@localhost:1521/ORCLCDB" - username = "dbzuser" - password = "dbz" - database-names = ["ORCLCDB"] - schema-names = ["DEBEZIUM"] - table-names = ["SOURCE.USER_SHOP", "SOURCE.USER_ORDER"] + FakeSource { + plugin_output = "fake" + schema = { + columns = [ + { name = "InvoiceNum", type = "bigint" }, + { name = "VendorID", type = "string" } + ] + } + rows = [ + { kind = INSERT, fields = [1001, "Vendor A"] }, + { kind = INSERT, fields = [1002, "Vendor B"] } + ] } } transform { FieldRename { - plugin_input = "customers_oracle_cdc" - plugin_output = "trans_result" - + plugin_input = "fake" + plugin_output = "fake1" + convert_case = "LOWER" - prefix = "f_" - suffix = "_s" replacements_with_regex = [ { - replace_from = "CREATE_TIME" - replace_to = "source_create_time" + replace_from = "(?<=[a-z0-9])(?=[A-Z])" + replace_to = "_" } ] } } sink { - Jdbc { - plugin_input = "trans_result" - - url = "jdbc:mysql://localhost:3306/test" - driver = "com.mysql.cj.jdbc.Driver" - user = "st_user_sink" - password = "mysqlpw" - - generate_sink_sql = true - database = "${schema_name}" - table = "${table_name}" - primary_keys = ["${primary_key}"] - - schema_save_mode = "CREATE_SCHEMA_WHEN_NOT_EXIST" - data_save_mode = "APPEND_DATA" + Console { + plugin_input = "fake1" + } +} +``` + +结果表 `fake1` 中的数据如下: + +| invoice_num | vendor_id | +|-------------|-----------| +| 1001 | Vendor A | +| 1002 | Vendor B | + +该正则匹配小写字母或数字与大写字母之间的位置。下划线会插入到该位置,因此 `InvoiceNum` 变为 `invoice_num`,`VendorID` 变为 `vendor_id`。 + +### 添加前缀和后缀 + +```hocon +transform { + FieldRename { + plugin_input = "fake" + plugin_output = "fake1" + prefix = "f_" + suffix = "_s" + } +} +``` + +字段名会变为 `f_InvoiceNum_s` 和 `f_VendorID_s`。 + +### 重命名指定字段 + +```hocon +transform { + FieldRename { + plugin_input = "fake" + plugin_output = "fake1" + specific = [ + { field_name = "InvoiceNum", target_name = "invoice_number" } + ] + } +} +``` + +`InvoiceNum` 变为 `invoice_number`,`VendorID` 保持不变。由于 `specific` 优先,匹配字段的 `convert_case`、`replacements_with_regex`、`prefix` 和 `suffix` 都会被忽略。 + +### 按精确匹配替换字段名 + +```hocon +transform { + FieldRename { + plugin_input = "fake" + plugin_output = "fake1" + replacements_with_regex = [ + { + replace_from = "VendorID" + replace_to = "vendor_id" + is_regex = false + } + ] } } ``` + +`VendorID` 变为 `vendor_id`,`InvoiceNum` 保持不变。当 `is_regex = false` 时,只有完整字段名等于 `replace_from` 的字段才会被替换。 + +### 多表 + +参见 [多表配置](#多表配置)。 + +## 更新日志 + +### 新版本 + +- 添加字段重命名转换连接器
