You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "zhangshenghang (via GitHub)" <gi...@apache.org> on 2024/09/03 13:16:21 UTC
[PR] [Feature] Support config variable substitution with default value [seatunnel]
zhangshenghang opened a new pull request, #7562:
URL: https://github.com/apache/seatunnel/pull/7562
### Purpose of this pull request
- resolve #7535
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as the documentation fix.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released SeaTunnel versions or within the unreleased branches such as dev.
If no, write 'No'.
If you are adding/modifying connector documents, please follow our new specifications: https://github.com/apache/seatunnel/issues/4544.
-->
### How was this patch tested?
unit test and e2e test and Local
### Check list
* [ ] If any new Jar binary package adding in your PR, please add License Notice according
[New License Guide](https://github.com/apache/seatunnel/blob/dev/docs/en/contribution/new-license.md)
* [ ] If necessary, please update the documentation to describe the new feature. https://github.com/apache/seatunnel/tree/dev/docs
* [ ] If you are contributing the connector code, please check that the following files are updated:
1. Update [plugin-mapping.properties](https://github.com/apache/seatunnel/blob/dev/plugin-mapping.properties) and add new connector information in it
2. Update the pom file of [seatunnel-dist](https://github.com/apache/seatunnel/blob/dev/seatunnel-dist/pom.xml)
3. Add ci label in [label-scope-conf](https://github.com/apache/seatunnel/blob/dev/.github/workflows/labeler/label-scope-conf.yml)
4. Add e2e testcase in [seatunnel-e2e](https://github.com/apache/seatunnel/tree/dev/seatunnel-e2e/seatunnel-connector-v2-e2e/)
5. Update connector [plugin_config](https://github.com/apache/seatunnel/blob/dev/config/plugin_config)
* [ ] Update the [`release-note`](https://github.com/apache/seatunnel/blob/dev/release-note.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Feature] Support config variable substitution with default value [seatunnel]
Posted by "hailin0 (via GitHub)" <gi...@apache.org>.
hailin0 merged PR #7562:
URL: https://github.com/apache/seatunnel/pull/7562
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Feature] Support config variable substitution with default value [seatunnel]
Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #7562:
URL: https://github.com/apache/seatunnel/pull/7562#discussion_r1747893672
##########
docs/en/concept/config.md:
##########
@@ -205,8 +205,19 @@ sql = """ select * from "table" """
## Config Variable Substitution
-In config file we can define some variables and replace it in run time. **This is only support `hocon` format file**.
+In a configuration file, we can define variables and replace them at runtime. However, note that only HOCON format files are supported.
+### Usage of Variables:
+- `${varName}`: If the variable is not provided, an exception will be thrown.
+- `${varName:default}`: If the variable is not provided, the default value will be used. If you set a default value, it should be enclosed in double quotes.
+- `${varName:}`: If the variable is not provided, an empty string will be used.
+
+If you set a variable in the configuration file but do not pass it during execution, an exception will be thrown. Example:
Review Comment:
```suggestion
If you set a variable in the config file but do not pass it during execution, an exception will be thrown. Example:
```
##########
docs/en/concept/config.md:
##########
@@ -205,8 +205,19 @@ sql = """ select * from "table" """
## Config Variable Substitution
-In config file we can define some variables and replace it in run time. **This is only support `hocon` format file**.
+In a configuration file, we can define variables and replace them at runtime. However, note that only HOCON format files are supported.
Review Comment:
```suggestion
In a config file, we can define variables and replace them at runtime. However, note that only HOCON format files are supported.
```
##########
docs/en/concept/config.md:
##########
@@ -302,16 +311,16 @@ sink {
source_table_name = "sql"
username = "seatunnel=2.3.1"
password = "$a^b%c.d~e0*9("
- }
+ }
}
```
-Some Notes:
-- Quota with `'` if the value has special character such as `(`
-- If the replacement variables is in `"` or `'`, like `resName` and `nameVal`, you need add `"`
-- The value can't have space `' '`, like `-i jobName='this is a job name' `, this will be replaced to `job.name = "this"`
-- If you want to use dynamic parameters, you can use the following format: -i date=$(date +"%Y%m%d").
-
+### Important Notes:
+- If a value contains special characters like `(`, enclose it in single quotes (`'`).
+- If the substitution variable contains double or single quotes (e.g., `"resName"` or `"nameVal"`), you need to include them with the value.
+- The value cannot contain spaces (`' '`). For example, `-i jobName='this is a job name'` will be replaced with `job.name = "this"`.
+- For dynamic parameters, you can use the following format: `-i date=$(date +"%Y%m%d")`.
+-
Review Comment:
Please share system reserved keywords in here, link to `sink-options-placeholders.md`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Feature] Support config variable substitution with default value [seatunnel]
Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #7562:
URL: https://github.com/apache/seatunnel/pull/7562#discussion_r1749496808
##########
seatunnel-core/seatunnel-core-starter/src/test/java/org/apache/seatunnel/core/starter/utils/ConfigShadeTest.java:
##########
@@ -181,6 +183,70 @@ public void testVariableReplacement() throws URISyntaxException {
}
}
+ @SetEnvironmentVariable(key = "jobName", value = "seatunnel variable test job")
Review Comment:
Please update the doc.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Feature] Support config variable substitution with default value [seatunnel]
Posted by "Hisoka-X (via GitHub)" <gi...@apache.org>.
Hisoka-X commented on code in PR #7562:
URL: https://github.com/apache/seatunnel/pull/7562#discussion_r1746539503
##########
docs/en/concept/config.md:
##########
@@ -245,26 +256,24 @@ sink {
password = ${password}
}
}
-
```
-In the above config, we define some variables, like `${rowNum}`, `${resName}`.
-We can replace those parameters with this shell command:
+In the configuration above, we have defined several variables like `${rowNum}`, `${resName}`. We can replace these parameters using the following shell command:
```shell
./bin/seatunnel.sh -c <this_config_file>
-i jobName='this_is_a_job_name'
--i resName=fake
--i rowNum=10
-i strTemplate=['abc','d~f','hi']
--i nameType=string
+-i ageType=int
-i nameVal=abc
-i username=seatunnel=2.3.1
-i password='$a^b%c.d~e0*9('
--m local
+-e local
Review Comment:
```suggestion
-m local
```
Starting from version 2.3.1, the parameter -e in seatunnel.sh is deprecated, use -m instead.
##########
seatunnel-api/src/main/java/org/apache/seatunnel/api/sink/TablePlaceholder.java:
##########
@@ -33,10 +33,10 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import static org.apache.seatunnel.common.utils.PlaceholderUtils.replacePlaceholders;
+
public class TablePlaceholder {
// Placeholder ${database_name} or ${database_name:default_value}
public static final String REPLACE_DATABASE_NAME_KEY = "database_name";
Review Comment:
I can not find the logic of `system reserved keywords`. If user set `database_name` value in `-i`. We should throw exception. Because it should be handle by SeaTunnel.
##########
docs/en/concept/config.md:
##########
@@ -203,10 +203,21 @@ sql = """ select * from "table" """
```
-## Config Variable Substitution
+## Configuration Variable Substitution
Review Comment:
I believe we should keep it named `config`. Because other part use `config` too.
##########
seatunnel-core/seatunnel-core-starter/src/main/java/org/apache/seatunnel/core/starter/utils/ConfigBuilder.java:
##########
@@ -180,12 +186,98 @@ private static Config backfillUserVariables(Config config, List<String> variable
.setOriginDescription("system properties"))
.parse()
.toConfig();
- return config.resolveWith(
- systemConfig, ConfigResolveOptions.defaults().setAllowUnresolved(true));
+
+ Config resolvedConfig =
+ config.resolveWith(
+ systemConfig, ConfigResolveOptions.defaults().setAllowUnresolved(true));
+
+ Map<String, Object> configMap = resolvedConfig.root().unwrapped();
+
+ configMap.forEach(
+ (key, value) -> {
+ if (value instanceof Map) {
+ processVariablesMap((Map<String, Object>) value);
+ } else if (value instanceof List) {
+ ((List<Map<String, Object>>) value)
+ .forEach(map -> processVariablesMap(map));
+ }
+ });
+
+ return ConfigFactory.parseString(
+ JsonUtils.toJsonString(configMap),
+ ConfigParseOptions.defaults().setSyntax(ConfigSyntax.JSON))
+ .resolve(ConfigResolveOptions.defaults().setAllowUnresolved(true));
}
return config;
}
+ private static void processVariablesMap(Map<String, Object> mapValue) {
+ mapValue.forEach(
+ (innerKey, innerValue) -> {
+ if (innerValue instanceof Map) {
+ processVariablesMap((Map<String, Object>) innerValue);
+ } else if (innerValue instanceof List) {
+ mapValue.put(innerKey, processVariablesList((List<?>) innerValue));
+ } else {
+ processVariable(innerKey, innerValue, mapValue);
+ }
+ });
+ }
+
+ private static List<?> processVariablesList(List<?> list) {
+ return list.stream()
+ .map(
+ variable -> {
+ if (variable instanceof String) {
+ String variableString = (String) variable;
+ return extractPlaceholder(variableString).stream()
+ .reduce(
+ variableString,
+ (result, placeholder) -> {
+ return replacePlaceholders(
+ result,
+ placeholder,
+ System.getProperty(placeholder),
Review Comment:
I find we can support set value with space by system property or env. At now, SeaTunnel can not set value with space like `value from other`. But if user set it in environment like `export valueWithSpace="value from other"`, then we can parse it into config.
Could you add some test case for this use way and update the doc? It can fix this defect https://github.com/apache/seatunnel/pull/7562/files#diff-f7dba51c9511c67942157a5e10fc0224f0c526f024db8dc95018be4229e27bbbR321
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [Feature] Support config variable substitution with default value [seatunnel]
Posted by "zhangshenghang (via GitHub)" <gi...@apache.org>.
zhangshenghang commented on code in PR #7562:
URL: https://github.com/apache/seatunnel/pull/7562#discussion_r1750369692
##########
seatunnel-core/seatunnel-core-starter/src/test/java/org/apache/seatunnel/core/starter/utils/ConfigShadeTest.java:
##########
@@ -181,6 +183,70 @@ public void testVariableReplacement() throws URISyntaxException {
}
}
+ @SetEnvironmentVariable(key = "jobName", value = "seatunnel variable test job")
Review Comment:
Already added
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org