Hadoop 七:Hadoop-Hdfs源码 conf包

一.conf包下四个类

 

二.详细描述

  1.  Configurable接口:Something that may be configured with a {@link Configuration}.,很绕口,简单理解为Hdfs系统配置文件的接口。
  2. Configured:Base class for things that may be configured with a {@link Configuration},Hdfs系统配置文件的抽象类。持有Configuration对象的应用。
  3. Configured:配置文件资源管理类。用DOM解析xml配置文件;默认加载资源core-default.xml,core-site.xml;只有在get(key)时才去加载资源文件。
  4. ConfServlet:A servlet to print out the running configuration data。
    Java代码   收藏代码
    1. @Override  
    2. public void doGet(HttpServletRequest request, HttpServletResponse response)  
    3.     throws ServletException, IOException {  
    4.   String format = request.getParameter("format");  
    5.   if (null == format) {  
    6.     format = "xml";  
    7.   }  
    8.   
    9.   if ("xml".equals(format)) {  
    10.     response.setContentType("text/xml; charset=utf-8");  
    11.   } else if ("json".equals(format)) {  
    12.     response.setContentType("application/json; charset=utf-8");  
    13.   }  
    14.   
    15.   Writer out = response.getWriter();  
    16.   try {  
    17.     out.write(getServletContext().getAttribute("hadoop.conf");)  
    18.   } catch (BadFormatException bfe) {  
    19.     response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());  
    20.   }  
    21.   out.close();  
    22. }  

 

三.Configuration代码 

Java代码   收藏代码
  1. //1.静态代码块,把默认资源放到CopyOnWriteArrayList<String> defaultResources里。  
  2.  static{  
  3.     //print deprecation warning if hadoop-site.xml is found in classpath  
  4.     ClassLoader cL = Thread.currentThread().getContextClassLoader();  
  5.     if (cL == null) {  
  6.       cL = Configuration.class.getClassLoader();  
  7.     }  
  8.     if(cL.getResource("hadoop-site.xml")!=null) {  
  9.       LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +  
  10.           "Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "  
  11.           + "mapred-site.xml and hdfs-site.xml to override properties of " +  
  12.           "core-default.xml, mapred-default.xml and hdfs-default.xml " +  
  13.           "respectively");  
  14.     }  
  15.     addDefaultResource("core-default.xml");  
  16.     addDefaultResource("core-site.xml");  
  17.   }  
  18.   
  19. public static synchronized void addDefaultResource(String name) {  
  20.     if(!defaultResources.contains(name)) {  
  21.       defaultResources.add(name);  
  22.       for(Configuration conf : REGISTRY.keySet()) {  
  23.         if(conf.loadDefaults) {  
  24.           conf.reloadConfiguration();  
  25.         }  
  26.       }  
  27.     }  
  28.   }  
  29.   
  30. //2.初始化代码块,把该实例放入WeakHashMap<Configuration,Object>里。  
  31. public Configuration(boolean loadDefaults) {  
  32.     this.loadDefaults = loadDefaults;  
  33.     updatingResource = new HashMap<String, String>();  
  34.     synchronized(Configuration.class) {  
  35.       REGISTRY.put(thisnull);  
  36.     }  
  37.   }  
  38.   
  39. //3.第一次get(key)时,才开始解析资源,保存到Properties properties里。  
  40. public String get(String name) {  
  41.     return substituteVars(getProps().getProperty(name));  
  42.   }  
  43.   
  44. private synchronized Properties getProps() {  
  45.     if (properties == null) {  
  46.       properties = new Properties(); //这个时候才初始化properties  
  47.       loadResources(properties, resources, quietmode);  
  48.       if (overlay!= null) {  
  49.         properties.putAll(overlay);  
  50.         for (Map.Entry<Object,Object> item: overlay.entrySet()) {  
  51.           updatingResource.put((String) item.getKey(), UNKNOWN_RESOURCE);  
  52.         }  
  53.       }  
  54.     }  
  55.     return properties;  
  56.   }  
  57.   
  58. private void loadResource(Properties properties, Object name, boolean quiet) {  
  59.   //DOM解析资源,保存到properties里。  
  60.   //需要注意的时,解析的是代码运行环境下的core-default.xml,core-site.xml。  
  61. }  

 

四.Configuration使用例子 

 

Java代码   收藏代码
  1. Configuration conf = new Configuration();  
  2. FileSystem fs = FileSystem.get(URI, conf);  
  1.  第一段代码:按照三里的12步先静态代码块,再初始化。
  2. 第二段代码,创建FileSystem时,会调用conf.getBoolean(key)方法获取所需参数,此时第一次加载资源,后续还会调用conf.get(key),直接从properties取就行了。
  3. 若上述两段代码在eclipse运行,必须在eclipse classpath下配置core-default.xml,core-site.xml。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值