查看Configuration的源码可以看到文件加载了这两个配置,所以在集群中,如果直接 Configuration conf = new Configuration(); 不给conf配置的话,会默认去读取DefaultResource这两个参数的内容
1 2
| addDefaultResource("core-default.xml"); addDefaultResource("core-site.xml");
|
当我们有这个Configuration的时候,我们可以手动设置里面的某些参数了,设置方法:
我在当前jia包下建立了一个myconfig.xml,内容如下,我放在集群的hadoop的配置文件下 $HADOOP_HOME/etc/hadoop下;
1 2 3 4 5 6 7 8 9
| <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration> <property> <name>zks</name> <value>people</value> <description>this used test hadoop configuation</description> </property> </configuration>
|
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.myhadoop.configuation;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.mapreduce.Job; import java.io.IOException; public class HadoopConfiguation { public static void main(String[] args) {
//默认加载$HADOOP_HOME/etc/hadoop下的core-default.xml,core-site.xml Configuration conf = new Configuration(); //输出集群的信息看看 System.out.println("fs.defaultFS: "+conf.get("fs.defaultFS"));
//自定义加载myconfig.xml conf.addResource("myconfig.xml"); String str=conf.get("zks"); System.out.println("myconfig.xml: "+str);
conf.set("zks","aaaaa"); System.out.println(conf.get("zks"));
//随便设置一个 conf.set("hello","it's me"); System.out.println(conf.get("hello")); } }
|
打包执行然后输出:
1 2 3 4 5
| hadoop jar hadoopconfig.jar fs.defaultFS: hdfs://master:9000 myconfig.xml: people aaaaa it's me
|
Configuration可以通过Content传递参数给map或者reduce
1 2
| Configuration conf = context. getConfiguration(); String str = conf.get("参数名");
|