一.Configuration介绍
- Configuration是Hadoop的公共类,所以被放在了hadoop-common-2.7.4.jar下:org.apache.hadoop.conf.Configruration。该类是Job的配置信息类,配置信息的传递必须通过Configuration。因为通过Configuration可以实现在多个mapper和多个reducer任务间共享信息。
- Configuration实现了Iterable和Writable两个接口。因此它具有迭代功能,迭代Configuration对象中所有name-value键值对,加载到内存。实现Writable是为了实现Hadoop框架要求的序列化,可以将内存中的name-value序列化到硬盘。
二.Configuration分析
Configuration是一个配置信息类,它包含配置信息的加载,获取配置信息和加载配置信息等。类加载的过程是,先加载该类的静态代码块,再加载静态变量,然后才是构造方法。所以,我在分析的时候,先看静态代码块,然后再看构造方法,最后根据该类的主要功能去分析。
1.加载静态代码块
static{
//print deprecation warning if hadoop-site.xml is found in classpath
ClassLoader cL = Thread.currentThread().getContextClassLoader();
if (cL == null) {
cL = Configuration.class.getClassLoader();
}
if(cL.getResource("hadoop-site.xml")!=null) {
LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
"Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
+ "mapred-site.xml and hdfs-site.xml to override properties of " +
"core-default.xml, mapred-default.xml and hdfs-default.xml " +
"respectively");
}
addDefaultResource("core-default.xml");
addDefaultResource("core-site.xml");
}
在静态代码块中,我们看到Configuration静态代码块加载的时候,加载了默认资源"core-default.xml"和"core-site.xml"。我们看addDefaultResource(...)方法。
public static synchronized void addDefaultResource(String name) {
if(!defaultResources.contains(name)) {
defaultResources.add(name);
for(Configuration conf : REGISTRY.keySet()) {
if(conf.loadDefaults) {
conf.reloadConfiguration();
}
}
}
}
这个方法中将配置文件名添加到集合defaultResources中。其中loadDefaults是一个flag,标示是否要加载默认配置文件(默认true)。然后将集合REGISTORY中的Configuration对象(只要该对象加载默认配置)进行reloadConfiguration(),即:重新加载配置文件。这里出现了一个集合