struts2 调用spark程序——java 程序调用脚本启动spark

问题描述:在开发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.csdn.net/forlong401/article/details/9139955


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值