Hadoop源码解析之distributedshell

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个数等。 代码里使

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值