2013-02-22 95 views
0

我有一个使用tomcat的动态web项目(hadoop已安装)。我想让用户通过网站为我的hadoop应用程序定义一个参数。然后在服务器上,我希望通过hadoop提供的查询参数运行我的jar。所以基本上我想我的服务器上运行以下命令:在tomcat上通过hadoop运行jar

hadoop jar query.jar query 

因为Hadoop的我不能简单地直接使用query.jar的方法在我的动态Web项目。所以我只想让服务器在其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的工作永远不会在服务器上执行。我在提供的execute String中的query.jar的路径是否正确?还有其他错误吗?

回答

0

只是因为你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应该删除任何恶意代码。

相关问题