HBase读写code

最近用HBase存储数据,记录下方法

  1. HBase批量写数据(rowkey、列簇、value)
    a:连接:
    private Connection initHbaseConnection(){
    if (StringUtils.isBlank(hbaseZookeeperQuorum) || StringUtils.isBlank(hbaseZookeeperZnodeParent)) {
    LOG.warn(“hbaseZookeeperQuorum = {}, hbaseZookeeperZnodeParent = {}, initHbaseConnection return null”,
    hbaseZookeeperQuorum, hbaseZookeeperZnodeParent);
    throw new IllegalArgumentException(
    “initHbaseConnection hbaseZookeeperQuorum/hbaseZookeeperZnodeParent is empty”);
    }
    Configuration configuration = HBaseConfiguration.create();
    configuration.set(“hbase.zookeeper.quorum”, hbaseZookeeperQuorum);
    configuration.set(“zookeeper.znode.parent”, hbaseZookeeperZnodeParent);
    try {
    return ConnectionFactory.createConnection(configuration);
    } catch (IOException e) {
    LOG.error(“ERROR init Hbase Connection, Hbase Connection is null”);
    return null;
    }
    }
    b:单个写
    public boolean setProductTag() {
    final String family = snTypeEnum == m;
    final String rowKey = getRowKey(productType, productId);
    Table table = null;
    try {
    table = hbaseConnection.getTable(TableName.valueOf(HBASE_TABLE_NAME));
    Put p = new Put(Bytes.toBytes(rowKey));
    p.addColumn(Bytes.toBytes(family), Bytes.toBytes(Sn), expireTime, Bytes.toBytes(Value));
    table.put(p);
    return true;
    } catch (IOException e)
    return false;
    } finally {
    try {
    if (table != null) {
    table.close();
    }
    } catch (IOException e) {
    LOG.error(“table.close error”, e);
    }
    }
    }
    c:写多列rowkey
    try {
    table = hbaseConnection.getTable(TableName.valueOf(HBASE_TABLE_NAME));
    List puts = Lists.newArrayListWithExpectedSize(ptpProductTags.size());
    for (PtpProductTag ptpProductTag : ptpProductTags) {
    final String family =m;
    int productType = 0;
    String tId = ;
    String Sn = ;
    String Value = ;
    Long expireTime = ;
    final String rowKey =
    Put p = new Put(Bytes.toBytes(rowKey));
    p.addColumn(Bytes.toBytes(family), Bytes.toBytes(Sn), expireTime, Bytes.toBytes(Value));
    puts.add(p);
    }
    if (!puts.isEmpty()) {
    table.put(puts);
    }
    return true;
    } catch (IOException e) {
    return false;
    } finally {
    try {
    if (table != null) {
    table.close();
    }
    } catch (IOException e) {
    LOG.error(“table.close error”, e);
    }
    }
    2、HBase拉取全量数据
    由于HBase没有offSet,拉取数据方法如下:
    public interface Comparator {
    void compare(Multimap
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值