[英]How to execute spark-submit in Java with Scala .jar provided?
[英]Execute spark-submit programmatically from java
我正在尝试通过以下方式执行它:
Process process = Runtime.getRuntime().exec(spark_cmd);
没有运气。 通过shell运行的命令启动我的应用程序,该应用程序成功。 通过exec运行它会启动一个进程,该进程会在不久后死掉并且什么也不做。 当我尝试
process.waitFor();
它挂起并永远等待。 当我尝试从过程中读取一些东西时,真正的魔术开始了:
InputStreamReader isr = new InputStreamReader(process.getErrorStream());
BufferedReader br = new BufferedReader(isr);
为此,我启动了一个在while循环中从流中读取的线程:
class ReadingThread extends Thread {
BufferedReader reader;
Wontekk(BufferedReader reader) {
this.reader = reader;
}
@Override
public void run() {
String line;
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
应用程序启动,执行一些操作,然后挂起。 当我中止应用程序时,spark应用程序会唤醒(??????????)并完成剩余的工作。 有人对发生的事情有合理的解释吗?
谢谢
您可以借助SparkLauncher在Java代码的帮助下以spark-submit的形式发送spark作业,因此您可以通过下面的链接并检查其
https://spark.apache.org/docs/1.4.0/api/java/org/apache/spark/launcher/SparkLauncher.html
一种方法是@Sandeep Purohit所说的Spark启动器
我会提供带有nohup命令的Shell脚本方法来提交作业,如下所示:
在执行mapreduce的情况下,这对我有用...同样,您也可以尝试执行spark后台作业。
看看https://en.wikipedia.org/wiki/Nohup “ nohup spark-submit <parameters> 2>&1 < /dev/null &
”
无论何时,您都会收到消息,然后可以轮询该事件并调用此Shell脚本。 下面是执行此操作的代码段...
/**
* This method is to spark submit
* <pre> You can call spark-submit or mapreduce job on the fly like this.. by calling shell script... </pre>
* @param commandToExecute String
*/
public static Boolean executeCommand(final String commandToExecute) {
try {
final Runtime rt = Runtime.getRuntime();
// LOG.info("process command -- " + commandToExecute);
final String[] arr = { "/bin/sh", "-c", commandToExecute};
final Process proc = rt.exec(arr);
// LOG.info("process started ");
final int exitVal = proc.waitFor();
LOG.trace(" commandToExecute exited with code: " + exitVal);
proc.destroy();
} catch (final Exception e) {
LOG.error("Exception occurred while Launching process : " + e.getMessage());
return Boolean.FALSE;
}
return Boolean.TRUE;
}
而且要调试
ps -aef | grep "your pid or process name"
下面的命令将列出该进程打开的打开文件。
lsof -p <your process id >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.