把图片添加到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);
读文件
//将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();
hbase常用操作 https://www.youkuaiyun.com/gather_20/MtTaEg0sMTc1My1ibG9n.html