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-13270-e653ba556c00e18fae6848243049253c4988ae95 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit a76348f1f98953444fd030fbbc56209d8ad40d18 Author: niumy <[email protected]> AuthorDate: Sun Nov 22 12:51:36 2026 +0000 [Docs][Transform-V2] Rewrite TextChunk documentation (#13270) Co-authored-by: muyue niu <[email protected]> --- docs/en/transforms/text-chunk.md | 369 ++++++++++++++++++++++++++++----------- docs/zh/transforms/text-chunk.md | 348 +++++++++++++++++++++++++++--------- 2 files changed, 528 insertions(+), 189 deletions(-) diff --git a/docs/en/transforms/text-chunk.md b/docs/en/transforms/text-chunk.md index 9aacf265664..fc0e8304205 100644 --- a/docs/en/transforms/text-chunk.md +++ b/docs/en/transforms/text-chunk.md @@ -1,178 +1,337 @@ # TextChunk -> TextChunk transform plugin +## Support Those Engines + +> SeaTunnel Zeta<br/> +> Flink<br/> +> Spark ## Description -Splits a long text field into multiple smaller chunks, emitting **one row per chunk** (a 1-row → N-rows -transform). Each output row keeps **all of the original source fields** unchanged, and appends two columns: the -chunk text and its 0-based sequence index within the source document. +TextChunk splits a long text field into multiple smaller chunks and emits **one row per chunk** (a 1-row → N-rows transform). Each output row keeps **all of the original source fields** unchanged — except a column explicitly reused as the chunk output or index column, whose values are overwritten with the per-chunk value — and carries the chunk text and its 0-based sequence index within the source document, in appended or reused columns. Use it to prepare long documents for consumers tha [...] + +The chunk column is a STRING column and the index column is an INT column. Each of them is appended at the end of the schema, or reused when a column with the same name already exists. All chunks of one input row share the same source key, so the transform extends the primary key and every unique key with the chunk index column to keep every chunk unique for key-aware sinks. ## Options -| name | type | required | default value | -|-------------------|--------|----------|---------------| -| text_field | string | yes | | -| output_field | string | no | chunk | -| chunk_index_field | string | no | chunk_index | -| chunk_size | int | no | 1000 | -| overlap_size | int | no | 0 | -| separators | array | no | ["\n\n", "\n", "。", "!", "?", ". ", " "] | -| skip_empty_text | boolean| no | true | -| table_match_regex | string | no | `.*` | -| table_transform | array | no | - | +| Name | Type | Required | Default | Description | +|------|------|----------|---------|-------------| +| text_field | String | Conditional | - | The source text field to split into chunks. Required by the transform-level rule and by every `table_transform` entry. | +| output_field | String | No | `chunk` | Output column holding each chunk (STRING). Must not match a primary/unique key column and must differ from `chunk_index_field`. | +| chunk_index_field | String | No | `chunk_index` | Output column holding the 0-based chunk index within the document (INT). Same restrictions as `output_field`. | +| chunk_size | Int | No | `1000` | Maximum chunk length in Unicode code points. Must be greater than `0`. | +| overlap_size | Int | No | `0` | Overlap carried into the start of the next chunk, in Unicode code points. Must satisfy `0 <= overlap_size < chunk_size`. | +| separators | Array<String> | No | `["\n\n", "\n", "。", "!", "?", ". ", " "]` | Separators tried in priority order to avoid cutting mid-sentence. An empty list falls back to fixed-size splitting. | +| skip_empty_text | Boolean | No | `true` | Drop rows whose text is null or empty instead of passing them through. | +| table_match_regex | String | No | `.*` | Match upstream table paths to which the transform-level rule applies. | +| table_transform | Array<Map> | No | `[]` | Complete rules for individual tables, selected by an exact `table_path`. | +| rule_match_mode | Enum | No | `FIRST_MATCH` | Select the first matching per-table rule, or apply all matching rules in order. See the duplicate-path behavior below. | +| common options | - | No | - | Common transform options such as `plugin_input` and `plugin_output`. | -### text_field [string] +### text_field [String] -The source text field to split into chunks. +The source text field to split into chunks. The field must exist in the input schema; otherwise the transform fails with a field-not-found error. -### output_field [string] +Without a nonempty `table_transform` list, `text_field` is required at the transform level. An empty `table_transform = []` does not remove this requirement. -The name of the output column holding each chunk (type `STRING`). If a column with this name already -exists it is reused, otherwise a new column is appended. Defaults to `chunk`, but **must not** match a -column that takes part in a primary key or a unique key. +With per-table entries, the transform-level rule may be absent. The presence of `text_field` marks the transform-level rule as configured: when it is not set at the transform level, the other transform-level options have no effect and only the `table_transform` entries apply. Each entry must set its own `text_field` and is validated on its own at submission time with default values applied. -### chunk_index_field [string] +### output_field [String] -The name of the output column holding the chunk sequence index within a document (type `INT`, -0-based). If a column with this name already exists it is reused, otherwise a new column is appended. -Defaults to `chunk_index`, but **must not** match a column that takes part in a primary key or a -unique key. +The name of the output column holding each chunk (type STRING). If a column with this name already exists it is reused (its type is replaced with STRING), otherwise a nullable column is appended at the end of the schema. Defaults to `chunk`. -### chunk_size [int] +The name must not match a column that takes part in a primary key or a unique key: the transform rejects such a configuration with an error, because writing per-chunk values into a key column would let chunks from different source rows collide in key-aware sinks. Reusing a non-key column (for example writing the chunk back into the source text column) is allowed. The name must also differ from `chunk_index_field`. -Maximum length of each chunk, counted in **Unicode code points** — a supplementary character such as -an emoji counts as one. (A combining sequence or a ZWJ-joined emoji still counts as several code -points and may be split between them.) Must be greater than `0`. Defaults to `1000`. +### chunk_index_field [String] -### overlap_size [int] +The name of the output column holding the chunk sequence index within a document (type INT, 0-based). The index value is always populated in every output row. A newly appended index column is declared NOT NULL; when an existing column is reused, its type is replaced with INT and its original nullable metadata is kept. Defaults to `chunk_index`. -Overlap length between adjacent chunks, counted in **Unicode code points**. -Carrying a little context from the end -of one chunk into the start of the next helps preserve meaning across chunk boundaries. The overlap is -made up of **whole trailing pieces** (complete words/sentences), so it never begins in the middle of a -word; `overlap_size` is therefore an upper bound — the actual overlap rounds down to whole pieces, and -is empty when even the last single piece is longer than the budget. (In the empty-`separators` -fixed-size fallback there are no pieces, so the overlap is a plain `overlap_size`-code-point window and -is not rounded to a word boundary.) Must satisfy `0 <= overlap_size < chunk_size`. Defaults to `0`. +The same key-column restriction as `output_field` applies, and the name must differ from `output_field`. -> Note: the number of chunks grows roughly as `chunk_size / (chunk_size - overlap_size)`, so an -> `overlap_size` close to `chunk_size` multiplies the output row count (and memory) dramatically — -> e.g. `chunk_size = 1000` with `overlap_size = 999` produces about one chunk per code point. Keep the -> overlap well below `chunk_size`. +The rows produced from one input row all carry the same source key, so the transform appends this column to the primary key and to every unique key of the produced table (unless the key is auto-generated or already contains the column). This keeps each chunk unique for key-aware sinks. -### separators [array] +### chunk_size [Int] -Separators tried in priority order to avoid cutting mid-sentence: the text is split by the first -separator, and any piece still longer than `chunk_size` is further split by the next separator, and so -on. If the list is left empty, the transform falls back to fixed-size splitting. Defaults to -`["\n\n", "\n", "。", "!", "?", ". ", " "]`. +Maximum length of each chunk, counted in **Unicode code points** — a supplementary character such as an emoji counts as one. (A combining sequence or a ZWJ-joined emoji still counts as several code points and may be split between them.) Must be greater than `0`. Defaults to `1000`. -### skip_empty_text [boolean] +Each produced chunk is at most `chunk_size` code points long and the carried overlap counts toward that budget, so a surrogate pair (e.g. an emoji) is never split in half. -Controls how a row whose `text_field` is `null` or empty is handled. When `true` the row is dropped; -when `false` the row is passed through (`output_field` set to `null`, `chunk_index_field` set to `0`). -Defaults to `true`. +### overlap_size [Int] -### table_match_regex [string] +Overlap length between adjacent chunks, counted in **Unicode code points**. Carrying a little context from the end of one chunk into the start of the next helps preserve meaning across chunk boundaries. The overlap is made up of **whole trailing pieces** (complete words/sentences), so it never begins in the middle of a word; `overlap_size` is therefore an upper bound — the actual overlap rounds down to whole pieces, and is empty when even the last single piece is longer than the budget. [...] -Regex matched against the upstream table path. The transform level rule only applies to matching -tables. Defaults to `.*` (every table). +:::caution +The number of chunks grows roughly as `chunk_size / (chunk_size - overlap_size)`, so an `overlap_size` close to `chunk_size` multiplies the output row count (and memory) dramatically — for example `chunk_size = 1000` with `overlap_size = 999` produces about one chunk per code point. The transform logs a runtime warning when `overlap_size` reaches half of `chunk_size`. Keep the overlap well below `chunk_size`. +::: -### table_transform [array] +### separators [Array<String>] -A list of per-table rules, used when the upstream source outputs several tables. Each entry contains -`table_path` in the form `databaseName[.schemaName].tableName` plus a complete TextChunk rule for -that table (`text_field` and the optional chunking options). Tables with an entry are not affected -by the transform level rule. See [Multi-Table Transform](transform-multi-table.md). +Separators tried in priority order to avoid cutting mid-sentence: the text is split by the first separator, and any piece still longer than `chunk_size` is further split by the next separator, and so on. If every separator has been tried and a piece is still longer than `chunk_size`, that piece is split into fixed-size windows, which then take part in the normal merge and overlap step like any other piece. If the list is left empty, the whole text falls back to fixed-size splitting. Defa [...] -### common options [string] +Separators are matched literally (not as regular expressions) and are retained: each separator stays attached to the end of the piece it follows, so merging pieces back into a chunk keeps the spaces and newlines between them (words split on `" "` are re-joined as `a b`, not `ab`), and runs of consecutive separators (e.g. blank lines) are preserved. With `overlap_size = 0` the chunks concatenate back to the original text. -Transform plugin common parameters, please refer to [Transform Plugin](common-options/common-options.md) for details. +### skip_empty_text [Boolean] -## Behavior +Controls how a row whose `text_field` is `null` or empty is handled. When `true` (the default) the row produces no output rows; when `false` the row is passed through with `output_field` set to `null` and `chunk_index_field` set to `0`. -- A `null` or empty text value produces **no** output rows for that input row when `skip_empty_text = true` (the default); set `skip_empty_text = false` to pass such a row through instead (`chunk = null`, `chunk_index = 0`). -- Each produced chunk is at most `chunk_size` Unicode code points long (the carried overlap counts toward the budget); a surrogate pair (e.g. an emoji) counts as one code point and is never split. -- Separators are retained: each separator stays attached to the end of the piece it follows, so merging pieces to fill a chunk keeps the spaces/newlines between them (words split on `" "` are re-joined as `a b`, not `ab`) and runs of consecutive separators (e.g. blank lines) are preserved. With `overlap_size = 0` the chunks concatenate back to the original text. -- The rows produced from one input row all carry the same source key (e.g. the same `id`). To keep it unique, the transform appends `chunk_index_field` to the primary key and to every unique key. -- Because of that key extension, `output_field` and `chunk_index_field` must not reuse a column that is part of a primary or unique key: overwriting such a key column with per-chunk values would let chunks from different source rows collide in key-aware sinks, so the transform rejects that configuration with an error. Reusing a non-key column (e.g. writing the chunk back into the source text column) is still allowed. +### table_match_regex [String] -## Example +A Java regular expression matched against the whole upstream table path, such as `test.abc` or `database.schema.table`. The default `.*` matches all table paths. This is the actual table path, not the dataset name in `plugin_input` or `plugin_output`. -The data read from source is a table like this: +This option selects the tables to which the transform-level rule applies. It does not restrict explicit `table_transform` entries, and without a transform-level rule (see `text_field`) it does not create a chunking rule on its own. -| id | content | -|----|-----------------------------------------------| -| 1 | SeaTunnel is a data integration platform. ... | +### table_transform [Array<Map>] -We want to split the `content` field into overlapping chunks, so we can add a `TextChunk` transform -like this: +A list of per-table rules; the default is an empty list. Each entry must have a nonblank `table_path` (an exact path, not a regular expression) in the form `databaseName[.schemaName].tableName`, plus a complete single-table TextChunk rule for that table: `text_field` and any optional chunking options. + +An entry is a complete rule and does not inherit the transform-level options. A matching entry takes precedence even when its table does not match `table_match_regex`. Every entry is validated on its own at submission time with default values applied, so it must satisfy `chunk_size > 0` and `0 <= overlap_size < chunk_size` even when it relies on the defaults. + +### rule_match_mode [Enum] + +Controls multiple entries with the same exact `table_path`: + +- Explicit `FIRST_MATCH`: use the first matching entry in list order. +- Explicit `ALL_MATCH`: apply all matching entries in list order. TextChunk is a 1-row-to-N-rows transform, so the rows produced by an earlier matching entry become the input of the next matching entry for the same table. + +Although the option's default is `FIRST_MATCH`, omitting the option rejects duplicate `table_path` entries during transform initialization. Set it explicitly if repeated paths are intentional. With unique paths, the default is sufficient. + +### common options + +Use `plugin_input` and `plugin_output` to connect this transform to upstream and downstream plugins. See [Transform Plugin](common-options/common-options.md) for common parameters. + +## Multi-Table Configuration + +For each input table, the effective rule is selected in this order: + +1. Use the matching `table_transform` entry or entries, according to `rule_match_mode`. +2. Otherwise, apply the transform-level rule if one is configured (see `text_field`) and the table path matches `table_match_regex`. +3. Otherwise, pass the table's rows through unchanged. + +In a `table_transform`-only configuration, unlisted tables pass through unchanged. A table that passes through unchanged also keeps its original schema: the chunk and chunk index columns are added only by the tables that actually get a chunking rule. + +The following examples cover all three configuration shapes. See [Multi-Table Transform](transform-multi-table.md) for the shared matching rules. + +## Examples + +These complete batch configurations use FakeSource and Console. Install those connectors in your SeaTunnel distribution before running a saved configuration with `bin/seatunnel.sh --config <file> -e local`. The rows are fixed by the `rows` option of FakeSource, so the expected outputs are deterministic. + +### Transform-level rule only + +Input: one row in table `test.doc`, with a 59-code-point text. With `separators = [" "]`, `chunk_size = 20` and `overlap_size = 6`, the text is split into space-separated pieces that are greedily merged into chunks of at most 20 code points, carrying at most 6 code points of whole trailing pieces into the next chunk: + +| id | content | chunk | chunk_index | +|----|---------|-------|-------------| +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `SeaTunnel is fast. ` | 0 | +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `fast. SeaTunnel is ` | 1 | +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `is reliable. ` | 2 | +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `SeaTunnel scales.` | 3 | + +The chunks retain their trailing separators, so `chunk 0` ends with the piece `fast. ` that is carried into `chunk 1`, and `chunk 1` ends with `is ` that is carried into `chunk 2` (`is reliable. `). The last chunk is `SeaTunnel scales.`. + +```hocon +env { + job.mode = "BATCH" + parallelism = 1 +} + +source { + FakeSource { + plugin_output = "fake" + schema { + table = "test.doc" + fields { + id = "int" + content = "string" + } + } + rows = [ + { fields = [1, "SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales."], kind = INSERT } + ] + } +} -``` transform { TextChunk { plugin_input = "fake" - plugin_output = "fake1" + plugin_output = "chunked" text_field = "content" - output_field = "chunk" - chunk_index_field = "chunk_index" - chunk_size = 200 - overlap_size = 20 + chunk_size = 20 + overlap_size = 6 + separators = [" "] + } +} + +sink { + Console { + plugin_input = "chunked" } } ``` -Then each input row is expanded into one row per chunk in result table `fake1`, keeping the original -fields and adding `chunk` / `chunk_index`: +### Transform-level rule with per-table rules -| id | content | chunk | chunk_index | -|----|-----------------------------------------------|------------------------|-------------| -| 1 | SeaTunnel is a data integration platform. ... | SeaTunnel is a data... | 0 | -| 1 | SeaTunnel is a data integration platform. ... | ...platform that ... | 1 | +Each input table contains one row. The explicit entry for `test.doc_a` overrides the transform-level rule. `test.doc_c` does not match `table_match_regex` and passes through unchanged. -## Job Config Example +| Table | Input rows | Output rows | Effective rule | +|-------|------------|-------------|----------------| +| `test.doc_a` | 1 | 2 | Per-table entry: `chunk_size = 2`, `separators = []`. | +| `test.doc_b` | 1 | 3 | Transform-level `chunk_size = 3` with the default separators. | +| `test.doc_c` | 1 | 1 | No matching rule; unchanged, without chunk columns. | -``` +```hocon env { - parallelism = 1 job.mode = "BATCH" + parallelism = 1 } source { FakeSource { plugin_output = "fake" - row.num = 5 - schema = { - fields { - id = "int" - content = "string" + tables_configs = [ + { + schema { + table = "test.doc_a" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [1, "xxxx"], kind = INSERT } + ] + }, + { + schema { + table = "test.doc_b" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [2, "yy yy yy "], kind = INSERT } + ] + }, + { + schema { + table = "test.doc_c" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [3, "kept as is"], kind = INSERT } + ] } - } + ] } } transform { TextChunk { plugin_input = "fake" - plugin_output = "fake1" + plugin_output = "chunked" text_field = "content" - output_field = "chunk" - chunk_index_field = "chunk_index" - chunk_size = 200 - overlap_size = 20 + chunk_size = 3 + overlap_size = 0 + table_match_regex = "test[.](doc_a|doc_b)" + table_transform = [ + { + table_path = "test.doc_a" + text_field = "content" + chunk_size = 2 + overlap_size = 0 + separators = [] + } + ] } } sink { Console { - plugin_input = "fake1" + plugin_input = "chunked" } } ``` -## Changelog +`test.doc_a` is chunked by its own entry into `xx` / `xx` (2 rows). `test.doc_b` falls back to the transform-level rule: the default separators split `yy yy yy ` into three pieces of 3 code points each, producing `yy ` / `yy ` / `yy ` (3 rows). `test.doc_c` has neither an entry nor a matching transform-level rule, so its single row passes through unchanged. + +### Per-table rules only + +There is no transform-level rule: `text_field` is not set at the transform level and the other transform-level options have no effect. Only the two listed tables are chunked; any other table would pass through unchanged. + +| Table | Input rows | Output rows | Effective rule | +|-------|------------|-------------|----------------| +| `test.doc_a` | 1 | 2 | Entry: `chunk_size = 2`, `separators = []`. | +| `test.doc_b` | 1 | 3 | Entry: `chunk_size = 3`, `separators = []`. | + +```hocon +env { + job.mode = "BATCH" + parallelism = 1 +} + +source { + FakeSource { + plugin_output = "fake" + tables_configs = [ + { + schema { + table = "test.doc_a" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [1, "xxxx"], kind = INSERT } + ] + }, + { + schema { + table = "test.doc_b" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [2, "yy yy yy "], kind = INSERT } + ] + } + ] + } +} -### new version +transform { + TextChunk { + plugin_input = "fake" + plugin_output = "chunked" + table_transform = [ + { + table_path = "test.doc_a" + text_field = "content" + chunk_size = 2 + overlap_size = 0 + separators = [] + }, + { + table_path = "test.doc_b" + text_field = "content" + chunk_size = 3 + overlap_size = 0 + separators = [] + } + ] + } +} + +sink { + Console { + plugin_input = "chunked" + } +} +``` -- Add TextChunk Transform Plugin +`test.doc_a` produces `xx` / `xx` (2 rows) and `test.doc_b` produces the fixed-size windows `yy ` / `yy ` / `yy ` (3 rows). diff --git a/docs/zh/transforms/text-chunk.md b/docs/zh/transforms/text-chunk.md index ce206396d24..1b3bb6b4511 100644 --- a/docs/zh/transforms/text-chunk.md +++ b/docs/zh/transforms/text-chunk.md @@ -1,157 +1,337 @@ -# TextChunk +# 文本分块 -> 文本分块转换插件 +## 支持的引擎 + +> SeaTunnel Zeta<br/> +> Flink<br/> +> Spark ## 描述 -将一个长文本字段切分成多个较小的块(chunk),**每个块输出为一行**(1 行 → N 行的转换)。 每个输出行都会**原样保留全部源字段**,并追加两列:块文本,以及该块在源文档内的序号(从 0 开始)。 +TextChunk(文本分块)将一个长文本字段切分成多个较小的块(chunk),**每个块输出为一行**(1 行 → N 行的转换)。每个输出行都会**原样保留全部源字段**——被显式复用为块输出列或序号列的列除外(其值会被每个块覆盖)——并以追加或复用的列携带块文本以及该块在源文档内的 0 起始序号。适合为按有限长度文本单元处理文档的下游场景做准备,例如按固定大小段落构建 embedding 的流水线。 + +块列为 STRING 类型,序号列为 INT 类型;两者都会追加到 schema 末尾,若已存在同名列则复用该列。同一输入行产生的所有块共享相同的源键,因此转换会把块序号列追加到主键和每一个唯一键上,保证对依赖键的 sink 而言每个块仍然唯一。 ## 属性 -| 名称 | 类型 | 是否必须 | 默认值 | -|-------------------|--------|------|-----| -| text_field | string | yes | | -| output_field | string | no | chunk | -| chunk_index_field | string | no | chunk_index | -| chunk_size | int | no | 1000 | -| overlap_size | int | no | 0 | -| separators | array | no | ["\n\n", "\n", "。", "!", "?", ". ", " "] | -| skip_empty_text | boolean| no | true | -| table_match_regex | string | no | `.*` | -| table_transform | array | no | - | +| 名称 | 类型 | 是否必须 | 默认值 | 描述 | +|------|------|----------|--------|------| +| text_field | String | 条件必需 | - | 需要切分成块的源文本字段。转换级规则和每个 `table_transform` 条目都必须设置。 | +| output_field | String | 否 | `chunk` | 存放每个块的输出列(STRING)。不能与主键/唯一键包含的列同名,且必须与 `chunk_index_field` 不同。 | +| chunk_index_field | String | 否 | `chunk_index` | 存放块在文档内 0 起始序号的输出列(INT)。限制与 `output_field` 相同。 | +| chunk_size | Int | 否 | `1000` | 每个块的最大长度,以 Unicode 码点计。必须大于 `0`。 | +| overlap_size | Int | 否 | `0` | 相邻块之间携带到下一块开头的重叠长度,以 Unicode 码点计。必须满足 `0 <= overlap_size < chunk_size`。 | +| separators | Array<String> | 否 | `["\n\n", "\n", "。", "!", "?", ". ", " "]` | 按优先级顺序尝试的分隔符,用于避免从句子中间切断。留空时回退为固定长度切分。 | +| skip_empty_text | Boolean | 否 | `true` | 文本为 null 或空字符串的行直接丢弃,而不是透传。 | +| table_match_regex | String | 否 | `.*` | 匹配要应用转换级规则的上游表路径。 | +| table_transform | Array<Map> | 否 | `[]` | 通过精确的 `table_path` 为各表指定完整规则。 | +| rule_match_mode | Enum | 否 | `FIRST_MATCH` | 选择第一条匹配的表级规则,或按顺序执行所有匹配的规则。重复路径的行为见下文。 | +| common options | - | 否 | - | `plugin_input`、`plugin_output` 等转换插件通用参数。 | + +### text_field [String] + +需要切分成块的源文本字段。该字段必须存在于输入 schema 中,否则转换会以"找不到字段"错误失败。 + +没有非空 `table_transform` 列表时,转换级必须设置 `text_field`。`table_transform = []` 不能免除这个要求。 + +配置了表级条目时,可以不设置转换级规则。转换级是否配置了规则以 `text_field` 是否存在为准:转换级未设置 `text_field` 时,其他转换级选项不产生任何作用,只有 `table_transform` 条目生效。每个条目必须设置自己的 `text_field`,并且在作业提交时会按应用默认值后的配置独立校验。 + +### output_field [String] + +存放每个块的输出列名(类型 STRING)。若已存在同名列则复用该列(类型替换为 STRING),否则在 schema 末尾追加一个可空列。默认值为 `chunk`。 + +该列名不能与主键或唯一键所包含的列同名:转换会直接报错拒绝此类配置,因为用每块的值覆盖键列会导致不同源行的块在依赖键的 sink 中碰撞。复用非键列(例如把块写回源文本列)仍然允许。该列名还必须与 `chunk_index_field` 不同。 + +### chunk_index_field [String] -### text_field [string] +存放块在文档内序号的输出列名(类型 INT,从 0 开始)。序号值在每行输出中总会被写入:新追加的序号列声明为 NOT NULL;若复用已有列,则类型替换为 INT,并保留该列原有的可空元数据。默认值为 `chunk_index`。 -需要切分成块的源文本字段。 +该列名受与 `output_field` 相同的键列限制,且必须与 `output_field` 不同。 -### output_field [string] +同一输入行产生的多行都带有相同的源键,因此转换会把该列追加到输出表的主键和每一个唯一键上(键为自增主键或已包含该列时除外)。这保证对依赖键的 sink 而言每个块仍然唯一。 -存放每个块的输出列名(类型 `STRING`)。若已存在同名列则复用该列,否则追加新列。默认值为 `chunk`,但**不能**与主键或唯一键所包含的列同名。 +### chunk_size [Int] -### chunk_index_field [string] +每个块的最大长度,以 **Unicode 码点(code point)** 计——一个增补字符(如 emoji)计为一个码点。(组合字符或 ZWJ 连字 emoji 仍算作多个码点,可能在码点之间被切分。)必须大于 `0`。默认值为 `1000`。 -存放块在文档内序号的输出列名(类型 `INT`,从 0 开始)。若已存在同名列则复用该列,否则追加新列。默认值为 `chunk_index`,但**不能**与主键或唯一键所包含的列同名。 +每个产生的块长度不超过 `chunk_size` 个码点,携带的 overlap 也计入该上限,因此代理对(例如 emoji)不会被劈开。 -### chunk_size [int] +### overlap_size [Int] -每个块的最大长度,以 **Unicode 码点(code point)** 计——一个增补字符(如 emoji)计为一个码点。(组合字符或 ZWJ 连字 emoji 仍算作 -多个码点,可能在码点之间被切分。)必须大于 `0`。默认值为 `1000`。 +相邻块之间的重叠长度,以 **Unicode 码点(code point)** 计。把前一个块末尾的少量上下文带入下一个块开头,有助于在块边界处保留语义。重叠由**若干个完整的片段**(完整的词/句)组成,因此绝不会从单词中间开始;`overlap_size` 是一个上界——实际重叠会向下取整到完整片段,当连最后一个片段都超过该预算时则不携带任何重叠。(当 `separators` 留空、回退为固定长度切分时不存在"片段",此时重叠为 `overlap_size` 个码点的定长窗口,不会向词边界取整。)必须满足 `0 <= overlap_size < chunk_size`。默认值为 `0`。 -### overlap_size [int] +:::caution +块的数量大致按 `chunk_size / (chunk_size - overlap_size)` 增长,因此当 `overlap_size` 接近 `chunk_size` 时,输出行数(以及内存占用)会急剧膨胀——例如 `chunk_size = 1000` 且 `overlap_size = 999` 时,几乎每个码点都会产生一个块。当 `overlap_size` 达到 `chunk_size` 的一半时,转换会在运行时输出告警日志。请让 overlap 明显小于 `chunk_size`。 +::: -相邻块之间的重叠长度,以 **Unicode 码点(code point)** 计。把前一个块末尾的少量上下文带入下一个块开头,有助于在块边界处保留语义。重叠由 -**若干个完整的片段**(完整的词/句)组成,因此绝不会从单词中间开始;`overlap_size` 是一个上界——实际重叠会向下取整到完整片段, -当连最后一个片段都超过该预算时,则不携带任何重叠。(当 `separators` 留空、回退为固定长度切分时不存在"片段",此时重叠为 -`overlap_size` 个码点的定长窗口,不会向词边界取整。)必须满足 `0 <= overlap_size < chunk_size`。默认值为 `0`。 +### separators [Array<String>] -> 注意:块的数量大致按 `chunk_size / (chunk_size - overlap_size)` 增长,因此当 `overlap_size` 接近 -> `chunk_size` 时,输出行数(以及内存占用)会急剧膨胀——例如 `chunk_size = 1000` 且 `overlap_size = 999` -> 时,几乎每个码点都会产生一个块。请让 overlap 明显小于 `chunk_size`。 +按优先级顺序尝试的分隔符,用于避免从句子中间切断:先用第一个分隔符切分,任何仍长于 `chunk_size` 的片段再用下一个分隔符继续切分,依此类推。当所有分隔符都已尝试而某个片段仍长于 `chunk_size` 时,该片段会被切成定长窗口,这些窗口随后与普通片段一样参与正常的合并与重叠步骤。若该列表留空,则整段文本回退为固定长度切分。默认值为 `["\n\n", "\n", "。", "!", "?", ". ", " "]`。 -### separators [array] +分隔符按字面量匹配(不是正则表达式)且会被保留:每个分隔符附着在它前面那一片的末尾,因此合并多片以填满一个块时,片与片之间的空格/换行会被保留(按 `" "` 切分的单词会重新拼成 `a b` 而不是 `ab`),连续的分隔符(例如空行)也会被保留。当 `overlap_size = 0` 时,各块拼接后可还原为原始文本。 -按优先级顺序尝试的分隔符,用于避免从句子中间切断:先用第一个分隔符切分,任何仍长于 `chunk_size` 的片段再用 -下一个分隔符继续切分,依此类推。若该列表留空,则回退为固定长度切分。默认值为 -`["\n\n", "\n", "。", "!", "?", ". ", " "]`。 +### skip_empty_text [Boolean] -### skip_empty_text [boolean] +控制 `text_field` 为 `null` 或空字符串时该行的处理方式。为 `true`(默认)时该行不产生任何输出行;为 `false` 时该行被透传(`output_field` 置为 `null`、`chunk_index_field` 置为 `0`)。 -控制 `text_field` 为 `null` 或空字符串时的行处理逻辑。为 `true` 时该行被丢弃,为 `false` 时该行透传 -(`output_field` 置为 `null`、`chunk_index_field` 置为 `0`),默认值为 `true`。 +### table_match_regex [String] -### table_match_regex [string] +针对完整上游表路径匹配的 Java 正则表达式,例如 `test.abc` 或 `database.schema.table`。默认值 `.*` 匹配所有表路径。这里匹配的是实际表路径,不是 `plugin_input` 或 `plugin_output` 中的数据集名称。 -与上游表路径进行匹配的正则表达式,转换级别规则只应用于匹配的表。默认值 `.*` 匹配所有表。 +此选项为转换级规则选择表,不限制显式的 `table_transform` 条目;没有转换级规则(见 `text_field`)时,仅设置此正则表达式也不会产生任何分块规则。 -### table_transform [array] +### table_transform [Array<Map>] -针对单表的规则列表,用于上游数据源输出多张表的场景。每个条目包含 `table_path`(格式为 `databaseName[.schemaName].tableName`)以及该表的一条完整的 TextChunk 规则(`text_field` 及可选的分块选项)。有对应条目的表不会再受转换级别规则影响。参见 [多表转换](transform-multi-table.md)。 +表级规则列表,默认为空列表。每个条目必须包含非空白的 `table_path`(精确路径,不是正则表达式,格式为 `databaseName[.schemaName].tableName`),以及该表的一条完整的单表 TextChunk 规则:`text_field` 及可选的分块选项。 -### common options [string] +每个条目都是完整规则,不继承转换级选项。匹配的条目具有更高优先级,即使该表不匹配 `table_match_regex` 也会使用该条目。每个条目在作业提交时会按应用默认值后的配置独立校验,因此即使依赖默认值,也必须满足 `chunk_size > 0` 且 `0 <= overlap_size < chunk_size`。 -转换插件的常见参数, 请参考 [Transform Plugin](common-options/common-options.md) 了解详情。 +### rule_match_mode [Enum] -## 行为说明 +控制多个条目具有相同精确 `table_path` 时的行为: -- 当 `skip_empty_text = true`(默认)时,文本值为 `null` 或空字符串的输入行**不产生**任何输出行;设为 `skip_empty_text = false` 则该行会被透传(`chunk = null`、`chunk_index = 0`)。 -- 每个产生的块长度不超过 `chunk_size` 个 Unicode 码点(携带的 overlap 也计入该上限);代理对(例如 emoji)计为一个码点,不会被劈开。 -- 分隔符会被保留:每个分隔符附着在它前面那一片的末尾,因此合并多片以填满一个块时,片与片之间的空格/换行会被保留(按 `" "` 切分的单词会重新拼成 `a b` 而不是 `ab`),连续的分隔符(例如空行)也会被保留。当 `overlap_size = 0` 时,各块拼接后可还原为原始文本。 -- 同一输入行产生的多行都带有相同的源键(例如相同的 `id`)。为让它仍然唯一,转换会把 `chunk_index_field` 追加到主键和每个唯一键上。 -- 正因为存在上述键扩展,`output_field` 与 `chunk_index_field` 不能复用主键或唯一键所包含的列:用每块的值覆盖这样的键列,会导致不同源行的块在依赖键的 sink 中碰撞,因此转换会对这类配置直接报错拒绝。复用非键列(例如把块写回源文本列)仍然允许。 +- 显式设置 `FIRST_MATCH`:按列表顺序使用第一条匹配的条目。 +- 显式设置 `ALL_MATCH`:按列表顺序执行所有匹配的条目。TextChunk 是 1 行 → N 行的转换,因此前一条匹配条目产生的行会作为同表下一条匹配条目的输入。 + +虽然该选项的默认值为 `FIRST_MATCH`,但省略此选项时,重复的 `table_path` 会在转换初始化阶段被拒绝。如果确实需要重复路径,请显式设置此选项。路径不重复时,使用默认值即可。 + +### common options + +使用 `plugin_input` 和 `plugin_output` 连接上游和下游插件。通用参数详见[转换插件](common-options/common-options.md)。 + +## 多表配置 + +对于每个输入表,按照以下顺序选择有效规则: + +1. 根据 `rule_match_mode` 使用匹配的一个或多个 `table_transform` 条目。 +2. 否则,如果配置了转换级规则(见 `text_field`)且表路径匹配 `table_match_regex`,使用转换级规则。 +3. 否则,原样传递该表的行。 + +仅配置 `table_transform` 时,未列出的表保持不变。原样传递的表也保持其原始 schema:只有实际获得分块规则的表才会追加块列和块序号列。 + +下面的示例涵盖三种配置方式。共享匹配规则详见[多表转换](transform-multi-table.md)。 ## 示例 -源端数据读取的表格如下: +以下完整批处理配置使用 FakeSource 和 Console。在 SeaTunnel 发行包中安装这两个连接器后,可以将配置保存为文件,并通过 `bin/seatunnel.sh --config <file> -e local` 运行。行数据由 FakeSource 的 `rows` 选项固定,因此期望输出是确定的。 -| id | content | -|----|-----------------------------------------------| -| 1 | SeaTunnel is a data integration platform. ... | +### 仅使用转换级规则 -我们想要将 `content` 字段切分成带重叠的块,可以像这样添加 `TextChunk` 转换: +输入:表 `test.doc` 中的一行,文本共 59 个码点。在 `separators = [" "]`、`chunk_size = 20`、`overlap_size = 6` 下,文本先按空格切分,再贪心合并为不超过 20 个码点的块,并向下一块开头携带不超过 6 个码点的完整尾部片段: + +| id | content | chunk | chunk_index | +|----|---------|-------|-------------| +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `SeaTunnel is fast. ` | 0 | +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `fast. SeaTunnel is ` | 1 | +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `is reliable. ` | 2 | +| 1 | SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales. | `SeaTunnel scales.` | 3 | + +各块保留了末尾分隔符:`chunk 0` 末尾的片段 `fast. ` 被携带到 `chunk 1` 开头,`chunk 1` 末尾的 `is ` 被携带到 `chunk 2` 开头(`is reliable. `)。最后一个块是 `SeaTunnel scales.`。 + +```hocon +env { + job.mode = "BATCH" + parallelism = 1 +} + +source { + FakeSource { + plugin_output = "fake" + schema { + table = "test.doc" + fields { + id = "int" + content = "string" + } + } + rows = [ + { fields = [1, "SeaTunnel is fast. SeaTunnel is reliable. SeaTunnel scales."], kind = INSERT } + ] + } +} -``` transform { TextChunk { plugin_input = "fake" - plugin_output = "fake1" + plugin_output = "chunked" text_field = "content" - output_field = "chunk" - chunk_index_field = "chunk_index" - chunk_size = 200 - overlap_size = 20 + chunk_size = 20 + overlap_size = 6 + separators = [" "] + } +} + +sink { + Console { + plugin_input = "chunked" } } ``` -那么每个输入行会被展开为每块一行,写入结果表 `fake1`,并保留原始字段、追加 `chunk` / `chunk_index`: +### 转换级规则 + 表级规则 -| id | content | chunk | chunk_index | -|----|-----------------------------------------------|------------------------|-------------| -| 1 | SeaTunnel is a data integration platform. ... | SeaTunnel is a data... | 0 | -| 1 | SeaTunnel is a data integration platform. ... | ...platform that ... | 1 | +每个输入表各一行。`test.doc_a` 的显式条目覆盖转换级规则。`test.doc_c` 不匹配 `table_match_regex`,原样透传。 -## 作业配置示例 +| 表 | 输入行数 | 输出行数 | 有效规则 | +|----|----------|----------|----------| +| `test.doc_a` | 1 | 2 | 表级条目:`chunk_size = 2`、`separators = []`。 | +| `test.doc_b` | 1 | 3 | 转换级 `chunk_size = 3`,使用默认分隔符。 | +| `test.doc_c` | 1 | 1 | 无匹配规则;原样透传,不追加块列。 | -``` +```hocon env { - parallelism = 1 job.mode = "BATCH" + parallelism = 1 } source { FakeSource { plugin_output = "fake" - row.num = 5 - schema = { - fields { - id = "int" - content = "string" + tables_configs = [ + { + schema { + table = "test.doc_a" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [1, "xxxx"], kind = INSERT } + ] + }, + { + schema { + table = "test.doc_b" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [2, "yy yy yy "], kind = INSERT } + ] + }, + { + schema { + table = "test.doc_c" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [3, "kept as is"], kind = INSERT } + ] } - } + ] } } transform { TextChunk { plugin_input = "fake" - plugin_output = "fake1" + plugin_output = "chunked" text_field = "content" - output_field = "chunk" - chunk_index_field = "chunk_index" - chunk_size = 200 - overlap_size = 20 + chunk_size = 3 + overlap_size = 0 + table_match_regex = "test[.](doc_a|doc_b)" + table_transform = [ + { + table_path = "test.doc_a" + text_field = "content" + chunk_size = 2 + overlap_size = 0 + separators = [] + } + ] } } sink { Console { - plugin_input = "fake1" + plugin_input = "chunked" } } ``` -## 更新日志 +`test.doc_a` 按自己的条目切分为 `xx` / `xx`(2 行)。`test.doc_b` 使用转换级规则:默认分隔符把 `yy yy yy ` 切成 3 个各 3 码点的片段,产生 `yy ` / `yy ` / `yy `(3 行)。`test.doc_c` 既没有条目也不匹配转换级规则,因此它的一行原样透传。 + +### 仅使用表级规则 + +没有转换级规则:转换级未设置 `text_field`,其他转换级选项不产生任何作用。只有列出的两张表被分块;任何其他表都会原样透传。 + +| 表 | 输入行数 | 输出行数 | 有效规则 | +|----|----------|----------|----------| +| `test.doc_a` | 1 | 2 | 条目:`chunk_size = 2`、`separators = []`。 | +| `test.doc_b` | 1 | 3 | 条目:`chunk_size = 3`、`separators = []`。 | + +```hocon +env { + job.mode = "BATCH" + parallelism = 1 +} + +source { + FakeSource { + plugin_output = "fake" + tables_configs = [ + { + schema { + table = "test.doc_a" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [1, "xxxx"], kind = INSERT } + ] + }, + { + schema { + table = "test.doc_b" + columns = [ + { name = "id", type = "int" }, + { name = "content", type = "string" } + ] + } + rows = [ + { fields = [2, "yy yy yy "], kind = INSERT } + ] + } + ] + } +} -### 新版本 +transform { + TextChunk { + plugin_input = "fake" + plugin_output = "chunked" + table_transform = [ + { + table_path = "test.doc_a" + text_field = "content" + chunk_size = 2 + overlap_size = 0 + separators = [] + }, + { + table_path = "test.doc_b" + text_field = "content" + chunk_size = 3 + overlap_size = 0 + separators = [] + } + ] + } +} + +sink { + Console { + plugin_input = "chunked" + } +} +``` -- 添加文本分块(TextChunk)转换插件 +`test.doc_a` 产生 `xx` / `xx`(2 行),`test.doc_b` 产生定长窗口 `yy ` / `yy ` / `yy `(3 行)。
