[英]Starting and monitoring spark application from a Java application
我正在開發一個應用程序,以從java
應用程序執行spark
批處理應用程序。
有一個主類可以啟動線程以啟動spark
應用程序。 它使用zookeeper
在將啟動spark
應用程序的機器中找到leader
。 Main
方法如下:
public static void main(String[] args) throws IOException {
final int id = Integer.valueOf(args[0]);
final String zkURL = args[1];
final ExecutorService service = Executors.newSingleThreadExecutor();
final Future<?> status = service.submit(new ProcessNode(id, zkURL));
try {
status.get();
} catch (InterruptedException | ExecutionException e) {
LOG.fatal(e.getMessage(), e);
service.shutdown();
}
選擇leader
,將在其上運行以下代碼以啟動spark
應用程序。
protected Boolean executeCommand() {
try {
final Runtime rt = Runtime.getRuntime();
final Process proc = rt.exec("sh start-sparkapp.sh");
final int exitVal = proc.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
System.out.println(" commandToExecute exited with code: " + exitVal);
proc.destroy();
} catch (final Exception e) {
System.out.println("Exception occurred while Launching process : " + e.getMessage());
return Boolean.FALSE;
}
return Boolean.TRUE;
}
但這將啟動長期的spark
工作。 因此,我相信,只有在完成spark
作業后,才會執行下一部分代碼。 我的要求是,一旦啟動spark
應用程序,控制將轉到代碼的下一部分,在此我監視同一spark
應用程序的狀態。 即我開始了spark
應用和監控的狀態spark
從同一個應用程序java
應用程序。 假設我有一個方法montior
來監視應用程序的狀態
public String monitor(ApplicationId id)
任何建議如何實現這一目標?
由於您將使用方法public String monitor(ApplicationId id)
監視Spark應用程序,因此我假設您不希望當前線程使用proc.waitFor()
等待該進程。 此外,您不想將過程的正常輸出打印到控制台。 這兩個操作都使您的線程等待生成的進程。 此外,您的monitor方法應將生成的進程的進程ID而不是spark applicationId作為輸入。 因此,修改后的代碼如下所示:
protected Boolean executeCommand() {
try {
final Runtime rt = Runtime.getRuntime();
final Process proc = rt.exec("sh start-sparkapp.sh");
/*
*Call to method monitor(ProcessId id)
*/
} catch (final Exception e) {
System.out.println("Exception occurred while Launching process : " + e.getMessage());
return Boolean.FALSE;
}
return Boolean.TRUE;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.