如何在 pyspark 中以编程方式获取 YARN "Memory Total" 和 "VCores Total" 指标

How to get YARN "Memory Total" and "VCores Total" metrics programmatically in pyspark

我一直在纠结这个: https://docs.actian.com/vectorhadoop/5.0/index.html#page/User/YARN_Configuration_Settings.htm

但是 none 这些配置是我所需要的。

"yarn.nodemanager.resource.memory-mb" 很有前途,但它似乎只适用于节点管理器,并且只获得主内存和 cpu,而不是集群的。

int(hl.spark_context()._jsc.hadoopConfiguration().get('yarn.nodemanager.resource.memory-mb'))

您可以从 Yarn History Server 访问这些指标。
URL: http://rm-http-address:port/ws/v1/cluster/metrics
指标:

totalMB
totalVirtualCores  

示例响应(也可以是 XML):

{  "clusterMetrics":   {
    "appsSubmitted":0,
    "appsCompleted":0,
    "appsPending":0,
    "appsRunning":0,
    "appsFailed":0,
    "appsKilled":0,
    "reservedMB":0,
    "availableMB":17408,
    "allocatedMB":0,
    "reservedVirtualCores":0,
    "availableVirtualCores":7,
    "allocatedVirtualCores":1,
    "containersAllocated":0,
    "containersReserved":0,
    "containersPending":0,
    "totalMB":17408,
    "totalVirtualCores":8,
    "totalNodes":1,
    "lostNodes":0,
    "unhealthyNodes":0,
    "decommissioningNodes":0,
    "decommissionedNodes":0,
    "rebootedNodes":0,
    "activeNodes":1,
    "shutdownNodes":0   } }

https://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/ResourceManagerRest.html#Cluster_Metrics_API

你只需要弄清楚你的 Yarn History Server 地址和端口 - 检查你的配置文件,我无法帮助你,因为我不知道你在哪里管理 Yarn。

当您拥有 URL 时,使用 python 访问它:

import requests
url = 'http://rm-http-address:port/ws/v1/cluster/metrics'
reponse = requests.get(url)
# Parse the reponse json/xml and get the relevant metrics... 

当然,此解决方案中不需要 Hadoop 或 Spark 上下文