如果用eclipse 连接hadoop测试 一定要把core-site.xml hdfs-site.xml 放到和包test同目录下 不然会报错程序会报File not found错误,并且加载相对应版本的的hadoop-core-xx.jar包
点击(此处)折叠或打开
- package test;
- import java.io.*;
- import java.util.*;
- import java.net.*;
- import org.apache.hadoop.fs.*;
- import org.apache.hadoop.conf.*;
- import org.apache.hadoop.io.*;
- import org.apache.hadoop.mapred.*;
- import org.apache.hadoop.util.*;
-
- public class Cat{
- public static void main (String [] args) throws Exception{
- try{
- Path pt=new Path("hdfs://10.14.2.201:9000/user/hadoop/words.txt");
- //Path pt=new Path("hdfs://npvm11.np.wc1.yellowpages.com:9000/user/john/abc.txt");
- FileSystem fs = FileSystem.get(new Configuration());
- BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
- String line;
- line=br.readLine();
- long sum = 0L;
- while (line != null){
- sum ++;
- System.out.println(line+"sum: "+sum);
- line=br.readLine();
- }
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }