继承关系:

  1. java.util

Interface Map.Entry<K,V>

description:

public static interface Map.Entry<K,V>

methods:

Modifier and Type Method and Description
boolean equals(Object o)

Compares the specified object with this entry for equality.
K getKey()

Returns the key corresponding to this entry.
V getValue()

Returns the value corresponding to this entry.
int hashCode()

Returns the hash code value for this map entry.
V setValue(V value)

Replaces the value corresponding to this entry with the specified value (optional operation).

2.java.lang.Object

|__ org.apache.hadoop.conf.Configuration

constructor: public class Configuration extends Objectimplements Iterable<Map.Entry<String,String>>, Writable 

3.org.apache.hadoop.util Class ToolRunner java.lang.Object   |__ org.apache.hadoop.util.ToolRunner

description:
public class ToolRunner

extends Object

  ToolRunner can be used to run classes implementing Tool interface. It works in conjunction with GenericOptionsParser to parse the generic hadoop command line arguments and modifies the Configuration of the Tool. The application-specific options are passed along without being modified.

methods:
static int run(Configuration conf, Tool tool, String[] args)
Runs the given Tool by Tool.run(String[]),
after parsing with the given generic arguments.
static int run(Tool tool, String[] args)

Runs the Tool with its
Configuration.
4.org.apache.hadoop.util 

Interface Tooldescription:
public interface Tool

extends Configurablemethods:
int run(String[] args)
Execute the command with the given arguments.
 5.org.apache.hadoop.conf 

Interface Configurable

constructor:

public interface Configurable

methods:
Configuration getConf()
Return the configuration used by this object.
void setConf(Configuration conf)

Set the configuration to be used by this object.
 6.
java.lang.Object
  |__ org.apache.hadoop.conf.Configureddescription:
public class Configured

extends Objectimplements Configurable
constructor:
Configured()
Construct a Configured.
Configured(Configuration conf)

Construct a Configured
 
methods:
Configuration getConf()
Return the configuration used by this object.
void setConf(Configuration conf)

Set the configuration to be used by this object.
Code1 (Configuration里添加的resource是String类型):
 import java.util.Map.Entry;

 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.conf.Configured;
 import org.apache.hadoop.util.ToolRunner;
 import org.apache.hadoop.util.Tool;
 import org.apache.hadoop.fs.Path;

 public class ConfigurationPrinter extends Configured implements Tool {
   static {
     Configuration.addDefaultResource("config.xml");
   }

   @Override
   public int run(String[] args) throws Exception {
     Configuration conf = getConf();
     for (Entry<String, String> hash: conf) {
       System.out.printf("%s=%s\n", hash.getKey(), hash.getValue());
     }
     return 0;
   }

   public static void main(String[] args) throws Exception {
     int exitCode = ToolRunner.run(new ConfigurationPrinter(), args);
     System.exit(exitCode);
   }
 }

注:Configuration class提供只一种静态方法:addDefaultresource(String name), 如上述代码, 添加Resource "config.xml"为String类型时,hadoop将从classpath里查找此文件;若Resource 为Path()类型时,hadoop将从local filesystem里查找此文件: Configuration conf = new Configuration(); conf.addResource(new Path("config.xml"));

code1的执行步骤:

#将自定义的config文件config.xml放在hadoop的$HADOOP_CONF_DIR里
mv config.xml $HADOOP_HOME/etc/hadoop/

#假如我们添加的resource如下:

 <!--cat $HADOOP_HOME/etc/hadoop/config.xml-->
 <configuration>
   <property>
     <name>color</name>
     <value>yellow</value>
   </property>

   <property>
     <name>size</name>
     <value>10</value>
   </property>

   <property>
     <name>weight</name>
     <value>heavy</value>
     <final>true</final>
   </property>
 </configuration>

执行代码:

mkdir class
source $HADOOP_HOME/libexec/hadoop-config.sh
javac  -d class ConfigurationPrinter.java
jar -cvf ConfigurationPrinter.jar -C class ./
export HADOOP_CLASSPATH=ConfigurationPrinter.jar:$CLASSPATH
#下面查找刚才添加的resource是否被读入
#我们在config.xml里添加了一项 <name>color</name>,执行
yarn ConfigurationPrinter|grep "color"
color=yellow
#可见代码是正确的

或者在commandline里指定HADOOP_CONF_DIR, 比如执行:

yarn ConfigurationPrinter --conf config.xml | grep color

color=yellow

也是可以的!

Code2 (Configuration里添加的resource是Path类型):

 import java.util.Map.Entry;

 import org.apache.hadoop.conf.Configuration;
 import org.apache.hadoop.conf.Configured;
 import org.apache.hadoop.util.ToolRunner;
 import org.apache.hadoop.util.Tool;
 import org.apache.hadoop.fs.Path;

 public class ConfigurationPrinter extends Configured implements Tool {
   @Override
   public int run(String[] args) throws Exception {
     Configuration conf = new Configuration();
     conf.addResource(new Path("config.xml"));
     for (Entry<String, String> hash: conf) {
       System.out.printf("%s=%s\n", hash.getKey(), hash.getValue());
     }
     return 0;
   }

   public static void main(String[] args) throws Exception {
     int exitCode = ToolRunner.run(new ConfigurationPrinter(), args);
     System.exit(exitCode);
   }
 }

此时添加的resource类型是Path()类型,故hadoop将从local filesystem里查找config.xml, 不需要将config.xml放在conf/下面,只要在代码中指定config.xml在本地文件系统中的路径即可(new Path("../others/config.xml"))

运行步骤:

mkdir class
source $HADOOP_HOME/libexec/hadoop-config.sh
javac  -d class ConfigurationPrinter.java
jar -cvf ConfigurationPrinter.jar -C class ./
export HADOOP_CLASSPATH=ConfigurationPrinter.jar:$CLASSPATH
#下面查找刚才添加的resource是否被读入
#我们在config.xml里添加了一项 <name>color</name>,执行
yarn ConfigurationPrinter|grep "color"
color=yellow
#可见代码是正确的

 

备注:ConfigurationParser支持set individual properties:

Generic Options
The supported generic options are:

-conf <configuration file>     specify a configuration file
     -D <property=value>            use value for given property
     -fs <local|namenode:port>      specify a namenode
     -jt <local|jobtracker:port>    specify a job tracker
     -files <comma separated list of files>    specify comma separated
                            files to be copied to the map reduce cluster
     -libjars <comma separated list of jars>   specify comma separated
                            jar files to include in the classpath.
     -archives <comma separated list of archives>    specify comma
             separated archives to be unarchived on the compute machines.

可以尝试:

yarn ConfigurationPrinter -d fuck=Japan | grep fuck
#输出为:
fuck=Japan

再次提醒:

ToolRunner can be used to run classes implementing Tool interface. It works in conjunction with GenericOptionsParser to parse the generic hadoop command line arguments and modifies the Configuration of the Tool. The application-specific options are passed along without being modified.

ToolRunnerGenericOptionsParser共同来(解析|修改) generic hadoop command line arguments  (什么是generic hadoop command line arguments? 比如:yarn  command [genericOptions] [commandOptions]

hadoop2.2编程:Tool, ToolRunner, GenericOptionsParser, Configuration的更多相关文章

  1. hadoop2.2编程:从default mapreduce program 来理解mapreduce

    下面写一个default mapreduce 的程序: import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapr ...

  2. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  3. hadoop2.2编程:MRUnit测试

    引用地址:http://www.cnblogs.com/lucius/p/3442381.html examples: Overview This document explains how to w ...

  4. hadoop2.2编程:矩阵相乘简单实现

    /* matrix-matrix multiplication on Hadoop A x B = C constraint: A, B, C must be of the same size I u ...

  5. hadoop2.2编程:MRUnit

    examples: Overview This document explains how to write unit tests for your map reduce code, and test ...

  6. hadoop2.2编程:DFS API 操作

    1. Reading data from a hadoop URL 说明:想要让java从hadoop的dfs里读取数据,则java 必须能够识别hadoop hdfs URL schema, 因此我 ...

  7. LPCScrypt, DFUSec : USB FLASH download, programming, and security tool, LPC-Link 2 Configuration tool, Firmware Programming

    What does this tool do? The LPC18xx/43xx DFUSec utility is a Windows PC tool that provides support f ...

  8. hadoop2.2编程: SequenceFileWritDemo

    import java.io.IOException; import java.net.URI; import org.apache.hadoop.fs.FileSystem; import org. ...

  9. Hadoop2.2编程:新旧API的区别

    Hadoop最新版本的MapReduce Release 0.20.0的API包括了一个全新的Mapreduce JAVA API,有时候也称为上下文对象. 新的API类型上不兼容以前的API,所以, ...

随机推荐

  1. [.Net MVC] 使用SQL Server数据库代替LocalDb

    之前开发的时候一直用的VS2013,所以数据库也用的LocalDb,这给开发带来很大便利.不过由于开发后还要进行部署,就改用了SQL Server 2012,这里总结下过程. 基本环境:VS2013, ...

  2. bzoj3208:花神的秒题计划I

    思路:因为Q.S.B操作总和不超过100,因此怎么暴力怎么写....当然记忆化搜索还是要的 #include<cstdio> #include<iostream> #inclu ...

  3. 二叉搜索的各种bugs——重复递增序列

    int binary_search(int* A, int value, int p, int r); int main(int argc, char *argv[]){ , , , , , , , ...

  4. OpenJudge 2753 菲波那契数列

    1.链接地址: http://bailian.openjudge.cn/practice/2753 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 菲波那契数列是指这样的数列 ...

  5. memcached全面剖析--4

    memcached的分布式算法   memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第 ...

  6. PHP学习笔记(5) - 选择一个合格的框架

    作为一个合格的PHP开发框架,个人觉得起码需要满足以下几点. 一.上梁不正下梁歪,好的框架首先自身要有良好的编码规范和文件目录结构,代码要易于阅读理解. 二.为了可以更好地适应OOP,框架必须可以提供 ...

  7. ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order

    如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...

  8. 第一部分实现功能:使用一个TabControl和一个Memo和TDictionary类实现文本临时存储

    效果图: 一期功能概要: a.双击tab关闭tab,双击tab右边空白添加tab(标题为以hhnnsszzz的时间格式命名) b.切换tab将数据存入dictionary,key为标题,value为m ...

  9. easy-ui datagrid

    Easy-ui引用    <link href="css/EasyUI/themes/icon.css" rel="stylesheet" type=&q ...

  10. WPF 控件

    ****ScrollViewer**** 前台: <ScrollViewer  HorizontalScrollBarVisibility="Auto" VerticalSc ...