Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support compression for output in MR v1. #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,39 @@
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.BytesWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.io.compress.GzipCodec;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RecordWriter;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.util.Progressable;
import org.apache.hadoop.util.ReflectionUtils;
import org.tensorflow.hadoop.util.TFRecordWriter;

import java.io.DataOutputStream;
import java.io.IOException;

public class TFRecordFileOutputFormatV1 extends FileOutputFormat<BytesWritable, Writable> {
@Override
public RecordWriter<BytesWritable, Writable> getRecordWriter(FileSystem ignored,
JobConf job, String name,
Progressable progress) throws IOException {
Path file = FileOutputFormat.getTaskOutputPath(job, name);
boolean isCompressed = getCompressOutput(job);
CompressionCodec codec = null;
String extension = "";
if (isCompressed) {
Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class);
codec = ReflectionUtils.newInstance(codecClass, job);
extension = codec.getDefaultExtension();
}
Path file = FileOutputFormat.getTaskOutputPath(job, name + extension);
FileSystem fs = file.getFileSystem(job);

int bufferSize = TFRecordIOConf.getBufferSize(job);
final FSDataOutputStream fsdos = fs.create(file, true, bufferSize);
FSDataOutputStream fsDataOutputStream = fs.create(file, true, bufferSize);
final DataOutputStream fsdos = isCompressed ?
new DataOutputStream(codec.createOutputStream(fsDataOutputStream)) : fsDataOutputStream;
final TFRecordWriter writer = new TFRecordWriter(fsdos);
return new RecordWriter<BytesWritable, Writable>() {
@Override
Expand All @@ -55,5 +67,4 @@ public void close(Reporter reporter)
}
};
}

}