Hadoop加载自有资源文件

本文介绍了Hadoop中的配置机制,特别是如何加载自定义的XML配置文件并使其成为全局配置的一部分。文章提供了具体的代码示例,展示了如何通过Configuration类来添加自定义资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

<span style="font-family:Microsoft YaHei;font-size:14px;">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");
  }</span>

建立一个良好的hadoop框架,势必会用很多自己写的资源文件,hadoop对xml支持好于对properties文件的支持,hadoop中的配置文件几乎都是是用xml写成的。那么如何加载自有的xml资源文件,使其成为全局的Configuration呢?如果需要hdfs-site.xml或者mapred-site.xml中配置,需要自行加载,可使用Configuration.addDefaultResource("hdfs-site.xml");

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

<span style="font-family:Microsoft YaHei;font-size:14px;">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;
    }
}
</span>
ok  现在完整的运行命令就是:  hadoop jar ‘a.jar’ -conf ‘yourself.xml’   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值