今日突发奇想,想在Java代码中执行hadoop命令如:
“hadoop fs -ls /”
于是查了一下,发现可以利用Java的Runtime来完成上面功能,具体代码如下:
public static void main(String[] args) {
try {
Process ps = Runtime.getRuntime().exec("/home/hadoop/hadoop/hadoop-1.2.1/bin/hadoop jar /home/hadoop/mahout/mahout-distribution-0.9/mahout-core-0.9-job.jar org.apache.mahout.fpm.pfpgrowth.FPGrowthDriver -i /user/hadoop/testdata/history_download -o out/ -k 3 -method mapreduce -regex '[\\t]' -s 10");<span style="white-space:pre"> </span>System.out.println(loadInputStream(ps.getInputStream()));
System.out.println(loadInputStream(ps.getErrorStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
private static String loadInputStream(InputStream is) throws IOException {
int ptr = 0;
BufferedInputStream bis = new BufferedInputStream(is);
StringBuffer sb = new StringBuffer();
while ((ptr = bis.read()) != -1) {
sb.append((char)ptr);
}
return sb.toString();
}
其中,用到了一下命令执行代码,代码中命令的路径最好是完整的路径,不然可能会报找不到路径错误,执行后会产生命令执行的输出。
Runtime.getRuntime().exec()