感谢@Pandey 提供的线索。答案https://stackoverflow.com/a/37980813/5634636对我帮助很大。
TL;DR
详细说明
注意:我只在 Apache Spark 2.3.1 上测试我的方法。我不能保证它也适用于其他版本。
让我们先明确我的要求。我想要 3 个功能:
- 远程提交 Spark 作业
- 随时检查作业状态(RUNNING、ERROR、FINISHED...)
- 出现错误时获取错误消息
本地提交
注意:此答案仅适用于 集群 模式
Spark 工具 spark-submit 会有所帮助。
远程提交
推荐使用 Spark 提交 API。 Apache Spark 官网上好像没有任何文档,所以有人称之为hidden API。详情见:https://www.nitendragautam.com/spark/submit-apache-spark-job-with-rest-api/
- 要提交 Spark 作业,请使用提交 API
- 要获取作业的状态,请使用状态 API:
http://<master-ip>:6066/v1/submissions/status/<submission-id>。提交作业时,submission-id 将以 json 格式返回。
- 错误消息包含在状态消息中。
- 有关错误消息的更多信息:注意状态 ERROR 和 FAILED 之间的区别。简而言之,FAILED 表示在执行 Spark 作业 期间出现问题(例如未捕获的异常),而 ERROR 表示在 提交 期间出现错误(例如无效的 jar 路径) .错误消息包含在状态 json 中。如果要查看 FAILED 原因,可以通过
http://<driver-ip>:<ui-port>/log/<submission-id> 访问。
这是一个错误状态示例(**** 是一个不正确的 jar 路径,是故意写错的):
{
"action" : "SubmissionStatusResponse",
"driverState" : "ERROR",
"message" : "Exception from the cluster:\njava.io.FileNotFoundException: File hdfs:**** does not exist.\n\torg.apache.hadoop.hdfs.DistributedFileSystem.listStatusInternal(DistributedFileSystem.java:795)\n\torg.apache.hadoop.hdfs.DistributedFileSystem.access$700(DistributedFileSystem.java:106)\n\torg.apache.hadoop.hdfs.DistributedFileSystem$18.doCall(DistributedFileSystem.java:853)\n\torg.apache.hadoop.hdfs.DistributedFileSystem$18.doCall(DistributedFileSystem.java:849)\n\torg.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81)\n\torg.apache.hadoop.hdfs.DistributedFileSystem.listStatus(DistributedFileSystem.java:860)\n\torg.apache.spark.util.Utils$.fetchHcfsFile(Utils.scala:727)\n\torg.apache.spark.util.Utils$.doFetchFile(Utils.scala:695)\n\torg.apache.spark.util.Utils$.fetchFile(Utils.scala:488)\n\torg.apache.spark.deploy.worker.DriverRunner.downloadUserJar(DriverRunner.scala:155)\n\torg.apache.spark.deploy.worker.DriverRunner.prepareAndRunDriver(DriverRunner.scala:173)\n\torg.apache.spark.deploy.worker.DriverRunner$$anon$1.run(DriverRunner.scala:92)",
"serverSparkVersion" : "2.3.1",
"submissionId" : "driver-20190315160943-0005",
"success" : true,
"workerHostPort" : "172.18.0.4:36962",
"workerId" : "worker-20190306214522-172.18.0.4-36962"
}