阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Hadoop加载自有xml资源文件

119次阅读
没有评论

共计 2147 个字符,预计需要花费 6 分钟才能阅读完成。

在 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

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计2147字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中