我的 HDFS 中有大约 1 Gig 的图像 .png 文件。谁能建议我一种将这些图像的索引值存储在 HBase 中并通过查询 HBase 来检索图像的方法。或者我如何使用 HDFS/HBase 来提供图像。请回复。
迫切需要 :(
提前致谢
以下代码将有所帮助。
//to store image file to hbase
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "test".getBytes());
Put put = new Put("row1".getBytes());
put.add("C".getBytes(), "image".getBytes(),
extractBytes("/path/to/image/input.jpg"));
table.put(put);
//to retrieve the image
Get get = new Get("row1".getBytes());
Result result = table.get(get);
byte[] arr = result.getValue("C".getBytes(), "image".getBytes());
OutputStream out = new BufferedOutputStream(new FileOutputStream(
"/path/to/image/output.jpg"));
out.write(arr);
//function to convert image file to bytes.
public static byte[] extractBytes(String ImageName) throws IOException {
File file = new File(ImageName);
BufferedImage originalImage = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
byte[] imageInByte = baos.toByteArray();
return imageInByte;
}
提供图像文件的基本方法有两种:将图像存储在 HBase 本身中,或者存储图像的路径。HBase 已成功地被一家大型商业照片共享网站用于存储和检索图像——尽管他们必须仔细调整和监控他们的系统(有关详细信息,请参阅 HBase 邮件列表)。
如果您将图像存储在 HDFS 上并且只在 HBase 中保留一个路径,则您必须确保不会有太多图像,因为 HDFS 不能很好地处理大量文件(取决于分配给您的名称节点的 RAM 大小,但仍有上限)。
除非您计划将元数据与每个图像一起存储,否则您可以使用非常简单的模式来存储数据或图像的路径。我想像一个带有两个列限定符的单列族:数据和类型。数据列可以存储路径或实际图像字节。该类型将存储图像类型(png、jpg、tiff 等)。这对于在返回图像时通过线路发送正确的 mime 类型很有用。
完成设置后,您只需要一个 servlet(或 thrift 中的等价物)来组装数据并将其返回给客户端。