首页 > 代码库 > Hadoop加载自有资源文件

Hadoop加载自有资源文件

在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呢?

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’   


Hadoop加载自有资源文件