A-A+

Hadoop源码解析之Configuration类

2016年05月25日 Hadoop 暂无评论 阅读 1,031 views 次

hadoop-logo

说在前面的话

JDK提供了jara.util.Properties类,用于处理配置文件,它继承自Hashtable表示了一个持久的属性集,该集可保存在流中或从流中加载。属性列表中每个键及其对应值都是字符串类型。

jara.util.Properties支持使用反射getset值。

Hadoop没有使用java.util.Properties管理配置文件,也没有使用Apache Jakarta Commons Configuration管理配置文件,而是使用了一套独有的配置文件管理系统,并提供自己的API,即使用 org.apache.hadoop.conf.Configuration处理配置信息。

Hadoop配置文件的格式解析

  1. <?xml version="1.0"?>
  2.     <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3.     <configuration>
  4.       <property>
  5.          <name>io.sort.factor</name>
  6.          <value>10</value>
  7.          <description>The number of streams to merge at once while sorting
  8.          files.  This determines the number of open file handles.</description>
  9.       </property>
  10.     <property>
  11.          <name>dfs.name.dir</name>
  12.          <value>${hadoop.tmp.dir}/dfs/name</value>
  13.          <description>Determines where on the local filesystem the DFS name
  14.          nodeshould store the name table(fsimage).  ……</description>
  15.       </property>
  16.     <property>
  17.          <name>dfs.web.ugi</name>
  18.          <value>webuser,webgroup</value>
  19.          <final>true</final>
  20.          <description>The user account used by the web interface.
  21.          Syntax: USERNAME,GROUP1,GROUP2, ……</description>
  22.       </property>
  23. </configuration>

Hadoop配置文件的根元素是configuration,一般只包含子元素property。每一个property元素就是一个配置项,配置文件不 支持分层或分级。每个配置项一般包括配置属性的名称name、值value和一个关于配置项的描述description;元素final和Java中的 关键字final类似,意味着这个配置项是“固定不变的”。final一般不出现,但在合并资源的时候,可以防止配置项的值被覆盖。

在上面的示例文件中,配置项dfs.web.ugi的值是“webuser,webgroup”,它是一个final配置项;从 description看,这个配置项配置了Hadoop Web界面的用户账号,包括用户名和用户组信息。这些信息可以通过Configuration类提供的方法访问。

在Configuration中,每个属性都是String类型的,但是值类型可能是以下多种类型,包括Java中的基本类型,如 boolean(getBoolean)、int(getInt)、long(getLong)、float(getFloat),也可以是其他类型,如 String(get)、java.io.File(getFile)、String数组(getStrings)等。以上面的配置文件为 例,getInt("io.sort.factor")将返回整数10;而getStrings("dfs.web.ugi")返回一个字符串数组,该数组有两个元素,分别是webuser和webgroup。

  1. Configurationconf = new Configuration();
  2. conf.addResource("core-default.xml");
  3. conf.addResource("core-site.xml");

合并资源指将多个配置文件合并,产生一个配置。如果有两个配置文件,也就是两个资源,如core-default.xml和core-site.xml,通过Configuration类的loadResources()方法,把它们合并成一个配置。代码如下:
如果这两个配置资源都包含了相同的配置项,而且前一个资源的配置项没有标记为final,那么,后面的配置将覆盖前面的配置。上面的例子 中,core-site.xml中的配置将覆盖core-default.xml中的同名配置。如果在第一个资源(core-default.xml)中 某配置项被标记为final,那么,在加载第二个资源的时候,会有警告提示。

Configuration类

直接运行Configuration.java则会调用默认配置文件部分结果如下:

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <configuration>
  3. <property>
  4.     <name>ipc.client.fallback-to-simple-auth-allowed</name>
  5.     <value>false</value>
  6.     <source>core-default.xml</source>
  7. </property>
  8. <property>
  9.     <name>file.bytes-per-checksum</name>
  10.     <value>512</value>
  11.     <source>core-default.xml</source>
  12. </property>
  13. <property>
  14.     <name>ipc.server.tcpnodelay</name>
  15.     <value>false</value>
  16.     <source>core-default.xml</source>
  17. </property>
  18. <property>
  19.     <name>ftp.client-write-packet-size</name>
  20.     <value>65536</value>
  21.     <source>core-default.xml</source>
  22. </property>
  23. <property>
  24.     <name>nfs3.mountd.port</name>
  25.     <value>4272</value>
  26.     <source>core-site.xml</source>
  27. </property>
  28. </configuration>

举例

我们一般在wordcount程序中使用Configuration的set函数来添加或修改相关配置项,下面通过这种途径解析其具体实现方式。

  1. Configuration conf = new Configuration(true);

Configuration有3个构造函数:

  • 如果在新建Configuration对象时无参数,则系统默认调用该构造函数:
  1. /** A new configuration. */
  2. public Configuration() {
  3.   this(true);
  4. }
  • 如果在新建Configuration对象时有boolean类型形参,则调用该构造函数:
  1. /** A new configuration where the behavior of reading from the default 
  2.  * resources can be turned off.
  3.  * 
  4.  * If the parameter {@code loadDefaults} is false, the new instance
  5.  * will not load resources from the default files. 
  6.  * @param loadDefaults specifies whether to load from the default files
  7.  */
  8. public Configuration(boolean loadDefaults) {
  9. this.loadDefaults = loadDefaults;
  10.   updatingResource = new HashMap<String, String[]>();
  11. synchronized(Configuration.class) {
  12.     REGISTRY.put(thisnull);
  13.   }
  14. }
  • 如果在新建Configuration对象时有Configuration类型形参,则调用该构造函数:
  1. /** 
  2.  * A new configuration with the same settings cloned from another.
  3.  * 
  4.  * @param other the configuration from which to clone settings.
  5.  */
  6. @SuppressWarnings("unchecked")
  7. public Configuration(Configuration other) {
  8.  this.resources = (ArrayList<Resource>) other.resources.clone();
  9.  synchronized(other) {
  10.    if (other.properties != null) {
  11.      this.properties = (Properties)other.properties.clone();
  12.    }
  13.    if (other.overlay!=null) {
  14.      this.overlay = (Properties)other.overlay.clone();
  15.    }
  16.    this.updatingResource = new HashMap<String, String[]>(other.updatingResource);
  17.  }
  18.   this.finalParameters = new HashSet<String>(other.finalParameters);
  19.   synchronized(Configuration.class) {
  20.     REGISTRY.put(thisnull);
  21.   }
  22.   this.classLoader = other.classLoader;
  23.   this.loadDefaults = other.loadDefaults;
  24.   setQuietMode(other.getQuietMode());
  25. }

方法实现

可以详细查看Configuration类API或者源码。

给我留言

*

Copyright © If Coding 保留所有权利.   Theme  Ality   

用户登录