因为项目需要将配置文件打包到jar包,然后再MR job中读取资源。
我以读取"hadoop-core-0.20.2-cdh3u2.jar"中core-default.xml配置文件举例,代码如下:
- public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException {
- InputStream in = FileSystem.class.getClass().getResourceAsStream("/core-default.xml");
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String s= "";
- while((s= reader.readLine()) != null){
- System.out.println(s);
- }
- }
我这里引用了FileSystem.class的路径,其实只要是在hadoop-core-0.20.2-cdh3u2.jar包中任何一个class都可以。
参考:http://www.iteye.com/topic/483115