如何从 HBase 获取图像

发布于 2024-12-03 04:48:59 字数 146 浏览 6 评论 0原文

我的 HDFS 中有大约 1 Gig 的图像 .png 文件。任何人都可以建议我一种将这些图像的索引值存储在 HBase 中并通过查询 HBase 检索图像的方法。或者我如何使用 HDFS/HBase 来提供图像。请回复。

紧急要求:(

提前致谢

I have around 1 Gig of image .png files in my HDFS. Can anyone suggest me a way to store the index values to these images in HBase and retrieve the image by querying HBase. Or how can I used HDFS/HBase to serve images. Pls reply .

Urgent requirement :(

Thanks In Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极致的悲 2024-12-10 04:48:59

下面的代码会有帮助。

    //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;
}

The following code will help.

    //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;
}
绳情 2024-12-10 04:48:59

提供图像文件有两种基本方法:将图像存储在 HBase 本身中,或者存储图像的路径。 HBase 已成功被大型商业照片共享网站用于存储和检索图像 - 尽管他们必须仔细调整和监控其系统(有关详细信息,请参阅 HBase 邮件列表)。

如果您将图像存储在 HDFS 上并且仅在 HBase 中保留路径,则必须确保不会有太多图像,因为 HDFS 不能很好地处理大量文件(取决于分配给名称节点的 RAM 大小,但仍有上限)。

除非您计划将元数据与每个图像一起存储,否则您可能可以使用非常简单的模式来存储数据或图像的路径。我想象的是一个带有两个列限定符的单列族:数据和类型。数据列可以存储路径或实际图像字节。该类型将存储图像类型(png、jpg、tiff 等)。这对于在返回图像时通过线路发送正确的 mime 类型非常有用。

一旦你完成了这个设置,你所需要的只是一个 servlet(或者 thrift 中的类似东西)来组装数据并将其返回给客户端。

There are two basic ways of serving image files: storing the image in HBase itself, or storing a path to the image. HBase has successfully been used by a large-scale commercial photo sharing site for storing and retrieving images -- although they have had to carefully tune and monitor their system (see the HBase mailing list for details).

If you store your images on HDFS and only keep a path in HBase you will have to ensure that you will not have too many images as HDFS does not deal well with a lot of files (depends on the size of RAM allocated to your namenode, but there is still an upper limit).

Unless you plan on storing meta data along with each image, you may be able to get away with a very simple schema for either storing the data or the path to the image. I am imagining something like a single column family with two column qualifiers: data, and type. The data column could store either the path or the the actual image bytes. The type would store the image type (png, jpg, tiff, etc.). This would be useful for sending the correct mime type over the wire when returning the image.

Once you have that set up, all you need is a servlet (or something equivalent in thrift) to assemble the data and return it to the client.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文