Hadoop 提供了自己的ARI org.apache.hadoop.conf.Configuration 用于管理系统配置.
Configuration提供了
addResource(String name)
addResource(URL url)
addResource(Path file)
addResource(InputStream in)
等方法用于以不同的形式加载配置,Hadoop会根据不同的形式构建Resource.由于Hadoop使用延迟加载的方式进行配置的加载,所以addResource方法并不会直接将资源加载到内存,而是存入私用变量resources中
public void addResource(String name) { addResourceObject(new Resource(name)); }
private synchronized void addResourceObject(Resource resource) { resources.add(resource); // add to resources restrictSystemProps |= resource.isParserRestricted(); reloadConfiguration(); }
reloadConfiguration会将已经加载的properties清空,在真正获取properties时,会判断properties是否为空,以此来判断是否需要执行加载,同时还会将finalParameters列表清空,finalParameters里存放的是标记为final的属性的key
public synchronized void reloadConfiguration() { properties = null; // trigger reload finalParameters.clear(); // clear site-limits }
在加载属性时,如果试图加载final的熟悉,会执行checkForOverride方法,在日志中打印warning信息
if (!finalParameters.contains(attr)) { properties.setProperty(attr, value); if (source != null) { putIntoUpdatingResource(attr, source); } } else { // This is a final parameter so check for overrides. checkForOverride(this.properties, name, attr, value); if (this.properties != properties) { checkForOverride(properties, name, attr, value); } }
private void checkForOverride(Properties properties, String name, String attr, String value) { String propertyValue = properties.getProperty(attr); if (propertyValue != null && !propertyValue.equals(value)) { LOG.warn(name + ":an attempt to override final parameter: " + attr + "; Ignoring."); } }
Resource实现了isParserRestricted(),如果存在isParserRestricted的资源,在Configuration执行substituteVars时,不会使用Java虚拟机的系统属性
Configuration同时也提供了
addResource(String name, boolean restrictedParser)
addResource(URL url, boolean restrictedParser)
addResource(Path file, boolean restrictedParser)
addResource(InputStream in, boolean restrictedParser)
等方法重载
substituteVars为Configuration提供了属性拓展的功能,在执行get方法获取属性值时,会调用substituteVars方法,substituteVars方法会根据正则表达式对属性进行替换,将${key}这种格式的变量转换成这个key对应的值,由于可能出现两个key,key1的key为${key2},key2的key为${key1},这种情况,所以Configuration定义了MAX_SUBST用于决定拓展的最大次数,默认为20
Hadoop会在真正获取属性时对资源进行加载
protected synchronized Properties getProps() { if (properties == null) { properties = new Properties(); Map<String, String[]> backup = updatingResource != null ? new ConcurrentHashMap<String, String[]>(updatingResource) : null; loadResources(properties, resources, quietmode); if (overlay != null) { properties.putAll(overlay); if (backup != null) { for (Map.Entry<Object, Object> item : overlay.entrySet()) { String key = (String) item.getKey(); String[] source = backup.get(key); if (source != null) { updatingResource.put(key, source); } } } } } return properties; }
Configuration会在启动时执行静态方法加载默认资源
static { // Add default resources addDefaultResource("core-default.xml"); addDefaultResource("core-site.xml"); // 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("hadoop-site.xml"); } }
Configuration的常用私有属性如下:
quietmode: boolean类型,默认TRUE,用于设置加载配置的默认,如果为TRUE,在设置时不会现在日志信息,如果需要调试应用,可以设置为FALSE
classLoader:Configuration的类加载器
finalParameters:不可变配置的keyset
restrictSystemProps: boolean类型,默认FALSE,判断执行substituteVars属性拓展时,是否可以从系统配置中执行拓展
allowNullValueProperties: boolean类型,默认FALSE,是否允许空值,如果=值为空并且允许控制,默认值为private static final String DEFAULT_STRING_CHECK = "testingforemptydefaultvalue";
resources:所有加载的资源列表
properties: 解析后的属性
overlay:通过set方法设置的属性
loadDefaults:boolean类型,默认TRUE,判断是否加载默认资源
defaultResources:默认资源列表,在static方法中执行addDefaultResource时将默认资源加入到这个list中
Hadoop提供了Configurable类用于设置对象所需要的属性
public interface Configurable { /** Set the configuration to be used by this object. */ void setConf(Configuration conf); /** Return the configuration used by this object. */ Configuration getConf(); }
原文:https://www.cnblogs.com/tyler-jin/p/10423745.html