欢迎投稿

今日深度:

HBase插入和读取图片,

HBase插入和读取图片,


把图片添加到HBase中需要先转变为二进制数组,读取时再转变回来。

图片的插入:

Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "Master,Slave1,Slave2");
HTable table = new HTable(configuration, "test03");
			
String imgPath = "E:\\celebrity\\test\\1.jpg";
FileInputStream fis = new FileInputStream(imgPath);
byte[] bbb = new byte[fis.available()];//读图为流,但字节数组还是空的
fis.read(bbb);//将文件内容写入字节数组
fis.close();
			
// 002是行键
Put put = new Put("002".getBytes()); 
// cf1是列族,img是列,bbb是插入的值(图片转化成的字节数组)
put.add("cf1".getBytes(), "img".getBytes(), bbb);
table.put(put);
table.close();

图片的读取:

//将hbase获取的二进制流转化成文件夹中的图片
Configuration configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "Master,Slave1,Slave2");
HTable table = new HTable(configuration,"test03");
Get get = new Get("002".getBytes());
Result rs = table.get(get);
byte[] bs = rs.value(); //保存get result的结果,字节数组形式
table.close();
File file=new File("E:\\celebrity\\test\\test.jpg");//将输出的二进制流转化后的图片的路径
FileOutputStream fos=new FileOutputStream(file);
fos.write(bs);
fos.close();

www.htsjk.Com true http://www.htsjk.com/hbase/34444.html NewsArticle HBase插入和读取图片, 把图片添加到HBase中需要先转变为二进制数组,读取时再转变回来。 图片的插入: Configuration configuration = HBaseConfiguration.create();configuration.set("hbase.zookeeper.quorum",...
相关文章
    暂无相关文章
评论暂时关闭