我有一个带有 tomcat 的动态 Web 项目(安装了 hadoop)。我想让用户通过网站为我的 hadoop 应用程序定义一个参数。然后在服务器上我想通过 hadoop 使用提供的查询参数运行我的 jar。所以基本上我想在我的服务器上运行以下命令:
hadoop jar query.jar query
因为hadoop,我不能直接在我的动态web项目中直接使用query.jar的方法。所以我只希望服务器在其 cmd 中运行上述命令。我已将 query.jar 放在服务器的 WEB-INF/lib 文件夹中。我在我的 servlet 中尝试了以下代码:
String query = request.getParameter("query");
String execute = "hadoop jar WEB-INF/lib/query.jar " + query;
Process process = Runtime.getRuntime().exec(execute);
try {
process.waitFor();
} catch (InterruptedException ex) {
System.out.print("Error running jar");
}
System.out.print("Execution Successful");
虽然我总是得到“执行成功”,但我的 hadoop 作业从未在服务器上执行。我在执行字符串中提供的 query.jar 的路径是否正确?还有其他错误吗?
最佳答案
只是因为您得到 Execution Successful
并不意味着它已正确执行。更改代码如下。
String query = sanitize(request.getParameter("query"));
String hadoopHome = System.getenv("HADOOP_HOME");
if(hadoopHome == null) {
hadoopHome = "<ABSOLUTE PATH TO HADOOP>";
}
String execute = hadoopHome + "/bin/hadoop jar <ABSOLUTE PATH TO>/query.jar <Class Name> " + query;
Process process = Runtime.getRuntime().exec(execute);
int i = -1;
try {
i = process.waitFor();
} catch (InterruptedException ex) {
System.out.print("Error running jar");
}
if(i == 0) {
System.out.print("Execution Successful");
} else {
System.out.print("Error running jar");
}
方法 sanitize
应该删除所有恶意代码,因为它将在服务器端运行。
关于java - 通过hadoop在tomcat上运行jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15023648/