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);

读文件

//将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.csdn.net/gather_20/MtTaEg0sMTc1My1ibG9n.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值