Java代码中Hbase的使用

3 篇文章 0 订阅

hbase简介
HBase是一个分布式的、面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文“Bigtable:一个结构化数据的分布式存储系统”。就像Bigtable利用了Google文件系统(File System)所提供的分布式数据存储一样,HBase在Hadoop之上提供了类似于Bigtable的能力。HBase是Apache的Hadoop项目的子项目。HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库。另一个不同的是HBase基于列的而不是基于行的模式。

hbase表结构

列簇1 列簇2(建议1-3个列簇)
列a 列b… 列a 列b…
列簇会用于做索引,一般列簇1,2用于存放常用的筛选用字段,列簇3用于存放内容字段
在下面的代码示例中,一共有两个列簇,familyName1和familyName2
familyName1中存放了三个用于频繁查询筛选的字段:ab,dt,hb
familyName2中存放了整个数据结构的json化字符串

hbase操作 代码示例
1、创建表

在这里插入代码片

2、增/改

public void put(String tableName, Object item) {
        if (item== null) {
            LOG.error("[HBase] itemis null!");
            return;
        }
        // 设置rowkey
        if (item.getid() == null || item.getid().isEmpty()) {
            LOG.error("[HBase] Adding null/empty key!");
            return;
        }

        String ultimateRowKey = getHashedID(item.getid());
        if (ultimateRowKey == null || ultimateRowKey.isEmpty()) {
            LOG.error("[HBase] Adding null/empty hashed key! Original key is " + item.getid());
            return;
        }
        Put put = new Put(Bytes.toBytes(ultimateRowKey));
        // HTabel负责跟记录相关的操作如增删改查等
        Table table = null;
        try {
            table = con.getTable(TableName.valueOf(tableName));
            put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("ab"), Bytes.toBytes(item.getAvailable()));
            put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("dt"), Bytes.toBytes(item.getDistype()));
            put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("hb"), Bytes.toBytes(item.getHotboost()));
            Gson gson = new Gson();
            String value = gson.toJson(item);
            put.addColumn(Bytes.toBytes(familyName2), Bytes.toBytes("js"), Bytes.toBytes(value));
            table.put(put);
        } catch (IOException e) {
            LOG.error("[HBase] Failed to connect to table while adding!  " + e.getMessage());
        } finally {
            try {
                if (table != null)
                    table.close();
            } catch (IOException e) {
                LOG.error("[HBase] Error while closing table " + e.getMessage());
            }
        }
    }

3、删

//按照rowkey进行删除某一行
public void del(String tablename, String rowkey) {
        // 设置rowkey
        if (rowkey == null || rowkey.isEmpty()) {
            LOG.error("[HBase] Adding null/empty key!");
            return;
        }
        String ultimateRowKey = getHashedID(rowkey);
        if (ultimateRowKey == null || ultimateRowKey.isEmpty()) {
            LOG.error("[HBase] Adding null/empty hashed key! Original key is " + rowkey);
            return;
        }
        Table table = null;
        Delete delete = new Delete(Bytes.toBytes(ultimateRowKey));
        try {
            table = con.getTable(TableName.valueOf(tablename));
            table.delete(delete);
        } catch (IOException e) {
            LOG.error("[HBase] Failed to connect to table while del!  " + e.getMessage());
        } finally {
            try {
                if (table != null)
                    table.close();
            } catch (IOException e) {
                LOG.error("[HBase] Error while closing table " + e.getMessage());
            }
        }
    }

4、查

public String get(String tablename, String rowKey) {
        // 先查询索引表
        Table cntTable = null;
        try {
            cntTable = con.getTable(TableName.valueOf(tablename));
            String ultimateRowKey = getHashedID(rowKey);
            if (ultimateRowKey == null || ultimateRowKey.isEmpty()) {
                LOG.error("[HBase] Getting hashed null/empty key! Original key is " + rowKey);
                return null;
            }
            Get get = new Get(Bytes.toBytes(ultimateRowKey));
            Result result = cntTable.get(get);

            String hbaseValue = Bytes.toString(result.getValue(Bytes.toBytes(familyName2), Bytes.toBytes("js")));
            return hbaseValue;
        } catch (IOException e) {
            LOG.error("[HBase] Failed to connect to " + tablename + " while getting!  " + e.getMessage());
            return null;
        } finally {
            try {
                if (cntTable != null)
                    cntTable.close();
            } catch (IOException e) {
                LOG.error("[HBase] Error while closing table " + tablename + e.getMessage());
            }
        }
    }

5.批量写入

public <T> void puts(String tableName, Map<String, Object> items) {
        if (items == null || items.isEmpty()) {
            LOG.error("[HBase] Adding null/empty item map!");
            return;
        }
        int maxSize = 10000;
        Table table = null;
        try {
            table = con.getTable(TableName.valueOf(tableName));
            int eachSize = Math.min(maxSize, items.size());
            List<Put> puts = new ArrayList<Put>(eachSize);
            int handled = 0;
            for (Entry<String, Object> entry : items.entrySet()) {
                String ultimateRowKey = getHashedID(entry.getKey());
                Object value = entry.getValue();
                if (ultimateRowKey == null || ultimateRowKey.isEmpty()) {
                    LOG.error("[HBase] Adding null/empty hashed key! Original key is " + entry.getKey());
                    handled++;
                    continue;
                }
                // System.out.println(ultimateRowKey);
                Put put = new Put(Bytes.toBytes(ultimateRowKey));
                put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("ab"), Bytes.toBytes(value .getAb()));
                put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("dt"), Bytes.toBytes(value .getDt()));
                put.addColumn(Bytes.toBytes(familyName1), Bytes.toBytes("hb"), Bytes.toBytes(value .getHb()));
                Gson gson = new Gson();
                String valuestr = gson.toJson(value);
                put.addColumn(Bytes.toBytes(familyName2), Bytes.toBytes("js"), Bytes.toBytes(valuestr));
                puts.add(put);
                handled++;
                // 每隔10000,写一次
                if (handled == eachSize) {
                    LOG.info("[HBase] Adding " + eachSize + "rows!");
                    table.put(puts);
                    puts = new ArrayList<Put>(eachSize);
                }
            }
            if (puts.size() > 0)
                table.put(puts);
        } catch (IOException e) {
            LOG.error("[HBase] Error while putting data " + e.getMessage());
        } finally {
            try {
                if (table != null)
                    table.close();
            } catch (IOException e) {
                LOG.error("[HBase] Error while closing table " + e.getMessage());
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值