forked from apache/seatunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature][Transform-v2] Add metadata transform
- Loading branch information
Showing
36 changed files
with
1,719 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Metadata | ||
|
||
> Metadata transform plugin | ||
## Description | ||
Metadata transform plugin for adding metadata fields to data | ||
|
||
## Available Metadata | ||
|
||
| Key | DataType | Description | | ||
|:---------:|:--------:|:---------------------------------------------------------------------------------------------------| | ||
| database | string | Name of the table that contain the row. | | ||
| table | string | Name of the table that contain the row. | | ||
| rowKind | string | The type of operation | | ||
| ts_ms | Long | The time at which the connector processed the event. | | ||
| delay | Long | The difference between data extraction time and database change time | | ||
| partition | string | Contains the partition field of the corresponding number table of the row, multiple using `,` join | | ||
|
||
## Options | ||
|
||
| name | type | required | default value | Description | | ||
|:---------------:|------|----------|---------------|---------------------------------------------------------------------------| | ||
| metadata_fields | map | yes | | A mapping metadata input fields and their corresponding output fields. | | ||
|
||
### metadata_fields [map] | ||
|
||
A mapping between metadata fields and their respective output fields. | ||
|
||
```hocon | ||
metadata_fields { | ||
database = c_database | ||
table = c_table | ||
rowKind = c_rowKind | ||
ts_ms = c_ts_ms | ||
delay = c_delay | ||
} | ||
``` | ||
|
||
## Examples | ||
|
||
```yaml | ||
|
||
env { | ||
parallelism = 1 | ||
job.mode = "STREAMING" | ||
checkpoint.interval = 5000 | ||
read_limit.bytes_per_second = 7000000 | ||
read_limit.rows_per_second = 400 | ||
} | ||
|
||
source { | ||
MySQL-CDC { | ||
result_table_name = "customers_mysql_cdc" | ||
server-id = 5652 | ||
username = "root" | ||
password = "zdyk_Dev@2024" | ||
table-names = ["source.user"] | ||
base-url = "jdbc:mysql://172.16.17.123:3306/source" | ||
} | ||
} | ||
|
||
transform { | ||
Metadata { | ||
metadata_fields { | ||
database = database | ||
table = table | ||
rowKind = rowKind | ||
ts_ms = ts_ms | ||
delay = delay | ||
} | ||
result_table_name = "trans_result" | ||
} | ||
} | ||
|
||
sink { | ||
Console { | ||
source_table_name = "custom_name" | ||
} | ||
} | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Metadata | ||
|
||
> Metadata transform plugin | ||
## Description | ||
元数据转换插件,用于将元数据字段添加到数据中 | ||
|
||
## 支持的元数据 | ||
|
||
| Key | DataType | Description | | ||
|:---------:|:--------:|:-----------------------:| | ||
| database | string | 包含该行的数据库名 | | ||
| table | string | 包含该行的数表名 | | ||
| rowKind | string | 行类型 | | ||
| ts_ms | Long | | | ||
| delay | Long | 数据抽取时间与数据库变更时间的差 | | ||
| partition | string | 包含该行对应数表的分区字段,多个使用`,`连接 | | ||
|
||
## 配置选项 | ||
|
||
| name | type | required | default value | Description | | ||
|:---------------:|------|:--------:|:-------------:|-------------------| | ||
| metadata_fields | map | 是 | - | 元数据字段与输入字段相应的映射关系 | | ||
|
||
### metadata_fields [map] | ||
|
||
元数据字段和相应的输出字段之间的映射关系 | ||
|
||
```hocon | ||
metadata_fields { | ||
database = c_database | ||
table = c_table | ||
rowKind = c_rowKind | ||
ts_ms = c_ts_ms | ||
delay = c_delay | ||
} | ||
``` | ||
|
||
## 示例 | ||
|
||
```yaml | ||
|
||
env { | ||
parallelism = 1 | ||
job.mode = "STREAMING" | ||
checkpoint.interval = 5000 | ||
read_limit.bytes_per_second = 7000000 | ||
read_limit.rows_per_second = 400 | ||
} | ||
|
||
source { | ||
MySQL-CDC { | ||
result_table_name = "customers_mysql_cdc" | ||
server-id = 5652 | ||
username = "root" | ||
password = "zdyk_Dev@2024" | ||
table-names = ["source.user"] | ||
base-url = "jdbc:mysql://172.16.17.123:3306/source" | ||
} | ||
} | ||
|
||
transform { | ||
Metadata { | ||
metadata_fields { | ||
database = database | ||
table = table | ||
rowKind = rowKind | ||
ts_ms = ts_ms | ||
delay = delay | ||
} | ||
result_table_name = "trans_result" | ||
} | ||
} | ||
|
||
sink { | ||
Console { | ||
source_table_name = "custom_name" | ||
} | ||
} | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
seatunnel-api/src/main/java/org/apache/seatunnel/api/table/type/MetadataUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.seatunnel.api.table.type; | ||
|
||
import org.apache.seatunnel.api.table.catalog.TablePath; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
import static org.apache.seatunnel.api.table.type.CommonOptions.DELAY; | ||
import static org.apache.seatunnel.api.table.type.CommonOptions.EVENT_TIME; | ||
import static org.apache.seatunnel.api.table.type.CommonOptions.PARTITION; | ||
|
||
public class MetadataUtil { | ||
|
||
public static final List<String> METADATA_FIELDS; | ||
|
||
static { | ||
METADATA_FIELDS = new ArrayList<>(); | ||
Stream.of(CommonOptions.values()) | ||
.filter(v -> v.getSupportMetadataTrans() == 1) | ||
.map(CommonOptions::getName) | ||
.forEach(METADATA_FIELDS::add); | ||
} | ||
|
||
public static void setDelay(SeaTunnelRow row, Long delay) { | ||
row.getOptions().put(DELAY.getName(), delay); | ||
} | ||
|
||
public static void setPartition(SeaTunnelRow row, String[] partition) { | ||
row.getOptions().put(PARTITION.getName(), partition); | ||
} | ||
|
||
public static void setEventTime(SeaTunnelRow row, Long delay) { | ||
row.getOptions().put(EVENT_TIME.getName(), delay); | ||
} | ||
|
||
public static Long getDelay(SeaTunnelRow row) { | ||
return (Long) row.getOptions().get(DELAY.getName()); | ||
} | ||
|
||
public static String getDatabase(SeaTunnelRow row) { | ||
TablePath tablePath = TablePath.of(row.getTableId()); | ||
return tablePath.getDatabaseName(); | ||
} | ||
|
||
public static String getTable(SeaTunnelRow row) { | ||
TablePath tablePath = TablePath.of(row.getTableId()); | ||
return tablePath.getTableName(); | ||
} | ||
|
||
public static String getRowKind(SeaTunnelRow row) { | ||
return row.getRowKind().shortString(); | ||
} | ||
|
||
public static String getPartitionStr(SeaTunnelRow row) { | ||
Object partition = row.getOptions().get(PARTITION.getName()); | ||
return Objects.nonNull(partition) ? String.join(",", (String[]) partition) : null; | ||
} | ||
|
||
public static String[] getPartition(SeaTunnelRow row) { | ||
return (String[]) row.getOptions().get(PARTITION.getName()); | ||
} | ||
|
||
public static Long getEventTime(SeaTunnelRow row) { | ||
return (Long) row.getOptions().get(EVENT_TIME.getName()); | ||
} | ||
|
||
public static boolean isMetadataField(String fieldName) { | ||
return METADATA_FIELDS.contains(fieldName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.