如何通过livy Programmatic API提交批处理jar Spark作业

时间:2018-11-21 03:34:04

标签: java scala apache-spark livy

我想使用livy Programmatic API提交批处理jar Spark作业,就像使用其余API批处理一样,我拥有json数据

{
    "className": "org.apache.spark.examples.SparkPi",
    "queue": "default",
    "name": "SparkPi by Livy",
    "proxyUser": "hadoop",
    "executorMemory": "5g",
    "args": [2000],
    "file": "hdfs://host:port/resources/spark-examples_2.11-2.1.1.jar"
}

但是我找不到与此有关的任何文档,这可能吗?怎么样?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用Livy通过rest API提交spark作业。请按照以下步骤操作,

  • 首先构建spark应用程序,然后创建程序集jar,然后将应用程序jar上传到hadoop集群的集群存储(HDFS)中。
  • 使用curl(用于测试)提交作业,并使用http客户端api实施。

在Scala中使用http客户端提交Spark作业的示例代码

import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet, 
HttpPost, HttpPut}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{CloseableHttpClient, HttpClientBuilder}
import org.apache.http.util.EntityUtils

import scala.util.parsing.json.{JSON, JSONObject}

def submitJob(className: String, jarPath:String, extraArgs: List[String]) : JSONObject = {

val jobSubmitRequest = new HttpPost(s"${clusterConfig.livyserver}/batches")

val data =  Map(
"className"-> className,
"file" -> jarPath,
"driverMemory" -> "2g",
"name" -> "LivyTest",
"proxyUser" -> "hadoop")

if(extraArgs != null && !extraArgs.isEmpty) {
 data  + ( "args" -> extraArgs)
}

val json = new JSONObject(data)

println(json.toString())

val params = new StringEntity(json.toString(),"UTF-8")
params.setContentType("application/json")

jobSubmitRequest.addHeader("Content-Type", "application/json")
jobSubmitRequest.addHeader("Accept", "*/*")
jobSubmitRequest.setEntity(params)

val client: CloseableHttpClient = HttpClientBuilder.create().build()
val response: CloseableHttpResponse = client.execute(jobSubmitRequest)
HttpReqUtil.parseHttpResponse(response)._2
}

请参阅该帖子以获取更多详细信息   https://www.linkedin.com/pulse/submitting-spark-jobs-remote-cluster-via-livy-rest-api-ramasamy/

以下链接中的示例项目 https://github.com/ravikramesh/spark-rest-service