zipfileoutputformat未以.zip格式mapreduce提供输出

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

我有一个应用程序,可以读取hbase并将记录写入文件。最终输出应为.zip压缩格式,而不是hadoop支持的格式。为此,我使用自定义zipfileoutputformat来获取.zip文件中的记录。
这是我的实现

ZipFileOutputFormat.setOutputPath(job, new Path(args[1]));

这是zipfileoutputformat.class的详细信息

public class ZipFileOutputFormat extends FileOutputFormat<NullWritable, Text> {
    @Override
    public RecordWriter<NullWritable, Text> getRecordWriter(
            TaskAttemptContext job) throws IOException, InterruptedException {
        Path file = getDefaultWorkFile(job, ".zip");

        FileSystem fs = file.getFileSystem(job.getConfiguration());

        return new ZipRecordWriter(fs.create(file, false));
    }

    public static class ZipRecordWriter extends
            RecordWriter<NullWritable, Text> {
        protected ZipOutputStream zos;

        public ZipRecordWriter(FSDataOutputStream os) {
            zos = new ZipOutputStream(os);
        }

        @Override
        public void write(NullWritable key, Text value) throws IOException,
                InterruptedException {
            // TODO: create new ZipEntry & add to the ZipOutputStream (zos)
        }

        @Override
        public void close(TaskAttemptContext context) throws IOException,
                InterruptedException {
            zos.close();
        }
    }
}

我没有得到任何错误,但我的输出仍然在r-000001格式。
我是否缺少任何配置?

csga3l58

csga3l581#

我发现了问题。在job conf中设置了下面的内容之后,我的问题就被消除了

LazyOutputFormat.setOutputFormatClass(job, ZipFileOutputFormat.class);

在那之前

LazyOutputFormat.setOutputFormatClass(job, TextInputFormat.class);

同时设置

job.setOutputFormatClass(ZipFileOutputFormat.class);

相关问题