Hadoop源码解析之Configuration类
说在前面的话
JDK提供了jara.util.Properties
类,用于处理配置文件,它继承自Hashtable
表示了一个持久的属性集,该集可保存在流中或从流中加载。属性列表中每个键及其对应值都是字符串类型。
jara.util.Properties
支持使用反射来get
或set
值。
Hadoop没有使用java.util.Properties管理配置文件,也没有使用Apache Jakarta Commons Configuration管理配置文件,而是使用了一套独有的配置文件管理系统,并提供自己的API,即使用 org.apache.hadoop.conf.Configuration处理配置信息。
Hadoop配置文件的格式解析
- <?xml version="1.0"?>
- <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
- <configuration>
- <property>
- <name>io.sort.factor</name>
- <value>10</value>
- <description>The number of streams to merge at once while sorting
- files. This determines the number of open file handles.</description>
- </property>
- <property>
- <name>dfs.name.dir</name>
- <value>${hadoop.tmp.dir}/dfs/name</value>
- <description>Determines where on the local filesystem the DFS name
- nodeshould store the name table(fsimage). ……</description>
- </property>
- <property>
- <name>dfs.web.ugi</name>
- <value>webuser,webgroup</value>
- <final>true</final>
- <description>The user account used by the web interface.
- Syntax: USERNAME,GROUP1,GROUP2, ……</description>
- </property>
- </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。
- Configurationconf = new Configuration();
- conf.addResource("core-default.xml");
- 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则会调用默认配置文件部分结果如下:
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <configuration>
- <property>
- <name>ipc.client.fallback-to-simple-auth-allowed</name>
- <value>false</value>
- <source>core-default.xml</source>
- </property>
- <property>
- <name>file.bytes-per-checksum</name>
- <value>512</value>
- <source>core-default.xml</source>
- </property>
- <property>
- <name>ipc.server.tcpnodelay</name>
- <value>false</value>
- <source>core-default.xml</source>
- </property>
- <property>
- <name>ftp.client-write-packet-size</name>
- <value>65536</value>
- <source>core-default.xml</source>
- </property>
- <property>
- <name>nfs3.mountd.port</name>
- <value>4272</value>
- <source>core-site.xml</source>
- </property>
- </configuration>
举例
我们一般在wordcount程序中使用Configuration的set函数来添加或修改相关配置项,下面通过这种途径解析其具体实现方式。
- Configuration conf = new Configuration(true);
Configuration有3个构造函数:
- 如果在新建Configuration对象时无参数,则系统默认调用该构造函数:
- /** A new configuration. */
- public Configuration() {
- this(true);
- }
- 如果在新建Configuration对象时有boolean类型形参,则调用该构造函数:
- /** A new configuration where the behavior of reading from the default
- * resources can be turned off.
- *
- * If the parameter {@code loadDefaults} is false, the new instance
- * will not load resources from the default files.
- * @param loadDefaults specifies whether to load from the default files
- */
- public Configuration(boolean loadDefaults) {
- this.loadDefaults = loadDefaults;
- updatingResource = new HashMap<String, String[]>();
- synchronized(Configuration.class) {
- REGISTRY.put(this, null);
- }
- }
- 如果在新建Configuration对象时有Configuration类型形参,则调用该构造函数:
- /**
- * A new configuration with the same settings cloned from another.
- *
- * @param other the configuration from which to clone settings.
- */
- @SuppressWarnings("unchecked")
- public Configuration(Configuration other) {
- this.resources = (ArrayList<Resource>) other.resources.clone();
- synchronized(other) {
- if (other.properties != null) {
- this.properties = (Properties)other.properties.clone();
- }
- if (other.overlay!=null) {
- this.overlay = (Properties)other.overlay.clone();
- }
- this.updatingResource = new HashMap<String, String[]>(other.updatingResource);
- }
- this.finalParameters = new HashSet<String>(other.finalParameters);
- synchronized(Configuration.class) {
- REGISTRY.put(this, null);
- }
- this.classLoader = other.classLoader;
- this.loadDefaults = other.loadDefaults;
- setQuietMode(other.getQuietMode());
- }
方法实现
可以详细查看Configuration类API或者源码。