2021SC@SDUSC
本次我将对hadoop中的common部分中Configuration部分进行分析
首先Configuration类实现了Writable和lterable接口,使得hadoop可以遍历以及自己序列化
配置相关文件格式
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>lala</name>
<value>${user.home}/hadoopdata</value>
<final>true</final>
</property>
</configuration>
一般而言hadoop是通过xml来进行配置的,并且支持属性的扩展,user.home当被调用get的时候,会首先通过System.getProperty()判断是否是系统参数,上图中${user.home}就被替换成当前用户自己的path。所以当我们配置hadoop环境时,通过直接使用一些系统属性,可以让配置的hadoop具有更强的效能。
Configuration属性清单:
Log是记录日志的对象。quietmode对应的是配置信息加载过程中是否属于静默的模式,如果处于静默的模式下,则在配置信息加载的过程中的一些信息不会被记录在日志中,默认情况是设置为true的。resources是一个对象数组,用于存放有关包含配置信息的对象。finalParameter是所有配置值被声明为final的变量的一个集合。loadDefault从表面字段上可以理解为是否要加载默认的配置。REGISTRY是一个WeakHashMap,用于多有个对象的相关配置的注册对它们进行管理,弱哈希可以自动清除不在正常使用的键对应的条目。defaultResources是一个CopyOnWrite的字符串数组,用于存储默认的配置资源名或者路径。{…}是一个静态初始化块,用于加载默认的配置资源。properties存储的是Configuration对象中的全部配置信息,它的类型是Properties的,这个类型是Java提供的对KV配置的一个属性集,提高了对KV配置参数的存储和操作方法。overlay则是进行覆盖的属性。classLoader主要是用于配置冲根据配置的参数构造相应的对象实例时提供上下文环境的类加载器。varPat是一个对含有环境变量的值的进行转换的正则表达式对象,比如我们设定的一个路径变量的值为$HOME/data,那么这个变量就会以一定的规则把该变量的值分为字串$HOME和/data,之后会把$HOME解析成系统上的家目录了。MAX_SUBST是设定对带有环境变量的值所能够深入解析的层次数,超出这个最大的层数的值将不能够解析。
代码分析
内部私有类Resource
private static class Resource {
private final Object resource;
private final String name;
private final boolean restrictParser;
public Resource(Object resource) {
this(resource, resource.toString());
}
public Resource(Object resource, boolean useRestrictedParser) {
this(resource, resource.toString(), useRestrictedParser);
}
...
内部私有类DeprecatedKeyInfo、DeprecationDelta、DeprecationContext
private static class DeprecatedKeyInfo {
private final String[] newKeys;
private final String customMessage;
private final AtomicBoolean accessed = new AtomicBoolean(false);
DeprecatedKeyInfo(String[] newKeys, String customMessage) {
this.newKeys = newKeys;
this.customMessage = customMessage;
}
public static class DeprecationDelta {
private final String key;
private final String[] newKeys;
private final String customMessage;
DeprecationDelta(String key, String[] newKeys, String customMessage) {
Preconditions.checkNotNull(key);
Preconditions.checkNotNull(newKeys);
Preconditions.checkArgument(newKeys.length > 0);
this.key = key;
this.newKeys = newKeys;
this.customMessage = customMessage;
}
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、提示封装在一起。