手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop加载自有xml资源文件

[日期:2014-10-23] 来源:Linux社区  作者:qingmu [字体:大 中 小]

在Hadoop中,使用configuration的时候,首先自动加载了默认的配置文件,比如core-default.xml、core-default.xml资源文件,代码如下:

static{
    //print deprecation warning if hadoop-site.xml is found in classpath
    ClassLoader cL = Thread.currentThread().getContextClassLoader();
    if (cL == null) {
      cL = Configuration.class.getClassLoader();
    }
    if(cL.getResource("hadoop-site.xml")!=null) {
      LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
          "Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
          + "mapred-site.xml and hdfs-site.xml to override properties of " +
          "core-default.xml, mapred-default.xml and hdfs-default.xml " +
          "respectively");
    }
    addDefaultResource("core-default.xml");
    addDefaultResource("core-site.xml");
  }

建立一个良好的Hadoop框架,势必会用很多自己写的资源文件,hadoop对xml支持好于对properties文件的支持,hadoop中的配置文件几乎都是是用xml写成的。那么如何加载自有的xml资源文件,使其成为全局的Configuration呢?

hadoop jar ‘你的jar包’ 之后跟随着一个-conf的命令,加载自有资源,靠的就是这个命令,ok,不废话了,上代码:

package com.ecom.asillin.utils;

import org.apache.hadoop.conf.Configuration;

/**
 * Created with IntelliJ IDEA.
 * User: asilin
 * Date: 14-10-23
 * Time: 上午10:17
 * To change this template use File | Settings | File Templates.
 */
public class ConfigurationUtils {

    //静态类单例
    private static class Singleton{
        public static ConfigurationUtils instance = new ConfigurationUtils();
    }

    private ConfigurationUtils(){}

    public static ConfigurationUtils getInstance(){
      return Singleton.instance;
    }

    //添加资源
    public static Configuration create(){
        Configuration conn = new Configuration();
        addSources(conn);
        return conn;
    }

    //添加默认资源  -conf 之后的资源
    private static Configuration addSources(Configuration conn){
        conn.addResource("你的xml文件名称,带有.xml,不要忘记");
        return conn;
    }
}

ok  现在完整的运行命令就是:  hadoop jar ‘a.jar’ -conf ‘yourself.xml’ 

Ubuntu 13.04上搭建Hadoop环境 http://www.linuxidc.com/Linux/2013-06/86106.htm

Ubuntu 12.10 +Hadoop 1.2.1版本集群配置 http://www.linuxidc.com/Linux/2013-09/90600.htm

Ubuntu上搭建Hadoop环境(单机模式+伪分布模式) http://www.linuxidc.com/Linux/2013-01/77681.htm

Ubuntu下Hadoop环境的配置 http://www.linuxidc.com/Linux/2012-11/74539.htm

单机版搭建Hadoop环境图文教程详解 http://www.linuxidc.com/Linux/2012-02/53927.htm

搭建Hadoop环境(在Winodws环境下用虚拟机虚拟两个Ubuntu系统进行搭建) http://www.linuxidc.com/Linux/2011-12/48894.htm

更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/108358.htm 

linux
相关资讯       Hadoop配置 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款