HBase插入和读取图片

来源:互联网 发布:九九乘法表 java 编辑:程序博客网 时间:2024/05/16 11:22

把图片添加到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();Put put = new Put("002".getBytes());put.add("cf1".getBytes(), "img".getBytes(), bbb);//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();
0 0
原创粉丝点击