通过java读取HDFS的数据

来源:互联网 发布:php 怎么去掉html标签 编辑:程序博客网 时间:2024/06/09 20:59
[java] view plaincopy
  1. 通过JAVA直接读取HDFS中的时候,一定会用到FSDataInputStream类,通过FSDataInputStream以流的形式从HDFS读数据代码如下:import java.io.IOException;  
  2. import java.net.URI;  
  3.   
  4. import org.apache.hadoop.conf.Configuration;  
  5. import org.apache.hadoop.fs.FSDataInputStream;  
  6. import org.apache.hadoop.fs.FileSystem;  
  7. import org.apache.hadoop.fs.Path;  
  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. }  


通过JAVA直接读取HDFS中的时候,一定会用到FSDataInputStream类,通过FSDataInputStream以流的形式从HDFS读数据

代码如下:
0 0
原创粉丝点击