gpt4 book ai didi

java - 两部分 : How to run 'ls' from a java program and how to tell computers on a storm cluster to execute specific commands

转载 作者:太空宇宙 更新时间:2023-11-04 05:01:39 24 4
gpt4 key购买 nike

所以用最少的 Java 经验编写一个测试 Storm 拓扑,所以我正在用蛮力的方式解决问题。我编写 Storm 拓扑的经验也很少。

我的集群上有三个主管节点,并希望每个节点都运行 ls在终端中,将输出汇集到文件中,然后将其返回到 nimbus 节点。

首先,我如何编写一台单独的计算机来运行 ls在终端?将输出汇集到文件对于我来说很简单。我只是不知道如何编写执行终端命令的程序。

其次,我如何告诉每个主管节点运行 ls单独?

最佳答案

您可以使用下面的代码片段在 shell 中运行命令。因此,使用相同的方法在所有管理节点(来自 nimbus 等外部节点)中使用 ssh 调用特定的 ls 命令。

        public String executeCommand(String command) {
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}

希望这有帮助。

关于java - 两部分 : How to run 'ls' from a java program and how to tell computers on a storm cluster to execute specific commands,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30875816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com