我如何运行并返回cassandra nodetool命令及其输出

时间:2018-01-31 07:21:38

标签: cassandra cql nodetool

如何使用Java运行并返回cassandra nodetool命令及其输出。我检查了this SO question,但没有明确说明如何做到这一点。

1 个答案:

答案 0 :(得分:1)

import java.io.InputStreamReader;

public class ShellTest {
     public static void main(String[] args) throws java.io.IOException, java.lang.InterruptedException {
            // Get runtime
            java.lang.Runtime rt = java.lang.Runtime.getRuntime();
            // Start a new process: UNIX command ls
            java.lang.Process p = rt.exec("ccm node1 nodetool status");
            // You can or maybe should wait for the process to complete
            p.waitFor();
            System.out.println("Process exited with code = " + p.exitValue());
            // Get process' output: its InputStream
            java.io.InputStream is = p.getInputStream();
            java.io.BufferedReader reader = new java.io.BufferedReader(new InputStreamReader(is));
            // And print each line
            String s = null;
            while ((s = reader.readLine()) != null) {
                System.out.println(s);
            }
            is.close();
        }
}