如何通过rest api获取以cluster模式运行于yarn上的spark streaming程序的流统计信息

在apache spark的官方文档中,https://spark.apache.org/docs/latest/monitoring.html#rest-api,列举了spark程序的rest入口是 http://localhost:4040/api/v1.

当spark程序跑在yarn上的时候,类似 localhost:4040这种地址实际上被yarn的resource manager代理了,所以入口地址通常是 http://<active-rm-address>:8088/proxy/application_<id>/api/v1,其中active-rm-address即处于active状态的resource manager的主机名。那么根据上面spark的monitoring.html文档中给出的rest api路径,比如查找streaming程序的统计量,那么可以附加 /applications/<application-id>/streaming/statistics,拼接成url。

application-id可以通过yarn application -list 命令查看,或者yarn的resource manager的web ui页面查看。

示例:

GET请求: http://<active-rm-address>:8088/proxy/application_1591086111954_6422/api/v1/applications/application_1591086111954_6422/streaming/statistics  (curl <url>即可对url请求GET)。

返回结果如下所示:

{
    "startTime": "2020-09-05T20:00:43.722GMT",
    "batchDuration": 3000,
    "numReceivers": 1,
    "numActiveReceivers": 1,
    "numInactiveReceivers": 0,
    "numTotalCompletedBatches": 102909,
    "numRetainedCompletedBatches": 1000,
    "numActiveBatches": 1,
    "numProcessedRecords": 35581455,
    "numReceivedRecords": 35581802,
    "avgInputRate": 113.99567099567113,
    "avgSchedulingDelay": 114,
    "avgProcessingTime": 2436,
    "avgTotalDelay": 2551
}

 

需要注意的是,虽然通过yarn的web ui界面查看spark streaming程序的信息,进入application manager页面的时候,都有Streaming这个Tab,但是对于 /applications/<app-id>/streaming/statistics 这个接口地址,好像在spark 2.1.x的时候还没有,从spark 2.2.0开始就有了(挑几个版本大致看了下各版本文档中关于monitoring的描述,不一定准确)。

改进

  更直接的方法,通过yarn application -list 命令搞定即可。我们知道,该命令的输出内容是若干列:
                Application-Id      Application-Name        Application-Type          User           Queue                   State             Final-State             Progress                        Tracking-URL
第一列我们可以获得Application-Id, 最后一列我们可以获得 Tracking-URL。这个Tracking-URL实际上是一个跳转地址,在浏览器中输入该地址访问,会跳转为
http://<active-rm-address>:8088/proxy/redirect/application_1591086111954_6422 类似的地址,而这个地址和 http://<active-rm-address>:8088/proxy/application_1591086111954_6422 (注意url中/proxy后面没有redirect了)是一样的,
所以可以直接使用Tracking-URL这列的地址组合 /api/v1/applications/<application-id>/streaming/statistics 之类的路径,构成GET请求的目标url。这样避免确定active-rm-address的过程(尤其是对于做了rm ha的集群而言——其实后来也发现即使做了rm ha的集群,使用处于standby状态的rm的地址放在上面的url,也是可以的,会自动跳转到active状态的rm的地址)。当然了,使用Tracking-URL还是最简单的,根本不需要预先知道rm的地址。对于这种使用跳转的地址方法,curl 命令需要加 -L 参数,否则返回的是包含跳转后地址的网页。

命令的一般形式:  curl -L  <Tracking-URL>/api/v1/applications/<application-id>/streaming/statistics

提醒

  targeturl中,如果把api/v1改成app/v1,好像也是能访问的,不过返回的是网页,不是json数据。我是手误碰到这种情况,让我迷惑了很久。

 

 

参考:"How to get Spark Metrics as JSON using Spark REST API in YARN Cluster mode"

posted @ 2020-09-10 02:16  oneinmore  阅读(945)  评论(0编辑  收藏  举报