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");
}
//初始化中加載默認配置文件,core-site是用戶的屬性定義
//如果有相同,後者的屬性會覆蓋前者的屬性
addDefaultResource("core-default.xml");
addDefaultResource("core-site.xml");
}
/**
* Add a default resource. Resources are loaded in the order of the resources
* added.
* @param name file name. File should be present in the classpath.
*/
public static synchronized void addDefaultResource(String name) {
if(!defaultResources.contains(name)) {
defaultResources.add(name);
//遍歷注冊過的資源配置,進行重新加載操作
for(Configuration conf : REGISTRY.keySet()) {
if(conf.loadDefaults) {
conf.reloadConfiguration();
}
}
}
}
/**
* Reload configuration from previously added resources.
*
* This method will clear all the configuration read from the added
* resources, and final parameters. This will make the resources to
* be read again before accessing the values. Values that are added
* via set methods will overlay values read from the resources.
*/
public synchronized void reloadConfiguration() {
//重新加載Configuration就是重新將裡面的屬性記錄清空
properties = null; // trigger reload
finalParameters.clear(); // clear site-limits
}
/** A new configuration. */
public Configuration() {
//初始化是需要加載默認資源的
this(true);
}
然後繼續調用重載函數:
/** 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;
if (LOG.isDebugEnabled()) {
LOG.debug(StringUtils.stringifyException(new IOException("config()")));
}
synchronized(Configuration.class) {
//加載過的Configuration對象對會加入到REGISTRY集合中
REGISTRY.put(this, null);
}
this.storeResource = false;
}
dfs.name.dir/var/local/hadoop/hdfs/nameDetermines where on the local filesystem the DFS name node
should store the name table. If this is a comma-delimited list
of directories then the name table is replicated in all of the
directories, for redundancy. truedfs.data.dir/var/local/hadoop/hdfs/dataDetermines where on the local filesystem an DFS data node
should store its blocks. If this is a comma-delimited
list of directories, then data will be stored in all named
directories, typically on different devices.
Directories that do not exist are ignored.
true
.......
/**
* Set the value of the name property.
*
* @param name property name.
* @param value property value.
* 根據name設置屬性值,屬性鍵值對保存在property中
*/
public void set(String name, String value) {
getOverlay().setProperty(name, value);
getProps().setProperty(name, value);
}
/**
* Get the value of the name property, null if
* no such property exists.
*
* Values are processed for variable expansion
* before being returned.
*
* @param name the property name.
* @return the value of the name property,
* or null if no such property exists.
*/
public String get(String name) {
return substituteVars(getProps().getProperty(name));
}