Java读取HDFS数据

 1 import java.net.URI;  
 2   
 3 import org.apache.hadoop.conf.Configuration;  
 4 import org.apache.hadoop.fs.FSDataInputStream;  
 5 import org.apache.hadoop.fs.FileSystem;  
 6 import org.apache.hadoop.fs.Path;  
 7   
 8   
 9 public class FileReadFromHdfs {  
10   
11     public static void main(String[] args) {  
12         try {  
13         String dsf = "hdfs://hadoop1:9000/tmp/wordcount/kkk.txt";  
14         Configuration conf = new Configuration();  
15           
16         FileSystem fs = FileSystem.get(URI.create(dsf),conf);  
17         FSDataInputStream hdfsInStream = fs.open(new Path(dsf));  
18           
19         byte[] ioBuffer = new byte[1024];  
20         int readLen = hdfsInStream.read(ioBuffer);  
21         while(readLen!=-1)  
22         {  
23             System.out.write(ioBuffer, 0, readLen);  
24             readLen = hdfsInStream.read(ioBuffer);  
25         }  
26         hdfsInStream.close();  
27         fs.close();  
28         } catch (IOException e) {  
29             // TODO Auto-generated catch block  
30             e.printStackTrace();  
31         }  
32           
33     }  
34   
35 }  

 

posted @ 2014-07-02 22:20  IRIS我的天使  阅读(389)  评论(0编辑  收藏  举报