使用java.net.URL对象从Hadoop文件系统读取文件

实现类似linux中cat命令的程序

文件名

HDFSCat.java

程序代码

import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

public class HDFSCat {
    public static void main(String[] args) {
        InputStream in = null;
        try {
            in = new URL(args[0]).openStream();
            IOUtils.copyBytes(in, System.out, 4096, false);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(in);
        }
    }

    static {
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
    }
}

编译

javac HDFSCat

运行

hadoop HDFSCat hdfs://localhost:9000/user/hadoop/in.txt

说明

  • 需要运行在配置hadoop的linux系统上
  • 编译前,需要设置CLASSPATH 点击查看
  • 运行命令中的 hdfs://localhost:9000是在hadoop/etc/hadoop/core-site.xml文件中fs.defaultFSvalue

相关文章:

  • 2021-08-12
  • 2021-12-22
  • 2022-03-01
  • 2021-12-09
  • 2021-10-15
  • 2022-01-20
  • 2022-01-29
  • 2022-12-23
猜你喜欢
  • 2021-05-30
  • 2021-05-23
  • 2021-08-24
  • 2021-06-17
  • 2021-06-25
  • 2022-01-05
  • 2022-01-22
相关资源
相似解决方案