从hadoop sequencefile获取原始图像时出错

f0brbegy  于 2021-06-03  发布在  Hadoop
关注(0)|答案(1)|浏览(290)

我首先将所有图像打包到hadoop sequencefile中:

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));

然后我想从序列文件中还原原始图像:

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中从序列文件中获取原始图像?

l7mqbcuq

l7mqbcuq1#

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

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时也是这样。

相关问题