使用SparkLauncher提交Spark作业

随声附和 提交于 2020-02-27 04:30:26

通常情况下,使用spark-submit来提交作业。
是否有办法使用代码动态地提交作业?
本文使用SparkLauncher提供了一种解决方法。

提交Spark Application


val handle: SparkAppHandle = newSparkLauncher()
      .setSparkHome("/path/to/spark/home")
      .setAppResource("/path/to/your/spark/program/jar")
      .setConf("spark.driver.memory", "2")
      .setConf("spark.driver.extraClassPath", "/your/class/path")
      .setConf("spark.executor.extraClassPath", "/your/class/path")
      .setConf("spark.executor.memory", "2")
      .setConf("spark.executor.cores", "10")
      .setConf("spark.num.executors", "5")
      .setMainClass("XXXXCLASS")
      .setVerbose(true)
      .setMaster("yarn")
      .setDeployMode("cluster")
      .startApplication(newSparkAppHandle.Listener {
override def stateChanged(handle: SparkAppHandle): Unit = {
        }
override def infoChanged(handle: SparkAppHandle): Unit = {
        }
      })

监控所提交的作业


返回值类型的为SparkAppHandle, 可以随时获取作业的状态

handle.getAppId
handle.getState

提交作业时, 传入的Listener可以监听状态事件

      .startApplication(newSparkAppHandle.Listener {
override def stateChanged(handle: SparkAppHandle): Unit = {
            println(handle.getState)
        }
override def infoChanged(handle: SparkAppHandle): Unit = {
            println(handle.getState)
        }

控制所提交的作业


利用返回值handle

handle.stop
handle.kill

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!