如何在hdfs上使用java压缩文件

polhcujo  于 2021-06-02  发布在  Hadoop
关注(0)|答案(1)|浏览(427)

这个问题在这里已经有答案了

hadoop:在hdfs中压缩文件(7个答案)
6年前关门了。
我是hdfs/hadoop新手,需要知道如何压缩hdfs目录中的文件hdfs://sandbox:8020/部分/路径。
我试过了

  1. Path p = new Path("/my/path/test1.gz");
  2. FSDataOutputStream os = fs.create(p);
  3. GZIPOutputStream gzipOs = new GZIPOutputStream(new BufferedOutputStream(os));
  4. Path filePath = file.getPath();
  5. FSDataInputStream is = fs.open(filePath);
  6. System.out.println("Writing gzip");
  7. byte[] buffer = new byte[1024];
  8. int len;
  9. while((len= is.read(buffer)) != -1){
  10. gzipOs.write(buffer, 0, len);
  11. }
  12. //close resources
  13. is.close();
  14. gzipOs.close();

但它不起作用。
有什么建议吗?提前谢谢。

lfapxunr

lfapxunr1#

下面的代码来自汤姆·怀特的权威指南。

  1. public class StreamCompressor {
  2. public static void main(String[] args) throws Exception {
  3. String codecClassname = args[0];
  4. Class<?> codecClass = Class.forName(codecClassname);
  5. Configuration conf = new Configuration();
  6. CompressionCodec codec = (CompressionCodec)
  7. ReflectionUtils.newInstance(codecClass, conf);
  8. CompressionOutputStream out = codec.createOutputStream(System.out);
  9. IOUtils.copyBytes(System.in, out, 4096, false);
  10. out.finish();
  11. }
  12. }

相关问题