解压缩Hadoop hdfs目录中的所有Gzip文件

 媛媛天下_945 发布于 2023-01-06 20:27

在我的HDFS上,我有一堆gzip文件,我想要解压缩到正常格式.有没有这样做的API?或者我怎么能写一个函数来做到这一点?

我不想使用任何命令行工具; 相反,我想通过编写Java代码来完成这项任务.

1 个回答
  • 您需要一个CompressionCodec解压缩文件.gzip的实现是GzipCodec.您可以CompressedInputStream通过编解码器获得一个简单的IO结果.这样的事情:说你有一个文件file.gz

    //path of file
    String uri = "/uri/to/file.gz";
    Configuration conf = new Configuration();
    FileSystem fs = FileSystem.get(URI.create(uri), conf);
    Path inputPath = new Path(uri);
    
    CompressionCodecFactory factory = new CompressionCodecFactory(conf);
    // the correct codec will be discovered by the extension of the file
    CompressionCodec codec = factory.getCodec(inputPath);
    
    if (codec == null) {
        System.err.println("No codec found for " + uri);
        System.exit(1);
    }
    
    // remove the .gz extension
    String outputUri =
        CompressionCodecFactory.removeSuffix(uri, codec.getDefaultExtension());
    
    InputStream is = codec.createInputStream(fs.open(inputPath));
    OutputStream out = fs.create(new Path(outputUri));
    IOUtils.copyBytes(is, out, conf);
    
    // close streams
    

    UPDATE

    如果你需要获取目录中的所有文件,你应该得到FileStatus类似的东西

    FileSystem fs = FileSystem.get(new Configuration());
    FileStatus[] statuses = fs.listStatus(new Path("hdfs/path/to/dir"));
    

    然后循环

    for (FileStatus status: statuses) {
        CompressionCodec codec = factory.getCodec(status.getPath());
        ...
        InputStream is = codec.createInputStream(fs.open(status.getPath());
        ...
    }
    

    2023-01-06 20:30 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有