我们在开发中可能会遇到获取yarn上应用占用的资源的实施情况
首先,我们感谢一下作者weixin_30828379的讲解,原文如下
weixin_30828379-【原创】大叔经验分享(21)yarn中查看每个应用实时占用的内存和cpu资源
但是大佬讲解的方式是使用url获取,实际中我们大多数使用的都是调用方法,并且在ha集群下使用配置文件读取就比较麻烦,所以宗旨就是找到ApplicationResourceUsageReport的获取方法;
在这里感谢腾讯云作者的笔记YARN任务监控界面Aggregate Resource Allocation指标解析
在文中提到如下代码,并且注释了:// 返回任务所使用的资源情况:所使用的container数量、预留的container数量、当前消耗的资源、当前预留的资源、所需的总资源(当前消耗的资源+当前预留的资源)、每秒的内存和CPU使用量。
/**
* 返回任务使用的资源情况
* @return
*/
public synchronized ApplicationResourceUsageReport getResourceUsageReport() {
AggregateAppResourceUsage resUsage = getRunningAggregateAppResourceUsage();
// 返回任务所使用的资源情况:所使用的container数量、预留的container数量、当前消耗的资源、当前预留的资源、所需的总资源(当前消耗的资源+当前预留的资源)、每秒的内存和CPU使用量
return ApplicationResourceUsageReport.newInstance(liveContainers.size(),
reservedContainers.size(), Resources.clone(currentConsumption),
Resources.clone(currentReservation),
Resources.add(currentConsumption, currentReservation),
resUsage.getMemorySeconds(), resUsage.getVcoreSeconds());
}
此处就证明了ApplicationResourceUsageReport返回的确实是实时的以及所有的统计,至此验证结束,我们来看下获取的方式
public class YarnListener {
private static Logger logger = LoggerFactory.getLogger(YarnListener.class);
public static void main(String[] args) {
URL resource = HDFSUtil.class.getClassLoader().getResource("config/hadoop-conf");
String resourceDir = resource.getPath().concat(Constants.SEPARATOR);
Configuration conf = new YarnConfiguration();
conf.addResource(new Path(resourceDir.concat("hdfs-site.xml")));
conf.addResource(new Path(resourceDir.concat("core-site.xml")));
conf.addResource(new Path(resourceDir.concat("yarn-site.xml")));
YarnClient yarnClient = YarnClient.createYarnClient();
yarnClient.init(conf);
yarnClient.start();
ApplicationId applicationId = ApplicationId.newInstance(1577686647484L,0001);
try {
ApplicationReport report = yarnClient.getApplicationReport(applicationId);
System.out.println(report.getStartTime());
System.out.println(report.getApplicationResourceUsageReport().getUsedResources().getVirtualCores());
System.out.println(report.getApplicationResourceUsageReport().getUsedResources().getMemorySize());
System.out.println(report.getStartTime());
System.out.println(report.getApplicationResourceUsageReport().toString());
} catch (YarnException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
yarnClient.stop();
}
}
输出结果如下
num_used_containers: -1
num_reserved_containers: -1
used_resources {
memory: -1
virtual_cores: -1
resource_value_map {
key: "memory-mb"
value: -1
units: "Mi"
type: COUNTABLE
}
resource_value_map {
key: "vcores"
value: -1
units: ""
type: COUNTABLE
}
}
reserved_resources {
memory: -1
virtual_cores: -1
resource_value_map {
key: "memory-mb"
value: -1
units: "Mi"
type: COUNTABLE
}
resource_value_map {
key: "vcores"
value: -1
units: ""
type: COUNTABLE
}
}
needed_resources {
memory: -1
virtual_cores: -1
resource_value_map {
key: "memory-mb"
value: -1
units: "Mi"
type: COUNTABLE
}
resource_value_map {
key: "vcores"
value: -1
units: ""
type: COUNTABLE
}
}
memory_seconds: 5549488
vcore_seconds: 5378
preempted_memory_seconds: 5549488
preempted_vcore_seconds: 5378
application_resource_usage_map {
key: "memory-mb"
value: 5549488
}
application_resource_usage_map {
key: "vcores"
value: 5378
}
application_preempted_resource_usage_map {
key: "memory-mb"
value: 5549488
}
application_preempted_resource_usage_map {
key: "vcores"
value: 5378
}
开心吗,什么都拿到了,下面的操作不用我说了吧
在日志中会有出现连接被拒绝的字眼,但是仔细看他只是在尝试不同的resourceManager节点