【发布时间】:2018-01-31 18:30:47
【问题描述】:
如何使用 Java 运行和返回 cassandra nodetool 命令及其输出。我检查了this SO question,但不清楚具体如何操作。
【问题讨论】:
-
你的用例是什么?也许您可以从 JMX 中获取这些信息或通过 JMX 运行一些命令。
-
我想在 java 中运行任何 nodetool 命令并获取输出
如何使用 Java 运行和返回 cassandra nodetool 命令及其输出。我检查了this SO question,但不清楚具体如何操作。
【问题讨论】:
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();
}
}
【讨论】: