我的 HDFS 中有大约 1 G 的图像 .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 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7337786/