文本文件在hdfs中压缩不正确

jslywgbw  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(284)

我有一个 .txt 我想把这个文件压缩到.gz中,然后上传到hdfs中的某个位置。
下面是我尝试的代码:

String codecClassName = args[1];
    String source = args[2];
    String dest = args[3];

    InputStream in = new BufferedInputStream(new FileInputStream(source));
    Class<?> codecClass = Class.forName(codecClassName);

    Configuration conf = new Configuration();
    CompressionCodec codec = (CompressionCodec)ReflectionUtils.newInstance(codecClass, conf);

    FileSystem fs = FileSystem.get(URI.create(dest),conf);
    OutputStream out = fs.create(new Path(dest),new Progressable() {

        @Override
        public void progress() {
            System.out.println(".");
        }
    });

    CompressionOutputStream outStream = codec.createOutputStream(out);

    IOUtils.copyBytes(in, outStream, 4096,false);

以下是此代码中传递的参数值:
arg1(压缩机名称): org.apache.hadoop.io.compress.GzipCodec arg2(本地驱动器中的位置): /home/user/Demo.txt arg3(hdfs中的位置): hdfs://localhost:8020/user/input/Demo.gz 当我运行这个代码时 Demo.gz 正在上述hdfs位置创建文件,但.gz文件的大小为0mb。
请让我知道为什么文件没有得到压缩和上传到hdfs正确。

fnatzsnv

fnatzsnv1#

你好像没有关闭溪流。您有两种选择:
通过将true作为第四个参数传递给copybytes来自动关闭它们
手动关闭,例如。 outStream.close()

相关问题