0

我首先将所有图像打包到 Hadoop 序列文件中:

FSDataInputStream in = null;    
in = fs.open(new Path(uri)); //uri is the image location in HDFS
byte buffer[] = new byte[in.available()];
in.read(buffer);
context.write(imageID, new BytesWritable(buffer));

然后我想在reducer中从序列文件中取回我的原始图像:

BufferedImage imag;    
imag = ImageIO.read(new ByteArrayInputStream(value.getBytes())); 

但是图像没有正确获得,因为我有这个错误:

Error: javax.imageio.IIOException: Error reading PNG image data
Caused by: java.io.EOFException: Unexpected end of ZLIB input stream

我的问题是如何从 hadoop 中的序列文件中获取原始图像?

4

1 回答 1

0

问题是我使用错误的方式来读取流。这是正确的方法:

import org.apache.commons.io.IOUtils;
Configuration confHadoop = new Configuration();
FileSystem fs = FileSystem.get(confHadoop);
Path file = new Path(fs.getUri().toString() + "/" + fileName);
in = fs.open(file);
byte[] buffer = IOUtils.toByteArray(in);

然后缓冲区可以通过new BytesWritable(buffer). 从 sequenceFile 读取时也是如此。

于 2014-04-22T08:43:43.530 回答