设为首页 收藏本站
查看: 1184|回复: 0

[经验分享] Hadoop配置文件解析

[复制链接]

尚未签到

发表于 2015-7-12 10:17:26 | 显示全部楼层 |阅读模式
Hadoop源码解析 2 --- Hadoop配置文件解析

  1 Hadoop Configuration简介
    Hadoop没有使用java.util.Properties管理配置文件,
也没有使用Apache Jakarta Commons
Configuration管理配置文件,而是使用了一套独有的配置文件管理系统,并提供自己的API,即使用
org.apache.hadoop.conf.Configuration处理配置信息。
      org.apache.hadoop.conf目录结构如下:
DSC0000.jpg
  
2 Hadoop配置文件的格式解析

    Hadoop配置文件采用XML格式,下面是Hadoop配置文件的一个例子:



   



io.sort.factor
10
The number of streams to merge at once while sorting  
files.  This determines the number of open file handles.


dfs.name.dir
${hadoop.tmp.dir}/dfs/name
Determines where on the local filesystem the DFS name  
nodeshould store the name table(fsimage).  ……


dfs.web.ugi
webuser,webgroup
true
The user account used by the web interface.  
Syntax: USERNAME,GROUP1,GROUP2, ……


      Hadoop配置文件的根元素是configuration,一般只包含子元素property。每一个property元素就是一个配置 项,配置文件不支持分层或分级。每个配置项一般包括配置属性的名称name、值value和一个关于配置项的描述description;元素final 和Java中的关键字final类似,意味着这个配置项是“固定不变的”。final一般不出现,但在合并资源的时候,可以防止配置项的值被覆盖。
    在
上面的示例文件中,配置项dfs.web.ugi的值是“webuser,webgroup”,它是一个final配置项;从description看,
这个配置项配置了Hadoop Web界面的用户账号,包括用户名和用户组信息。这些信息可以通过Configuration类提供的方法访问。
    在
Configuration中,每个属性都是String类型的,但是值类型可能是以下多种类型,包括Java中的基本类型,如
boolean(getBoolean)、int(getInt)、long(getLong)、float(getFloat),也可以是其他类型,如
String(get)、java.io.File(getFile)、String数组(getStrings)等。以上面的配置文件为
例,getInt("io.sort.factor")将返回整数10;而getStrings("dfs.web.ugi")返回一个字符串数组,该数
组有两个元素,分别是webuser和webgroup。
    合并资源指将多个配置文件合并,产生一个配置。如果有两个配置文件,也就是两个资源,如core-default.xml和core-site.xml,通过Configuration类的loadResources()方法,把它们合并成一个配置。代码如下:
    Configurationconf = new Configuration();  
    conf.addResource("core-default.xml");  
    conf.addResource("core-site.xml");
    如
果这两个配置资源都包含了相同的配置项,而且前一个资源的配置项没有标记为final,那么,后面的配置将覆盖前面的配置。上面的例子中,core-
site.xml中的配置将覆盖core-default.xml中的同名配置。如果在第一个资源(core-default.xml)中某配置项被标记
为final,那么,在加载第二个资源的时候,会有警告提示。
  3 直接运行Configuration.java则会调用默认配置文件部分结果如下:






ipc.client.fallback-to-simple-auth-allowed
false
core-default.xml


file.bytes-per-checksum
512
core-default.xml


ipc.server.tcpnodelay
false
core-default.xml


ftp.client-write-packet-size
65536
core-default.xml


nfs3.mountd.port
4272
core-site.xml


  4 我们一般在wordcount程序中使用Configuration的set函数来添加或修改相关配置项,下面通过这种途径解析其具体实现方式
  4.1 Configuration conf = new Configuration(true)的具体实现如下(见4.1.2):
      Configuration有3个构造函数:
      4.1.1 如果在新建Configuration对象时无参数,则系统默认调用该构造函数



    public Configuration() {
this(true);
}
      4.1.2 如果在新建Configuration对象时有boolean类型形参,则调用该构造函数



    /**
* 1 新建一个Configuration类,如果loadDefaults=false,
* 则新建的Configuration实例默认不会加载默认的配置文件
*/
public Configuration(boolean loadDefaults) {
System.out.println("Configuration(boolean loadDefaults)");
this.loadDefaults = loadDefaults;// 选择是否加载默认配置文件,false为不加载,true加载
System.out.println("loadDefaults: " + loadDefaults);
updatingResource = new HashMap();// 保存修改过的配置项
synchronized (Configuration.class) {
REGISTRY.put(this, null);
}
}
      4.1.3 如果在新建Configuration对象时有Configuration类型形参,则调用该构造函数



    /**
*
* @param 调用其它Configuration对象的配置文件
*/
@SuppressWarnings("unchecked")
public Configuration(Configuration other) {
this.resources = (ArrayList) other.resources.clone();
synchronized (other) {
if (other.properties != null) {
this.properties = (Properties) other.properties.clone();
}
if (other.overlay != null) {
this.overlay = (Properties) other.overlay.clone();
}
this.updatingResource = new HashMap(
other.updatingResource);
}
this.finalParameters = new HashSet(other.finalParameters);
synchronized (Configuration.class) {
REGISTRY.put(this, null);
}
this.classLoader = other.classLoader;
this.loadDefaults = other.loadDefaults;
setQuietMode(other.getQuietMode());
}
  4.2 conf.set("fs.defaultFS", "file///");
      set函数有:
          public void set(String name, String value, String source)
          public void set(String name, String value)
          public synchronized void setIfUnset(String name, String value)
          public void setInt(String name, int value)
          public void setLong(String name, long value)
          public void setFloat(String name, float value)
          public void setDouble(String name, double value)
          public void setBoolean(String name, boolean value)
          public void setBooleanIfUnset(String name, boolean value)
          public  void setEnum(String name, T value)
          public void setTimeDuration(String name, long value, TimeUnit unit)
          public void setPattern(String name, Pattern pattern)
          public void setStrings(String name, String... values)
          public void setStrings(String name, String... values)
          public void setClass(String name, Class theClass, Class xface)
      其中,后面的set相关函数都是调用第一个set函数实现,下面就具体解析一下public void set(String name, String value, String source)



    /**
*
* @Title        set
* @Description  将参数name对应的value存入property中,如果该name在property中存在则覆盖,否则添加
* @param
* @return
* @throws
*/
public void set(String name, String value, String source) {
System.out.println("set(name, value, source) start !");
Preconditions.checkArgument(name != null, "Property name must not be null");
Preconditions.checkArgument(value != null, "The value of property " + name + " must not be null");
DeprecationContext deprecations = deprecationContext.get();//保存不在配置文件的key
System.out.println("deprecations: "+deprecations);
System.out.println("deprecations.getDeprecatedKeyMap().isEmpty(): "+deprecations.getDeprecatedKeyMap().isEmpty());
if (deprecations.getDeprecatedKeyMap().isEmpty()) {
getProps();
}
getOverlay().setProperty(name, value);
getProps().setProperty(name, value);
String newSource = (source == null ? "programatically" : source);
System.out.println("newSource: " + newSource);
if (!isDeprecated(name)) {//检测该name(key)项是否在配置文件中存在
            
System.out.println("!isDeprecated(name): " + !isDeprecated(name));
updatingResource.put(name, new String[] { newSource });//将该name(key)项参数添加进updatingResource中,说明该项已被修改
String[] altNames = getAlternativeNames(name);//判断该name(key)是否在默认配置文件中存在,如果存在则将name存入altNames中
/**
* 如果name(key)则默认配置文件中存在,则将name对应value存入updatingResource
*/
if (altNames != null) {
for (String n : altNames) {
System.out.println("altNames: "+n);
if (!n.equals(name)) {
getOverlay().setProperty(n, value);
getProps().setProperty(n, value);
updatingResource.put(n, new String[] { newSource });
}
}
}
} else {
String[] names = handleDeprecation(deprecationContext.get(), name);
String altSource = "because " + name + " is deprecated";
for (String n : names) {
System.out.println("names: "+names);
getOverlay().setProperty(n, value);
getProps().setProperty(n, value);
updatingResource.put(n, new String[] { altSource });
}
}
}
  5 Configuration测试程序如下:



/**  
* @Title        ConfigurationTest.java
* @Package      org.apache.hadoop.conftest
* @Description  TODO
* @date         2014年9月11日 上午11:27:14
* @version      V1.0
*/
package org.apache.hadoop.conftest;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
public class ConfigurationTest {
public static void main(String args[]){
Configuration conf = new Configuration(true);
Path hadoop_mapred = new Path("hadoop-2.3.0/etc/hadoop/mapred-site.xml");
Path hadoop_yarn = new Path("hadoop-2.3.0/etc/hadoop/yarn-site.xml");
conf.addResource(hadoop_mapred);
conf.addResource(hadoop_yarn);
conf.set("mapreduce.jobtracker.system.dir", "file:///data1");//this conf can change the same parameter in the mapred-site.xml when the paramter is used
conf.setInt("test1", 10);//This parameter will be add to property due to it not in the properties
conf.set("fs.defaultFS", "file///data");//This parameter will change the same parameter value in the properties
System.out.println(conf.get("test1"));
System.out.println(conf.get("mapreduce.jobtracker.system.dir"));
System.out.println(conf.get("yarn.resourcemanager.admin.address"));
System.out.println("ok");
}
}
  
  原创文章欢迎转载,转载时请注明出处。
    作者推荐文章:
      》Java自学之道
      》总结5种比较高效常用的排序算法
      》如何获取系统信息
  
    》如何生成二维码过程详解
      百度云盘下载地址 http://pan.baidu.com/s/1eQzSiEA

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-85714-1-1.html 上篇帖子: HADOOP 存储图片方案------------准备工作 下篇帖子: Hadoop单机模式配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表