Skip to content

Commit

Permalink
[Feat-#1392][core] add parameters in json format (#1393)
Browse files Browse the repository at this point in the history
  • Loading branch information
aubdiy authored Nov 12, 2022
1 parent 964388b commit ecc9b8b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion chunjun-core/src/main/java/com/dtstack/chunjun/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void main(String[] args) throws Exception {

Options options = new OptionParser(args).getOptions();
String job = URLDecoder.decode(options.getJob(), StandardCharsets.UTF_8.name());
String replacedJob = JobUtil.replaceJobParameter(options.getP(), job);
String replacedJob = JobUtil.replaceJobParameter(options.getP(), options.getPj(), job);
Properties confProperties = PropertiesUtil.parseConf(options.getConfProp());
StreamExecutionEnvironment env = EnvFactory.createStreamExecutionEnvironment(options);
StreamTableEnvironment tEnv =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,12 @@ public class Options {
@OptionRequired(description = "env properties")
private String confProp = "{}";

@OptionRequired(description = "json modify")
@OptionRequired(description = "parameters in simple format")
private String p = "";

@OptionRequired(description = "parameters in json format")
private String pj = "";

@OptionRequired(description = "plugin load mode, by classpath or shipfile")
private String pluginLoadMode = "shipfile";

Expand Down Expand Up @@ -176,6 +179,14 @@ public void setP(String p) {
this.p = p;
}

public String getPj() {
return pj;
}

public void setPj(String pj) {
this.pj = pj;
}

public String getPluginLoadMode() {
return pluginLoadMode;
}
Expand Down
20 changes: 15 additions & 5 deletions chunjun-core/src/main/java/com/dtstack/chunjun/util/JobUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.dtstack.chunjun.constants.ConstantValue;

import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.type.TypeReference;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
Expand All @@ -31,15 +33,19 @@ private JobUtil() throws IllegalAccessException {
throw new IllegalAccessException(getClass().getName() + " can not be instantiated");
}

public static String replaceJobParameter(String p, String job) {
public static String replaceJobParameter(String p, String pj, String job) {
if (StringUtils.isNotBlank(p)) {
Map<String, String> parameters = CommandTransform(p);
job = JsonValueReplace(job, parameters);
Map<String, String> parameters = commandSimpleTransform(p);
job = jsonValueReplace(job, parameters);
}
if (StringUtils.isNotBlank(pj)) {
Map<String, String> parameters = commandJsonTransform(pj);
job = jsonValueReplace(job, parameters);
}
return job;
}

public static String JsonValueReplace(String json, Map<String, String> parameter) {
public static String jsonValueReplace(String json, Map<String, String> parameter) {
for (String item : parameter.keySet()) {
if (json.contains("${" + item + "}")) {
json = json.replace("${" + item + "}", parameter.get(item));
Expand All @@ -49,7 +55,7 @@ public static String JsonValueReplace(String json, Map<String, String> parameter
}

/** 将命令行中的修改命令转化为HashMap保存 */
public static HashMap<String, String> CommandTransform(String command) {
public static HashMap<String, String> commandSimpleTransform(String command) {
HashMap<String, String> parameter = new HashMap<>();
String[] split = StringUtils.split(command, ConstantValue.COMMA_SYMBOL);
for (String item : split) {
Expand All @@ -58,4 +64,8 @@ public static HashMap<String, String> CommandTransform(String command) {
}
return parameter;
}

public static HashMap<String, String> commandJsonTransform(String command) {
return JsonUtil.toObject(command, new TypeReference<HashMap<String, String>>() {});
}
}

0 comments on commit ecc9b8b

Please # to comment.