公告:    欢迎来到王者★孤傲博客主页,若有问题敬请留言 !     没有大量的积累和感悟,是不会把事情做好的, 只有不停的进取,才能够不丢人! Copyright © 2016-2020 RaodiBlogs. All rights reserved.

readFile



import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
/***
 * 
 * @author
 *
 */
public class readFile {
    public static void main(String[] args) throws IOException {
        //创建Configuration实例
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://192.168.1.100:9000");
        //通过配置获取到文件系统
        FileSystem fs = FileSystem.get(conf);
        //定义要读取文件所在的HDFS路径
        Path src=new Path("hdfs://192.168.1.100:9000/user/hadoop/input/core-site.xml");
        //通过文件系统的open()方法得到一个文件输入流,用于读取
        FSDataInputStream dis = fs.open(src);
        //用IOUtils下的copyBytes将流中的数据打印输出到控制台
        IOUtils.copyBytes(dis, System.out, conf);
        //关闭输入流
        dis.close();
    }
}
posted @ 2019-06-19 20:29  王者★孤傲  阅读(1887)  评论(0编辑  收藏  举报
……