博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hadoop Configuration
阅读量:6388 次
发布时间:2019-06-23

本文共 5690 字,大约阅读时间需要 18 分钟。

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);    }}

 

转载于:https://www.cnblogs.com/SpeakSoftlyLove/p/5384377.html

你可能感兴趣的文章
Sublime-text gitHub 问题收集
查看>>
UML 类图
查看>>
Unity Remote 无法连接
查看>>
linux下core file size设置笔记
查看>>
mysql 、redis的区别
查看>>
使用JPA中@Query 注解实现update 操作
查看>>
7.4. APC Cache (php-apc - APC (Alternative PHP Cache) module for PHP 5)
查看>>
Web 仪表盘
查看>>
我的Fedora 7硬盘安装过程
查看>>
18.4. FAQ
查看>>
Python——SSHClient.py
查看>>
MVC解决更新冲突问题
查看>>
江西理工大学南昌校区cool code竞赛
查看>>
[LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树
查看>>
Ubuntu SDL lib 安装
查看>>
Java 并发编程内部分享PPT分享
查看>>
关于discuz中禾金投票系统循环出现引导页的问题
查看>>
C#开源系统大汇总
查看>>
Linux服务器安全初始化自选安装Shell脚本
查看>>
PyCharm教程
查看>>