深入理解hadoop(一)----Common的实现----Configuration

属本人个人原创,转载请注明,希望对大家有帮助!!

一,hadoop的配置管理

a,hadoop通过独有的Configuration处理配置信息

Configuration conf = new Configuration();
conf.addResource("core-default.xml");
conf.addResource("core-site.xml");

后者会覆盖前者中 未 final标记的相同配置项


b,hadoop属性扩展

如果配置项 dfs.name.dir的值是 ${hadoop.tmp.dir} /dfs/name, hadoop.tmp.dir的值是 /data,

则结果为/data/dfs/name


c,Configuration 成员变量分析

	//如果为true,加载配置文件的过程中,不输出日志信息
	boolean quietmode;    
	//保存通过addResources()方法添加的资源
	ArrayList resources;   
	//保存配置文件中已经被声明为final的键-值对
	Set finalParameters;  
	//是否加载默认资源
	boolean loadDefaults ;  
	// 保存默认资源 列如HDFS 中 把hdfs-default.xml和 hdfs-site.xml作为默认资源
	static ArrayList defaultResources ;   
	//保存 hadoop配置文件解析后的键-值对
	Properties properties ;    
	//记录通过set()方式改变的配置项
	Properties overlay ;          
	//类变量加载器,通过它来加载指定类
	ClassLoader classLoader ;  
  

public URL getResource(String name){
   return classLoader.getResource(name);
} 

//如果为true,加载配置文件的过程中,不输出日志信息
	boolean quietmode;    
	//保存通过addResources()方法添加的资源
	ArrayList resources;   
	//保存配置文件中已经被声明为final的键-值对
	Set finalParameters;  
	//是否加载默认资源
	boolean loadDefaults ;  
	// 保存默认资源 列如HDFS 中 把hdfs-default.xml和 hdfs-site.xml作为默认资源
	static ArrayList defaultResources ;   
	//保存 hadoop配置文件解析后的键-值对
	Properties properties ;    
	//记录通过set()方式改变的配置项
	Properties overlay ;          
	//类变量加载器,通过它来加载指定类
	ClassLoader classLoader ; 
	
	
	/**
	 * 资源通过addResource()方法或者静态addDefaultResource()方法添加到Configuration中,添加的资源并不会立即被加载
	 * 只是通过reloadConfiguration()方法清空properties 和finalParameters代码如下
	 */
	public void addResource(String name){
		addResourceObject(name);
	}

	private synchronized void addResourceObject(Object resource) {
		//添加到成员变量resources中
		resources.add(resource);
		reloadConfiguration();
	}

	private synchronized void reloadConfiguration() {
		properties = null;
		finalParameters.clear();
	} 
  
	/**
	 * 实际载入资源
	 */
	private synchronized Properties getProps(){
		if(properties==null){
			properties = new Properties();
			loadResources(properties,resources,quietmode);
		}
		
		return properties;
	}

	private void loadResources(Properties properties,
			Object name, boolean quietmode) {
		try{
			//得到用于创建DOM解析器的工厂
			DocumentBuilderFactory documentBuilderFactory = 
				DocumentBuilderFactory.newInstance();
			
			//忽略XML注释
			documentBuilderFactory.setIgnoringComments(true);
			
			//提供对XML名称空间的支持
			documentBuilderFactory.setNamespaceAware(true);
			
			try{
				//允许XInclude机制
				documentBuilderFactory.setXIncludeAware(true);
			}catch (Exception e) {
				
			}
			
			//获取解析XML的DocumentBuilder对象
			DocumentBuilder builder = 
				documentBuilderFactory.newDocumentBuilder();
			
			Document doc = null;
			Element root = null;
			
			//根据不同的资源,做不同的处理
			
			if(name instanceof URL){
				
			}else if(name instanceof InputStream){
				
			}else if(name instanceof Element){
				root = (Element) name;
			}
		}catch (Exception e) {
			// TODO: handle exception
		}	
		
	}




你可能感兴趣的:(深入理解----hadoop)