hbase直接存取图片

文章转载自: http://blog.csdn.net/king_sky_wjb/article/details/30246441 @ 键盘上的孤舞者package hbase.test; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.KeyValue; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; public class HbaseTest { static Configuration configuration = null; static { configuration = HBaseConfiguration.create(); } //存储图片 /** * @param tableName 表名 * @param rowkey 行号 * @param colFamily 列簇 * @param colName 列名 * @param bs 列值,此处为图片 * @param imgType * 图片类型,读取图片时使用,图片类型的列名指定为imageType,imgType这里指的是列名为imageType的列值 */ public static void storeImage(String tableName, String rowkey, String colFamily, String colName, byte[] bs, String imgType) throws IOException { HTable table = new HTable(configuration, tableName); java.util.List puts = new java.util.ArrayList(); //存二进制图片 Put put = new Put(Bytes.toBytes(rowkey)); put.add(Bytes.toBytes(colFamily), Bytes.toBytes(colName), bs); //存图片类型 Put putx = new Put(Bytes.toBytes(rowkey)); putx.add(Bytes.toBytes(colFamily), Bytes.toBytes("imageType"), Bytes.toBytes(imgType)); puts.add(put); puts.add(putx); //put.a //table.put(put); table.put(puts); table.close(); } //读取图片 public static void getImage(String tableName, String rowkey) throws IOException { HTable table = new HTable(configuration, tableName); Get get = new Get(Bytes.toBytes(rowkey)); Result result = table.get(get); for (KeyValue k : result.raw()) { String col = Bytes.toStringBinary(k.getQualifier()); System.out.println("===k:String===>>:" + col); if (col.equals("imgContent")) { byte[] bs = k.getValue(); FileUtils.writeByteArrayToFile(new java.io.File("/home/zcwangjb/document/hbase.jpg"), bs); } } table.close(); } //创建表并指定列簇 public static void createTable(String tableName, String cols[]) throws MasterNotRunningException, ZooKeeperConnectionException, IOException { HBaseAdmin baseAdmin = new HBaseAdmin(configuration); if (baseAdmin.tableExists(tableName)) { System.out.println("表【" + tableName + "】存在"); } else { HTableDescriptor tableDescriptor = new HTableDescriptor(tableName); for (String s : cols) { HColumnDescriptor columnDescriptor = new HColumnDescriptor(s); tableDescriptor.addFamily(columnDescriptor); } baseAdmin.createTable(tableDescriptor); baseAdmin.close(); } } //删除表 public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException { HBaseAdmin admin = new HBaseAdmin(configuration); if (admin.tableExists(tableName)) { admin.disableTable(tableName); admin.deleteTable(tableName); } } //添加数据 /** * @param tableName 表名 * @param rowkey 行号 * @param colFamily 列簇 * @param column 列 * @param value 列值 */ public static void insertData(String tableName, String rowkey, String colFamily, String column, String value) throws IOException { HTable table = new HTable(configuration, tableName); Put put = new Put(Bytes.toBytes(rowkey)); put.add(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(value)); table.put(put); table.close(); } //查询一条数据 public static void getOneByeRowkey(String tableName, String rowkey) throws IOException { HTable table = new HTable(configuration, tableName); Get get = new Get(Bytes.toBytes(rowkey)); Result result = table.get(get); for (KeyValue k : result.raw()) { System.out.println("行号:" + Bytes.toStringBinary(k.getRow())); System.out.println("时间戳:" + k.getTimestamp()); System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily())); System.out.println("列:" + Bytes.toStringBinary(k.getQualifier())); System.out.println("值:" + Bytes.toString(k.getValue())); } table.close(); } //查询指定数据 public static void scanData(String tableName) throws IOException { HTable table = new HTable(configuration, tableName); Scan scan = new Scan(); ResultScanner rs = table.getScanner(scan); for (Result result : rs) { for (KeyValue k : result.raw()) { System.out.println("行号:" + Bytes.toStringBinary(k.getRow())); System.out.println("时间戳:" + k.getTimestamp()); System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily())); System.out.println("列:" + Bytes.toStringBinary(k.getQualifier())); System.out.println("值:" + Bytes.toString(k.getValue())); } } table.close(); } //删除一条数据 @SuppressWarnings("empty-statement") public static void deleteByRow(String tableName, String rowkey) throws IOException { HTable table = new HTable(configuration, tableName); //删除一条数据 Delete delete = new Delete(Bytes.toBytes(rowkey)); table.delete(delete); //删除多条数据 String[] rowkeys = {}; java.util.List list = new java.util.ArrayList(); for (String rk : rowkeys) { Delete d = new Delete(Bytes.toBytes(rk)); list.add(d); } table.delete(list); // table.close(); } public static void main(String[] args) throws ZooKeeperConnectionException, IOException { String tableName = "mytest"; String cols[] = {"a", "b", "c"}; // createTable(tableName, cols); // insertData(tableName, "1", "a", "abc", "11111"); // getOneByeRowkey(tableName, "1"); //scanData(tableName); // deleteByRow(tableName, "1"); // scanData(tableName); //getImage(tableName, "1"); // String imgType = ".jpg"; // byte[] bs = null; // java.io.InputStream is = new java.io.FileInputStream(new File("/home/zcwangjb/document/solr.jpg")); // bs = IOUtils.toByteArray(is); // storeImage(tableName, "1", "a", "imgContent", bs, imgType); getImage(tableName, "1"); } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值