Hadoop loads self-owned xml resource files
In Hadoop, when using configuration, the default configuration file, such as the core-default.xml, core-default.xml resource file, is automatically loaded first, the Code is as follows:
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 ");
}
To build a good Hadoop framework, many resource files written by yourself are bound to be used. hadoop supports xml better than properties files, and almost all configuration files in hadoop are written in xml. So how can I load my xml resource file to make it a global Configuration?
Hadoop jar 'your jar package 'is followed by a-conf command to load its own resources. It depends on this command. OK, no nonsense. Go to the Code:
Package com. ecom. asillin. utils;
Import org. apache. hadoop. conf. Configuration;
/**
* Created with IntelliJ IDEA.
* User: asilin
* Date: 14-10-23
* Time: AM
* To change this template use File | Settings | File Templates.
*/
Public class ConfigurationUtils {
// Static class Singleton
Private static class Singleton {
Public static ConfigurationUtils instance = new ConfigurationUtils ();
}
Private ConfigurationUtils (){}
Public static ConfigurationUtils getInstance (){
Return Singleton. instance;
}
// Add resources
Public static Configuration create (){
Configuration conn = new Configuration ();
AddSources (conn );
Return conn;
}
// Add resources after default resource-conf
Private static Configuration addSources (Configuration conn ){
Conn. addResource ("your xml file name, with. xml, do not forget ");
Return conn;
}
}
Now the complete running command is: hadoop jar 'a. jar'-conf 'yourself. xml'
Build a Hadoop environment on Ubuntu 13.04
Cluster configuration for Ubuntu 12.10 + Hadoop 1.2.1
Build a Hadoop environment on Ubuntu (standalone mode + pseudo Distribution Mode)
Configuration of Hadoop environment in Ubuntu
Detailed tutorial on creating a Hadoop environment for standalone Edition
Build a Hadoop environment (using virtual machines to build two Ubuntu systems in a Winodws environment)