Hadoop源码解析之distributedshell
1. 概述
本文介绍YARN自带的一个非常简单的应用程序编程实例—distributedshell,他可以看做YARN编程中的“helloworld”,它的主要功能是并行执行用户提供的shell命令或者shell脚本。本文主要介绍distributedshell的实现方法。
版本为hadoop-2.5.2
Distributedshell的源代码在文件夹
hadoop-2.5.2-src\hadoop-yarn-project\hadoop-yarn\hadoop-yarn-applications\hadoop-yarn-applications-distributedshell下。
Distributedshell的实现完全与一般YARN应用程序的编写方法完全一致。
2. 客户端解析
DistributedshellClient的入口main函数如下:
public static void main(String[]args) {
boolean result = false;
try {
Client client =new Client();
LOG.info("Initializing Client");
try {
boolean doRun = client.init(args);
if (!doRun) {
System.exit(0);
}
} catch (IllegalArgumentExceptione) {
System.err.println(e.getLocalizedMessage());
client.printUsage();
System.exit(-1);
}
result = client.run();
} catch (Throwable t) {
LOG.fatal("Error running CLient",t);
System.exit(1);
}
…
}
2.1 构造yarn的客户端对象yarnClient。
创建时会指定本Client要用到的AM。 创建yarnClient。yarn将client与RM的交互抽象出了编程库YarnClient,用以应用程序提交、状态查询和控制等,简化应用程序。
public Client(Configurationconf) throws Exception {
this( //指定AM
"org.apache.hadoop.yarn.applications.distributedshell.ApplicationMaster",
conf);
}
利用YarnClient类创建一个可以直接与ResourceManager交互的客户端yarnClient。
Client(String appMasterMainClass,Configuration conf) {
…
yarnClient = YarnClient.createYarnClient(); //创建yarnClient
yarnClient.init(conf);
opts = new Options();
opts.addOption("appname", true, "ApplicationName. Default value - DistributedShell");
opts.addOption("priority", true, "ApplicationPriority. Default 0");
}
2.2 初始化
init会解析命令行传入的参数,例如使用的jar包、内存大小、cpu个数等。 代码里使