问题描述:在开发web项目结合spark云平台时遇到一个难题,当一个类继承了ActionSupport成为一个action后,在它的method()中不能调用spark程序(会报找不到spark jar包的错误,不知道怎么解决)。
笨拙的解决方案:把spark程序打包成jar文件,写一个脚本,在action的method()中调用这个脚本,运用spark-submit启动spark程序。
脚本代码 ,CRS.sh :
/usr/local/spark/spark-1.0.0-bin-hadoop1/bin/spark-submit \
--class ar.runCourseCF \
--master local[4] \
/root/IdeaProjects/HelloWorld/web/WEB-INF/lib/test.jar
struts2 的 action类:
public class recommendationAction extends ActionSupport
implements ModelDriven<RecommendationArguments> {
private RecommendationArguments recommendation = new RecommendationArguments();
public String execute() throws Exception
{
//脚本路径
String shellPath = "/root/IdeaProjects/HelloWorld/src/CRS.sh";
System.out.println(shellPath);
ShellUtil shell = new ShellUtil();
shell.runShell(shellPath);
return SUCCESS;
}
public RecommendationArguments getModel()
{
return recommendation;
}
}
java运行脚本的代码:
public class ShellUtil {
public void runShell(String shellPath)
{
//String shellPath="/root/IdeaProjects/CRS.sh"; //程序路径
try {
Process process = null;
//添加权限
String command1 = "chmod 777 " + shellPath;
process = Runtime.getRuntime().exec(command1);
process.waitFor();
//用sh CRS.sh执行脚本
String command2 = "sh " + shellPath;
Runtime.getRuntime().exec(command2).waitFor();
}catch(Exception e)
{
System.out.println(e.toString());
}
}
}
参考文档:
http://blog.youkuaiyun.com/forlong401/article/details/9139955