1.hadoop学习之org.apache.hadoop.conf

来源:互联网 发布:淘宝steam充值卡知乎 编辑:程序博客网 时间:2024/06/06 15:47


org.apache.hadoop.conf是hadoop common(core)模块的一个包,主要作用是读取配置文件

主要有三个类和接口,分别是

Configurable,
Configuration,
Configured.
   Configurable 【接口】
publicinterfaceConfigurable
{
函数成员:
 
    void setConf ( Configuration conf) ;
//接口函数,设置Configuration对象
 
Configuration   getConf() ;
 //接口函数,获取Configuration 对象。
 
}
 
 
 
1.2)Configuration 【类】
publicclassConfiguration
{
    变量成员:
    privatestaticfinalLogger LOG =
LogFormatter.getLogger("org.apache.hadoop.conf.Configuration");
 
privateArrayList defaultResources =newArrayList();
    privateArrayList finalResources =new ArrayList();
 
    privateProperties properties;
    privateClassLoader classLoader =
    Thread.currentThread().getContextClassLoader();
 
 
 
 
 
函数成员:
 
publicConfiguration()
{
1-  在本地的defaultResources列表中添加"hadoop-default.xml"
2-  在本地的finalResources列表中添加"hadoop-site.xml"
}
 
publicConfiguration(Configuration other)
{
1-  本地的defaultResources无条件设置为other相应的defaultResources的复制版本。
2-  本地的finalResources无条件设置为other相应的defaultResources的复制版本。
3-  只有当other的properties有效时,才把本地的properties设置为other的properties 的复制版本。
}
 
publicvoidaddDefaultResource(String name)
{
1-  defaultResources列表中添加name
2-  设置本地properties为null
}
 
publicvoidaddDefaultResource(File file)
{
1-  defaultResources里添加file
2-  设置本地properties为NULL
}
 
publicvoidaddFinalResource(String name)
{
1-  finalResources里添加name
2-  设置本地properties为NULL
}
 
publicvoidaddFinalResource(File file)
{
1-      finalResources里添加file
2-  设置本地properties为NULL
}
 
privatesynchronizedvoidaddResource(ArrayList resources, Object resource)
{
1-  往resources列表里添加resource
2-  设置本地properties为NULL
}
 
publicObject getObject(String name)
{
    直接从本地properties中查询name对应的对象Object.
通过方法get(name)
}
 
publicvoidsetObject(String name, Object value)
{
    直接put(name,value)放入properties中。
}
 
publicObject get(String name, Object defaultValue)
{
    如果properties中存在name对应的object(通过getObject函数),则返回查询结果
否则返回参数defaultValue.
}
 
publicString get(String name)
{
    直接从properties中查询getProperty(name)
}
 
publicvoidset(String name, Object value)
{
    直接对properties设置setProperty(name,value.toString());
}
 
publicString get(String name, String defaultValue)
{
    如果从properties中查询getProperty(name不为空,则返回结果
否则返回defaultValue.
}
 
publicintgetInt(String name, intdefaultValue)
{
尝试查询name对应的整数,失败则返回defaultValue.
 
}
 
publicvoidsetInt(String name, intvalue)
{
    在本地properties中加入(name,value).
}
 
publiclonggetLong(String name, longdefaultValue)
{
    尝试获取name对应的long,失败则返回defaultValue.
}
 
publicvoidsetLong(String name, longvalue)
{
     设置(name,value).
}
 
publicfloatgetFloat(String name,float defaultValue)
{
    尝试获取name对应的float,失败则返回defaultValue.
}
 
publicbooleangetBoolean(String name,boolean defaultValue)
{
    尝试获取name对应的boolean,失败则返回defaultValue.
}
 
publicvoidsetBoolean(String name,booleanvalue)
{
    往properties中添加(name,value);
}
 
publicString[] getStrings(String name)
{
    尝试将name通过", \t\n\r\f"来分割,结果作为数组返回。
}
 
publicClass getClass(String name, Class defaultValue)
{
     
尝试找到name对应的class,不存在的话则返回defaultValue.
}
 
publicClass getClass(String propertyName, Class defaultValue,Class xface)
{
    尝试找到propertyName对应的class,没有则赋值为defaultValue.
总之,这两者任意一个结果必须满足可以转化为xface类型。
 
}
 
publicURL getResource(String name)
{
    通过本地的classLoader找到name对应的URL
}
 
publicvoidsetClass(String propertyName, Class theClass, Class xface)
{
    在保证theClass可以转化为xface的前提下
设置(propertyName,theClass).
}
 
privatesynchronizedProperties getProps()
{
1-  如果本地properties未初始化,则基于defaultResources&finalResources来初始化properties.
2-  保证properties初始化后,返回properties.
}
 
privatevoidloadResources(Properties props,
                             ArrayList resources,
                             booleanreverse,booleanquiet)
{
    基于resources里的各个item,来赋值props.
}
 
privatevoidloadResource(Properties properties, Object name,boolean quiet)
{
    基于单个name指定的文件,来赋值props.
}
 
publicInputStream getConfResourceAsInputStream(String name)
{
    尝试做这样的事情
1-  name找到对应的URL
2-  URL.openStream().
}
 
publicReader getConfResourceAsReader(String name)
{
    尝试做这样的事情
1-  name找到对应的URL
2newInputStreamReader(url.openStream()).
3-  当然失败了,则返回NULL
 
}
 
privatevoidtoString(ArrayList resources, StringBuffer sb)
{
    遍历resources,将内容添加到sb中去
1-  文件,直接添加文件内容
2-  字符串,那就直接添加字符串吧。
}
 
publicString toString()
{
    格式如下:
Configuration:
+defaults:
+ defaultResources对应的字符串
+final:
+finalResources对应的字符串
 
}
 
publicvoidwrite(OutputStream out)throwsIOException
{
    遍历本地的properties 中的key,value.
过滤掉value不为 string类型的配置项。
合法的配置项通过out写。
}
 
publicFile getFile(String dirsProp, String path)throwsIOException {
1-  把dirsProp 分割成若干数组
2-  从数组中通过某个算法从某个下标(不一定是0,并且会保证遍历数组一遍)开始遍历数组每一项。
3-  当这个数组对应的文件夹存在或者可以被创建时,返回(选中的数组项,path)构成的file.
}
}
 
 
1.3)Configured【类】
PublicclassConfigured implementsConfigurable
{
    privateConfiguration conf;
 
函数成员如下:
publicvoidsetConf(Configuration conf)
{
    设置本地conf为参数conf指向的对象的引用
}
 
publicConfiguration getConf()
{
    返回本地的 conf
}
 
publicConfigured(Configuration conf)
{
    设置本地conf为参数conf指向的对象的引用
}
}
0 0
原创粉丝点击