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();
    }
}

相关文章:

  • 2022-12-23
  • 2021-10-15
  • 2021-07-05
  • 2022-12-23
  • 2021-06-09
  • 2022-02-25
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2021-10-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2022-12-23
相关资源
相似解决方案