Flink提供ParameterTool类,从不同的来源读取参数;
1.fromArgs()
即可读取在命令行中传递的参数,注意传参 格式为 key value ,key必须以 - 或者 – 开头
2.fromPropertiesFile
3.get
若传递的参数多,可以在命令行中使用fromArgs传递 PropertiesFile 的文件路径,然后再运用此方法读取配置文件中的参数。
4.DEMO
public class Demo {
public static void main(String[] args) throws Exception {
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
ParameterTool parameters = ParameterTool.fromArgs(args);
String local_path = parameters.get("path",null); //指定参数名:path
DataSet<String> toFilter = env.fromElements(local_path);
FlatMapOperator<String, String> flatMap = toFilter.flatMap(new RichFlatMapFunction<String, String>() {
@Override
public void flatMap(String value, Collector<String> out) throws Exception {
String[] split = value.split(",");
for(String s:split){
out.collect(s);
}
}
});
flatMap.print();
}
}
public class Upjar {
public static void main(String[] args) throws IOException {
List<String> arg = new ArrayList<>();
arg.add("sh");
arg.add("-c");
arg.add("./flink run -p 2 -C file:///home/czFlinkDemo/target/czFlinkDemo-1.0-SNAPSHOT.jar -c com.cz.dataset.wordCount.Demo /home/czFlinkDemo/target/czFlinkDemo-1.0-SNAPSHOT.jar -path a,gh,jk,ui,o\n");
ProcessBuilder pb = new ProcessBuilder(arg);
//设置工作目录
pb.directory(new File("/home/flink/flink-1.10.0/bin"));
//redirectErrorStream 属性默认值为false,意思是子进程的标准输出和错误输出被发送给两个独立的流,可通过 Process.getInputStream() 和 Process.getErrorStream() 方法来访问
//值设置为 true,标准错误将与标准输出合并。合并的数据可从 Process.getInputStream() 返回的流读取,而从 Process.getErrorStream() 返回的流读取将直接到达文件尾
pb.redirectErrorStream(true);
File log = new File("/mnt/htst/log/htst.log");
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(log));
Process p = pb.start();
assert pb.redirectInput() == ProcessBuilder.Redirect.PIPE;
//重定向标准输出到日志
assert pb.redirectOutput().file() == log;
assert p.getInputStream().read() == -1;
}
}
注意: 把自定义的传参放到命令行的最后