资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

hadoop-common中Configuration的示例代码

这篇文章主要为大家展示了“ hadoop-common中Configuration的示例代码 ”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“ hadoop-common中Configuration的示例代码 ”这篇文章吧。

创新互联专注于西平企业网站建设,响应式网站开发,商城建设。西平网站建设公司,为西平等地区提供建站服务。全流程定制开发,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

Configuration类实现了Iterable、Writable接口,使得可以遍历和序列化(hadoop自己序列化)

配置文件格式

lala${user.home}/hadoopdatatrue

hadoop是通过xml进行配置的,同时支持属性扩展,user.home当调用get的时候,会首先通过System.getProperty()判断是否是系统参数,例中${user.home}就被替换成当前用户的path。所以,当我们在配置hadoop时,可以直接用一些系统属性,增强可移植性。
当一个属性被生命为final时,后面添加配置,不会覆盖先加在的配置。
同时,因为使用的是java的DOM解析,所以支持XML的包涵,在配置文件中可以用来分类管理。

代码分析

私有内部类Resource

  private static class Resource {
  //私有内部类,标记资源名字和资源对象private final Object resource;private final String name;
    ...
    }

私有内部类DeprecatedKeyInfo、DeprecationDelta、DeprecationContext

  private static class DeprecatedKeyInfo {private final String[] newKeys;private final String customMessage;private final AtomicBoolean accessed = new AtomicBoolean(false);private final String getWarningMessage(String key) {
     }
  }  public static class DeprecationDelta {private final String key;private final String[] newKeys;private final String customMessage;
  }  private static class DeprecationContext {//存放oldkey-newkeysprivate final Map deprecatedKeyMap;//存放newkeys-oldkey,提供反查功能private final Map reverseDeprecatedKeyMap;
    DeprecationContext(DeprecationContext other, DeprecationDelta[] deltas) {
    ...this.deprecatedKeyMap = UnmodifiableMap.decorate(newDeprecatedKeyMap);this.reverseDeprecatedKeyMap =UnmodifiableMap.decorate(newReverseDeprecatedKeyMap);
    }
  }

DeprecatedKeyInfo保存了新的key和信息,如果customMessage为空,在调用getWarningMessage会自动生成默认的信息。
DeprecationDelta 保存了被遗弃的key 和 建议用的新key。
DeprecationContext封装讲被遗弃的key和推荐使用的keys、提示封装在一起。

  private static AtomicReference deprecationContext =      new AtomicReference(          new DeprecationContext(null, defaultDeprecations));

一个全局的DeprecationContext对象,原子的,并且将默认被遗弃的key加载进去。

静态addDeprecations方法

值得一提的是此方法很巧妙的使用无锁的方法,但是,保证了数据的安全性,看具体代码:

  public static void addDeprecations(DeprecationDelta[] deltas) {
    DeprecationContext prev, next;
    do {
      prev = deprecationContext.get();
      next = new DeprecationContext(prev, deltas);
    } while (!deprecationContext.compareAndSet(prev, next));
  }

compareAndSet方法是当前对象和prev相等(==)时,更新当前对象为next

setDeprecatedProperties

分析源码,我们发现,setDeprecatedProperties的作用就是为了更新overlay和properties,使得,我们在获得key时,能得到最新的状态,看下面例子:

  configuration.addDeprecation("xx", new String[]{"xx1","xx2","xx3"});  //configuration.setDeprecatedProperties();
  System.out.println(configuration.get("xx"));

当注释掉configuration.setDeprecatedProperties后,我get时,获得的事null值,所以我们要遍历已经被遗弃的key时,需要更新setDeprecatedProperties,可以使得被遗弃的key依旧可以被使用。

handleDeprecation

首先判断该key是否是被遗弃的,如果是,将得到建议用的key,否则更新overlay、properties,并返回建议使用的key数组。

用样handleDeprecation方法是,执行刷新操作。具体用在asXmlDocument中。

static{}静态代码块

分析代码我们可以得到一下几点:

  1. 如果在classpath下存在hadoop-site.xml,会log4j会打印警告信息,没有加载到defaultResources。

  2. 默认加载两个核心配置文件core-default.xml、core-site.xml

addResourceObject以及若干方法

不管用何种addResource,最终都是调用了addResourceObject(Resource resource),他首先将资源添加到一个全局的List集合,然后调用reloadConfiguration来触发刷新properties并且标记为final的key失效。

findSubVariable substituteVars

在hadoop-2.7之前,只有一个substituteVars方法,使用java自身的正则表达式来匹配获得${user.home }中间的值(user.home)。

hadoop-2.7版本之后,为了提升性能,自己实现了匹配获取中间值的方法( findSubVariable ) ps:可能是因为,由于java自身的正则表达式方式过于消耗性能,所以,通过自己手动匹配,降低性能的消耗。

  //此方法,将字符串中${}中间的位置的区间获取到,详细看代码
  private static int[] findSubVariable(String eval) {
        ...
 }  //1.将获取key进行替换,如果System.getProperty()存在,替换
  //2.不存在,查找properties,如果存在替换,不存在,原样保留
  private String substituteVars(String expr) {
    ...
  }

set方法

  //通过程序设置key-value,source允许为空,当用户不设置源时,程序自动将programatically这是为source,
  //当值为被遗弃的,此方法会先将新key的到,并设置source为 because old key is deprecated
 public void set(String name, String value, String source) {
     ...
 }

loadResource

该方法是解析xml的,采用了DOM解析,分析代码我们知道,xml格式需要和上面写到的格式,同时DOM解析,支持xml文件引入。

和以前版本相比,xml配置文件中,在property中可以声明source标签,声明资源的信息?

 if ("source".equals(field.getTagName()) && field.hasChildNodes())
  source.add(StringInterner.weakIntern(
      ((Text)field.getFirstChild()).getData()));

以上是“ hadoop-common中Configuration的示例代码 ”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


分享标题:hadoop-common中Configuration的示例代码
标题链接:http://cdkjz.cn/article/gjhseh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220