首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Hadoop加载自有xml资源文件

Hadoop加载自有xml资源文件

作者头像
星哥玩云
发布于 2022-07-03 13:55:51
发布于 2022-07-03 13:55:51
1K0
举报
文章被收录于专栏:开源部署开源部署

在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’ 

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档