Hadoop Configuration配置类的分析

本文主要探讨了Hadoop Common模块中的配置组件,特别是Configuration类。分析了如何通过继承Configurable接口实现配置,并揭示了Configuration类中集合变量的作用。文章提到了配置文件如hdfs-site.xml的格式,并详细阐述了get属性获取操作的实现,指出其并非简单的getProps().get(name)操作,尤其是在处理特定结构时。最后,作者强调了配置类代码设计的精巧性,适合在大型系统开发中借鉴。
摘要由CSDN通过智能技术生成

          学习Hadoop Common模块,当然应该是从最简单,最基础的模块学习最好,所以我挑选了其中的conf配置模块进行学习。整体的类结构非常简单。


只要继承了Configurable接口,一般表明就是可配置的,可以执行相应的配置操作,但是配置的集中操作的体现是在Configuration这个类中。这个类中定义了很多的集合变量:

/**
   * List of configuration resources.
   */
  private ArrayList<Object> resources = new ArrayList<Object>();

  /**
   * List of configuration parameters marked <b>final</b>. 
   * finalParameters集合中保留的是final修饰的不可变的参数
   */
  private Set<String> finalParameters = new HashSet<String>();

  /**
   * 是否加载默认资源配置
   */
  private boolean loadDefaults = true;
  
  /**
   * Configuration objects
   * Configuration对象
   */
  private static final WeakHashMap<Configuration,Object> REGISTRY = 
    new WeakHashMap<Configuration,Object>();
  
  /**
   * List of default Resources. Resources are loaded in the order of the list 
   * entries
   */
  private static final CopyOnWriteArrayList<String> defaultResources =
    new CopyOnWriteArrayList<String>();
上面只是列举出了一部分,基本的用途都是拿来保存一些资源的数据。还有一个变量比较关键:

//资源配置文件中的属性会加载到Properties属性中来
  private Properties properties;
所有的属性变量都是存放到java中的Properties中存放,便于后面的直接存取。Property其实就是一个HashTable。我们按着Configuration加载的顺序来学习一下他的整个过程。首先当然是执行初始化代码块:

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");
  }
学习过java构造函数的执行顺序的同学,应该知道初始化代码块中的代码的执行顺序是先于构造函数的,所以会执行完上面的操作,就来到了addDefaultResource():

/**
   * 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 addDefaultResourc
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值