yarn 简单的client客户端实现

一、Client只与ResourceManager交互,主要有以下步骤:
 
1、向ResourceManager注册自己,并获得ResourceMangaer的响应,RM在响应中会报告集群资源(maximumResourceCapability),其中有最大可用VCores数量和最大可用内存。
2、client构造ApplicationSubmissioinContext,内容包括ApplicationId,ApplicationName,mem,vcores,priority,queue和ContainerLauncherContext,其中必须的是mem,vcores,ContainerLauncherContext,ContainerLauncherContext中需要设置三个内容:
  • localResources:container运行时候需要的本地文件,可能是脚本、jar包等。通常会上传到hdfs中,container运行时再去下载下来。
  • enviroment:container运行时候需要设置的环境变量。
  • commands:container执行命令,是linux命令。
3、提交ApplicationSubmissionContext。调用submitApplication()方法,返回一个ApplicationId。
4、client通过applicationId 查询任务的运行情况,直到任务Application状态变为FINISHED。
 
二、源码分析:
 
实现以上四个步骤的用到的接口,但这些接口不是唯一的,还有其他接口也能实现一样的功能。
1、向ResourceManager注册:
1 YarnClientApplication app = yarnClient.createApplication();
2 GetNewApplicationResponse appResponse = app.getNewApplicationResponse();
获知集群最大可用资源:
1 Resource clusterMax = appResponse.getMaximumResourceCapability();
2 int maxMem = clusterMax.getMemory();
3 int maxCore = clusterMax.getVirtualCores();
2、准备提交ApplicationMaster要运行的相关信息ApplicationSubmissioinContext:
 1 ApplicationSubmissionContext appContext = app.getApplicationSubmissionContext();
 2 ContainerLaunchContext clc = Records.newRecord(ContainerLaunchContext.class);
 3 /*mem、vcores*/
 4 Resource amResource = Records.newRecord(Resource.class);
 5 amResource.setMemory(Math.min(clusterMax.getMemory(), 1024));
 6 amResource.setVirtualCores(Math.min(clusterMax.getVirtualCores(), 4));
 7 appContext.setResource(amResource);
 8 
 9 /*localResource*/
10 Map<String, LocalResource> localResourceMap = new HashMap<String, LocalResource>();
11 File appMasterJarFile = new File(appMasterJar);
12 localResourceMap.put(appMasterJarFile.getName(), toLocalResource(fs,appResponse.getApplicationId().toString(),appMasterJarFile));
13 clc.setLocalResources(localResourceMap);

猜你喜欢

转载自www.cnblogs.com/chenqy930/p/9374317.html
今日推荐