如果用eclipse 连接hadoop测试 一定要把core-site.xml  hdfs-site.xml 放到和包test同目录下 不然会报错程序会报File not found错误,并且加载相对应版本的的hadoop-core-xx.jar包

点击(此处)折叠或打开

  1. package test;
  2. import java.io.*;
  3. import java.util.*;
  4. import java.net.*;
  5. import org.apache.hadoop.fs.*;
  6. import org.apache.hadoop.conf.*;
  7. import org.apache.hadoop.io.*;
  8. import org.apache.hadoop.mapred.*;
  9. import org.apache.hadoop.util.*;
  10.  
  11. public class Cat{
  12.         public static void main (String [] args) throws Exception{
  13.                 try{
  14.                         Path pt=new Path("hdfs://10.14.2.201:9000/user/hadoop/words.txt");
  15.                         //Path pt=new Path("hdfs://npvm11.np.wc1.yellowpages.com:9000/user/john/abc.txt");
  16.                         FileSystem fs = FileSystem.get(new Configuration());
  17.                         BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
  18.                         String line;
  19.                         line=br.readLine();
  20.                         long sum = 0L;
  21.                         while (line != null){
  22.                                 sum ++;
  23.                                 System.out.println(line+"sum: "+sum);
  24.                                 line=br.readLine();
  25.                         }
  26.                 }catch(Exception e){
  27.                     e.printStackTrace();
  28.                 }
  29.         }
  30. }

12-20 21:51