Configuration的主要是加载配置文件,并储存在properties中。

细节内容不重复了,主要参考Hadoop技术内幕,Hadoop源代码,以及:

http://blog.csdn.net/zhoubangtao/article/details/25977561

  • 整个资源的加载流程如下:

  • 过程中遇到的问题:

    • Java Io 学习:

      • http://blog.csdn.net/yczz/article/details/38761237
    • Document Builder使用:
      • http://blog.csdn.net/redarmy_chen/article/details/12912065
      • http://docs.oracle.com/javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html
package com.baidu.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.lang.System;
import java.util.Objects;
import java.util.Properties;
import java.net.URL; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException; public class Configuration { private Properties properties; /**
* 判断参数是否为空,并对url进行处理
* @param builder
* @param url
* @return
* @throws IOException
* @throws SAXException
*/
private Document parse(DocumentBuilder builder, URL url)
throws IOException, SAXException {
if (url == null) {
return null;
}
return parse(builder, url.openStream(), url.toString());
} /**
* 判断参数是否为空,实际调用Documentbuilder parse 函数
* @param builder
* @param is
* @param systemId
* @return
* @throws IOException
* @throws SAXException
*/
private Document parse(DocumentBuilder builder, InputStream is,
String systemId) throws IOException, SAXException {
if (is == null) {
return null;
}
try {
return (systemId == null) ? builder.parse(is) : builder.parse(is,
systemId);
} finally {
is.close();
}
} /**
* 获取 properties 对应key的value
* @param key
* @return
*/
public String get(String key) {
return properties.getProperty(key);
} public void addResource(String source_name) {
properties = new Properties();
loadResource(properties, source_name);
} /**
* 通过DocumentBuilder来解析xml文件, resource 直接使用了 Object 来代替,
* 实际就是一个字符串
* @param properties 储存的prpoerties
* @param resource xml文件的绝对路径
*/
public void loadResource(Properties properties, Object resource) {
try {
DocumentBuilderFactory docBuilderFactory
= DocumentBuilderFactory.newInstance(); //ignore all comments inside the xml file
docBuilderFactory.setIgnoringComments(true); //allow includes in the xml file
docBuilderFactory.setNamespaceAware(true);
try {
docBuilderFactory.setXIncludeAware(true);
} catch (UnsupportedOperationException e) {
System.out.println("Failed to set setXIncludeAware(true) for parser");
}
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
Document doc = null;
Element root = null; if (resource instanceof String) { // a file resource
// Can't use FileSystem API or we get an infinite loop
// since FileSystem uses Configuration AP
//
//I. Use java.io.File instead.
File file = new File(((String)resource)).getAbsoluteFile();
if (file.exists()) {
doc = parse(builder, new BufferedInputStream(
new FileInputStream(file)), ((String)resource).toString());
}
} root = doc.getDocumentElement(); NodeList props = root.getChildNodes(); for (int i = 0; i < props.getLength(); i++) {
Node propNode = props.item(1); Element prop = (Element)propNode; if ("configuration".equals(prop.getTagName())) {
loadResource(properties, prop.getTagName());
} NodeList fields = prop.getChildNodes();
String attr = null;
String value = null; for (int j = 0; j < fields.getLength(); j++) {
Node fieldNode = fields.item(j);
if (!(fieldNode instanceof Element))
continue;
Element field = (Element)fieldNode;
if ("name".equals(field.getTagName()) && field.hasChildNodes())
attr = ((Text)field.getFirstChild()).getData().trim(); if ("value".equals(field.getTagName()) && field.hasChildNodes())
value = ((Text)field.getFirstChild()).getData().trim();
}
//System.out.println("name is " + attr);
//System.out.println("value is " + value);
properties.setProperty(attr, value);
} } catch (IOException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
} public static void main(String args[]) {
//System.out.println("Hello");
Configuration c = new Configuration();
c.addResource("test.xml"); String value = c.get("yarn.nodemanager.aux-services");
System.out.println(value);
} }

Hadoop Configuration的更多相关文章

  1. Hadoop学习日志- install hadoop

    资料来源 : http://www.tutorialspoint.com/hadoop/hadoop_enviornment_setup.htm Hadoop 安装 创建新用户 $ su passwo ...

  2. Hadoop 学习笔记3 Develping MapReduce

    小笔记: Mavon是一种项目管理工具,通过xml配置来设置项目信息. Mavon POM(project of model). Steps: 1. set up and configure the ...

  3. Hadoop入门进阶课程13--Chukwa介绍与安装部署

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan  ...

  4. Hadoop配置文件解析

    Hadoop源码解析 2 --- Hadoop配置文件解析 1 Hadoop Configuration简介    Hadoop没有使用java.util.Properties管理配置文件, 也没有使 ...

  5. Hadoop学习笔记: sqoop配置与使用

    sqoop即SQL-to-Hadoop,是一个把数据从关系型数据库导入到Hadoop系统中的工具(HDFS,HIVE和HBase),也可以将数据从Hadoop导入到关系型数据库.本文以sqoop 1. ...

  6. hadoop文件系统FileSystem详解 转自http://hi.baidu.com/270460591/item/0efacd8accb7a1d7ef083d05

    Hadoop文件系统 基本的文件系统命令操作, 通过hadoop fs -help可以获取所有的命令的详细帮助文件. Java抽象类org.apache.hadoop.fs.FileSystem定义了 ...

  7. (转)单机上配置hadoop

    哈哈,几天连续收到百度两次电话,均是利好消息,于是乎不知不觉的自己的工作效率也提高了,几天折腾了好久终于在单机上配置好了hadoop,然后也成功的运行了一个用例,耶耶耶耶耶耶. 转自:http://w ...

  8. 大数据平台搭建(hadoop+spark)

    大数据平台搭建(hadoop+spark) 一.基本信息 1. 服务器基本信息 主机名 ip地址 安装服务 spark-master 172.16.200.81 jdk.hadoop.spark.sc ...

  9. Steps to Install Hadoop on CentOS/RHEL 6---reference

    http://tecadmin.net/steps-to-install-hadoop-on-centosrhel-6/# The Apache Hadoop software library is ...

随机推荐

  1. Intent (一)

    1,简介 Intent 是一种消息传递机制,可以理解为一种对消息的封装,执行某操作的抽象描述,可用于应用程序内部及应用程序之间 其组成包括: 要执行的动作(action) 如VIEW_ACTION(查 ...

  2. HDU 3501 Calculation 2 (欧拉函数)

    题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eula ...

  3. Oracle 6 - 锁和闩 - transaction的可串行化

    本文主要内容 1.transaction的可串行化 2.数据库并发带来的问题, dirty read, Nonrepeatable reads, Phantoms幻读 3.隔离级别和2中的问题 4. ...

  4. Oracle 体系结构2 - 共享和专用服务器

    1. 怎么查看自己的oracle是共享还是专用服务器 2. 怎么修改设置 3.各有什么优缺点 4.适用环境 对于专用服务器,每一个数据库连接,oracle都会分配一个专门的进程为其服务 oracle@ ...

  5. 网络上下载的Ghost系统含威胁

  6. Android中使用HTTP和HttpClient进行通信

    /** * 使用HTTP的Get方式进行数据请求 */ protected void httpGet() { /** * 进行异步请求 */ new AsyncTask<String, Void ...

  7. A. Counting Kangaroos is Fun(贪心)

    #include<stdio.h> #include<algorithm> using namespace std; ]; int main() { int i,n,high; ...

  8. ibatis动态查询

    在复杂查询过程中,我们常常需要根据用户的选择决定查询条件,这里发生变化的并不只是SQL 中的参数,包括Select 语句中所包括的字段和限定条件,都可能发生变化.典型情况,如在一个复杂的组合查询页面, ...

  9. mysql级联更新的两种方式:触发器更新和外键

    1.mysql级联更新有两种方式:触发器更新和外键更新. 2.触发器更新和外键更新的目的都是为了保证数据完整性. 我们通常有这样的需求:删除表Table 1中记录,需要同时删除其它表中与Table 1 ...

  10. C++:对象指针

    对象指针概念:每一个对象在初始化后都会在内存中占有一定的空间.因此,既可以通过对象名访问, 也可以通过一个对象地址来访问一个对象.对象指针就是用于存放对象地址的变量. 声明对象指针的一般语法格式为:类 ...