2015-12-21 64 views
2

是否可以在代碼內執行spark-submit腳本,然後獲取由YARN分配的應用程序ID?在Scala代碼中運行spark-submit

bin/spark-submit 
--class com.my.application.XApp 
--master yarn-cluster --executor-memory 100m 
--num-executors 50 hdfs://name.node.server:8020/user/root/x-service-1.0.0-201512141101-assembly.jar 
1000 

這是爲了使用戶能夠通過REST API啓動和停止作業。

我發現,

https://spark.apache.org/docs/latest/api/java/org/apache/spark/launcher/SparkLauncher.html

import org.apache.spark.launcher.SparkLauncher; 

     public class MyLauncher { 
     public static void main(String[] args) throws Exception { 
      Process spark = new SparkLauncher() 
      .setAppResource("/my/app.jar") 
      .setMainClass("my.spark.app.Main") 
      .setMaster("local") 
      .setConf(SparkLauncher.DRIVER_MEMORY, "2g") 
      .launch(); 
      spark.waitFor(); 
     } 
     } 

但我無法找到一個方法來獲取應用ID,也好像app.jar具有高於執行前進行預建代碼?

回答

2

是的,您的應用程序jar確實需要在這些情況下預先構建。看起來像Spark Job Server或IBM Spark Kernel可能更接近你想要的(雖然它們重用了Spark上下文)。

+0

這是否意味着不可能擁有包含Spark應用程序和Web服務的單個scala項目?目前,我的構建系統將服務和應用程序捆綁在一起。該服務包含啓動,停止等。 – nish1013

+0

如果使用Process spark = new SparkLauncher() .setSparkHome(「C:\\ spark-1.4.1-bin-hadoop2.6」) .setAppResource(「C:\\ spark -1.4.1-bin-hadoop2.6 \\ lib \\ spark-examples-1.4.1-hadoop2.6.0.jar「) .setMainClass(」org.apache.spark.examples.SparkPi「)。setMaster(」紗簇「)啓動(); 有沒有辦法在Yarn中獲取application_id? – nish1013

1

SparkLauncher只會提交您構建的應用程序。要獲取應用程序ID,您需要訪問應用程序jar中的SparkContext。

import org.apache.spark.SparkConf 
import org.apache.spark.SparkContext 
... 
val sc = new SparkContext(new SparkConf()) 
sc.applicationId 

此應用程序ID將是:

在你的榜樣,您可以用訪問應用程序ID在 「/my/app.jar」(也許在 「my.spark.app.Main」)應用程序以紗線叢集模式構建和提交時的YARN應用程序ID。

See the Spark Scala API docs.

支持訪問啓動的應用程序似乎在星火1.6(SPARK-8673)要來了。下面是一個從this test suite派生的Scala示例。

val handle = new SparkLauncher() 
    ... // application configuration 
    .setMaster("yarn-client") 
    .startApplication() 
try { 
    handle.getAppId() should startWith ("application_") 
    handle.stop() 
} finally { 
    handle.kill() 
} 

處理程序可以添加到啓動的應用程序,而是一個監聽器API被暴露,是監測啓動的應用程序的推薦方式。 See this pull request for details

+0

是否可以調用此腳本bin/spark-submit --class com.my.application。XApp - master yarn-cluster --executor-memory 100m --num -executeors 50 hdfs://name.node.server:8020/user/root/x-service-1.0.0-201512141101-assembly.jar 1000內代碼比使用ethe SparkLauncher? – nish1013

+0

@ nish1013是的,您可以在shell腳本中調用spark-submit,然後記錄控制檯輸出以捕獲應用程序ID,然後使用grep和awk獲取應用程序ID。例如'bin/spark-submit --class com.my.application.XApp ...> log.txt> 2>&1'然後'cat log.txt | grep'提交申請'$ 1 | awk'{print $ NF}'' –

+0

謝謝。有沒有辦法grep applicationId而不真正寫入文件? – nish1013