我需要在 Hadoop 作业中处理和操作许多图像,输入将通过网络进行,使用 MultiThreadedMapper
下载速度较慢。
但是减少输出的最佳方法是什么?我认为我应该将原始二进制图像数据写入序列文件,将这些文件传输到它们的最终位置,然后编写一个小应用程序将 SequenceFile
中的各个图像提取到各个 JPG 和 GIF 中。
或者有更好的选择可以考虑吗?
最佳答案
如果你觉得可以(或者通过谷歌搜索你可以找到一个实现),你可以编写一个 FileOutputFormat ,它用 ZipOutputStream 包装一个 FSDataOutputStream 。 ,为每个 reducer 提供一个 Zip 文件(从而节省您编写 seq 文件提取程序的精力。
不要被编写自己的 OutputFormat 吓倒,它实际上并不那么困难(并且比编写必须担心拆分的自定义 InputFormat 容易得多)。事实上,这是一个起点 - 您只需要实现 write 方法:
// Key: Text (path of the file in the output zip)
// Value: BytesWritable - binary content of the image to save
public class ZipFileOutputFormat extends FileOutputFormat<Text, BytesWritable> {
@Override
public RecordWriter<Text, BytesWritable> 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<Text, BytesWritable> {
protected ZipOutputStream zos;
public ZipRecordWriter(FSDataOutputStream os) {
zos = new ZipOutputStream(os);
}
@Override
public void write(Text key, BytesWritable value) throws IOException,
InterruptedException {
// TODO: create new ZipEntry & add to the ZipOutputStream (zos)
}
@Override
public void close(TaskAttemptContext context) throws IOException,
InterruptedException {
zos.close();
}
}
}
关于Hadoop 方法输出数百万个小型二进制/图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250494/