通过java读取HDFS的数据 (转)

原文链接:通过java读取HDFS的数据

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

import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;


public class FileReadFromHdfs {

    public static void main(String[] args) {
        try {
        String dsf = "hdfs://hadoop1:9000/tmp/wordcount/kkk.txt";
        Configuration conf = new Configuration();
        
        FileSystem fs = FileSystem.get(URI.create(dsf),conf);
        FSDataInputStream hdfsInStream = fs.open(new Path(dsf));
        
        byte[] ioBuffer = new byte[1024];
        int readLen = hdfsInStream.read(ioBuffer);
        while(readLen!=-1)
        {
            System.out.write(ioBuffer, 0, readLen);
            readLen = hdfsInStream.read(ioBuffer);
        }
        hdfsInStream.close();
        fs.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

}

参考链接:

hadoop java操作hdfs

Java创建hdfs文件实例

利用HDFS、HBase的JavaAPI编程

 

posted @ 2015-11-24 19:03  ~风轻云淡~  阅读(6493)  评论(0编辑  收藏  举报