- String:在classpath中寻找
- path: 直接从本地文件系统中查找
Configuration类无处不在,初学Hadoop的人都知道配置参数是在core-site.xml文件中进行的,所以这个类的目的也就明确了,默认情况下hadoop的Configuration类会按照顺序加载两个文件,文件的路径是通过classpath得到的(这里的classpath是指应用运行的类路径。服务端hadoop的classpath指向的是conf。客户端,classpath就是客户端应用的类路径src):
1、core-default.xml: hadoop的只读文件 位于src/core/文件夹中
2、core-site.xml: 指定hadoop安装过程需要的一些参数配置
xml文件中:资源以xml形式的数据表示,由一系列的键值对组成。资源可以用String或path命名,String-指示hadoop在classpath中查找该资源;Path-指示hadoop在本地文件系统中查找该资源。
注:也可以项目下新建folder :conf。将build.xml放入conf中。1、右键conf -> build path ->use as souce folder 2、 右键conf -> build path -> configue build path .进入到 libreries 选项卡中,选择 Add Class Folder 。在弹出的对话框中将选择conf文件夹。单击确定。总的方针是将 build.xml 添加到类路径中。
例子:
build.xml文件如下:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>lishuwei</name>
<value>blue</value>
</property>
</configuration>
程序代码演示了两中方法的使用:
package com.lianglei;
import org.apache.hadoop.conf.Configuration;
public class VtesXml {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration cong = new Configuration();
// cong.addResource(new Path("/home/lloppo/workspace/Test_hbase/conf/build.xml")); //采用的是绝对路径
cong.addResource("build.xml");
System.out.println(cong.get("lishuwei"));
}
}