您的位置:首页 > 编程语言 > Java开发

java读取HDFS的数据

2015-10-06 14:51 686 查看
通过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 blocke.printStackTrace();}}}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: