我想使用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"
}
但是我找不到与此有关的任何文档,这可能吗?怎么样?
答案 0 :(得分:1)
是的,您可以使用Livy通过rest API提交spark作业。请按照以下步骤操作,
在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