scala-flink监控api(上传作业)

cgh8pdjw  于 2021-06-25  发布在  Flink
关注(0)|答案(1)|浏览(410)

你好,我有一个问题上传作业到FlinkAPI使用scala
所有的请求似乎都起作用了

import scalaj.http._ 
val url: String = "http://127.0.0.1:8081"
val response: HttpResponse[String] = Http(url+"/config").asString
return response

当我尝试通过curl上传jar文件时(works)

curl -vvv -X POST -H "Expect:" -F "jarfile=@/home/Downloads/myJob.jar" http://127.0.0.1:8081/jars/upload

现在我想使用scala上传文档,但文档没有提供一个工作示例,我对这类文章还比较陌生:https://ci.apache.org/projects/flink/flink-docs-release-1.3/monitoring/rest_api.html#submitting-程序
目前我的代码是(不工作):取自:https://github.com/guru107/flinkjobuploadplugin/tree/master/src/main/java/com/github/guru107 -根据我的需要编辑

// Ideal Case is to upload a Jar File as a multipart in Scala
  import java.io.IOException
  import org.apache.http.client.methods.HttpPost
  import org.apache.http.entity.mime.MultipartEntityBuilder
  import org.apache.http.impl.client.{HttpClients, LaxRedirectStrategy}
  import org.apache.http.message.BasicHeader
  import org.apache.http.util.EntityUtils

  val requestUrl = "http://localhost:8081/jars/upload"
  val jarPath = "@/home/Downloads/myJob.jar"

  val httpClient: CloseableHttpClient = HttpClients.custom.setRedirectStrategy(new LaxRedirectStrategy).build

  val fileToUpload: File = new File(jarPath)
  val uploadFileUrl: HttpPost = new HttpPost(requestUrl)

  val builder: MultipartEntityBuilder = MultipartEntityBuilder.create
  builder.addBinaryBody("jarfile", fileToUpload)

  val multipart: HttpEntity = builder.build
  var jobUploadResponse: JSONObject = null

  uploadFileUrl.setEntity(multipart)
  var response: CloseableHttpResponse  = null
  try {
    response = httpClient.execute(uploadFileUrl)
    println("response: " + response)
    response.setHeader(new BasicHeader("Expect", ""))
    response.setHeader(new BasicHeader("content-type", "application/x-java-archive"))
    val bodyAsString = EntityUtils.toString(response.getEntity, "UTF-8")
    println("bodyAsString: " + bodyAsString)
    jobUploadResponse = new JSONObject(bodyAsString)
    println("jobUploadResponse: " + jobUploadResponse)
  }

无法上载文件。
请提供一个工作示例或scala示例的链接,以便将job/jar文件上载到scala中的flink
提前谢谢

roejwanj

roejwanj1#

您可以使用 com.github.mjreid.flinkwrapper 上传带有scala代码的jar文件:

val apiEndpoint: String = as.settings.config.getString("flink.url") //http://<flink_web_host>:<flink_web_port>
val client = FlinkRestClient(apiEndpoint, as)
client.runProgram(<jarId>)

相关问题