用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

相關文章
相關標籤/搜索