简体   繁体   English

两部分:如何从 Java 程序运行 'ls' 以及如何告诉 Storm 集群上的计算机执行特定命令

[英]Two part: How to run 'ls' from a java program and how to tell computers on a storm cluster to execute specific commands

So writing a test storm topology with minimal Java experience, so I'm figuring things out in a brute force way.所以用最少的 Java 经验编写一个测试风暴拓扑,所以我正在以蛮力的方式解决问题。 My experience writing storm topologies is also minimal.我编写风暴拓扑的经验也很少。

I have three supervisor nodes on my cluster and want each of them to run ls in the terminal, funnel the output to a file and then return it to the nimbus node.我的集群上有三个主管节点,希望每个节点都在终端中运行ls ,将输出汇集到一个文件中,然后将其返回到 nimbus 节点。

Firstly, how would i code an individual computer to run ls in the terminal?首先,我将如何编码一台单独的计算机来在终端中运行ls Funneling the output to a file is simple enough for me to figure out.将输出汇集到一个文件对我来说很简单。 I just don't know how to write programs that execute terminal commands.我只是不知道如何编写执行终端命令的程序。

Secondly, how do i tell each of my supervisor nodes to run ls individually?其次,我如何告诉我的每个主管节点单独运行ls

You can use below snippet to run a command in shell.您可以使用以下代码段在 shell 中运行命令。 So use this same method to invoke the specific ls command using ssh in all supervisor nodes (from external node like nimbus).因此,使用相同的方法在所有主管节点(来自外部节点,如 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();
    }

Hope this helped.希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM