用java从hadoop中读数据

如果用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();
                }
        }
}

你可能感兴趣的:(用java从hadoop中读数据)