开发者社区> 问答> 正文

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

我想使用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"

}
但我找不到任何关于此的文件

展开
收起
flink小助手 2018-12-13 11:24:36 3819 0
1 条回答
写回答
取消 提交回答
  • flink小助手会定期更新直播回顾等资料和文章干货,还整合了大家在钉群提出的有关flink的问题及回答。

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

    首先构建spark应用程序并创建程序集jar并将应用程序jar上载到hadoop集群的集群存储(HDFS)上。
    使用curl(用于测试)提交作业并使用http client 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
    }

    2019-07-17 23:20:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Hybrid Cloud and Apache Spark 立即下载
Scalable Deep Learning on Spark 立即下载
Comparison of Spark SQL with Hive 立即下载