Hbase查询Java实现

/**
 * hbase 根据rowkey 条件过滤
 *
 * @param hbaseFimilay hbase 列族
 * @param hbaseColumn  hbase 列
 * @param userList     用户列表
 * @throws Exception 异常类
 */
public Map<String, HashSet<String>> scanWithFilterByRowKey(String hbaseFimilay, String hbaseColumn, List<String> userList, String taskId) {
    Map<String, HashSet<String>> date2Users = new HashMap<>();
    try {
        for (int i = 0; i < userList.size(); i++) {
            Scan scan = new Scan();
            FilterList filterList = new FilterList();
            Filter filter = new RowFilter(CompareFilter.CompareOp.EQUAL, new BinaryPrefixComparator(userList.get(i).getBytes()));
            filterList.addFilter(filter);
            scan.setFilter(filterList);
            scan.addColumn(hbaseFimilay.getBytes(), hbaseColumn.getBytes());
            ResultScanner scanner = table.getScanner(scan);
            HashSet<String> dateCollection = new HashSet<>();
            for (Result result : scanner) {
                final byte[] value = result.getValue(hbaseFimilay.getBytes(), hbaseColumn.getBytes());
                dateCollection.add(new String(value));
            }
            date2Users.put(userList.get(i), dateCollection);
        }
        table.close();
        connection.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return date2Users;
}

/**
 * 模糊查rowkeys + 删除rowKey数据
 *
 * @param hbaseFimilay hbase family
 * @param hbaseColumn  hbase column
 * @param userList     用户列表
 * @throws Exception 异常类
 */
public void scanFuzzyMatchingByRowKey(String hbaseFimilay, String hbaseColumn, List<String> userList) throws Exception {
    Connection connection = ConnectionFactory.createConnection(config);
    Table table = connection.getTable(TableName.valueOf(tablename));
    // 模糊查rowkey集合
    HashSet<byte[]> o = new HashSet<>();
    for (int i = 0; i < userList.size(); i++) {
        Scan scan = new Scan();
        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("*_" + userList.get(i) + "_*"));
        scan.setFilter(rowFilter);
        scan.addColumn(hbaseFimilay.getBytes(), hbaseColumn.getBytes());
        ResultScanner scanner = table.getScanner(scan);
        while (scanner.iterator().hasNext()) {
            byte[] row = scanner.next().getRow();
            o.add(row);
        }
    }

    // 删除集合rowkey行
    Iterator<byte[]> iterator = o.iterator();
    while (iterator.hasNext()) {
        Delete delete = new Delete(iterator.next());
        table.delete(delete);
    }

    table.close();
    connection.close();
}

/**
 * 删除表中指定的rowkey列,并非所有的列
 *
 * @param hbaseTableName  hbase 表名
 * @param hbaseFamilyName hbase family
 * @param columnList      hbase column
 * @param rowKey          hbase rowkey
 * @throws Exception 异常类
 */
public void deleteColumnsByRowkey(String hbaseTableName, String hbaseFamilyName, List<String> columnList, String rowKey) throws Exception {
    Connection connection = ConnectionFactory.createConnection(config);
    Table table = connection.getTable(TableName.valueOf(hbaseTableName));
    for (String s : columnList) {
        Delete delete = new Delete(rowKey.getBytes());
        table.delete(delete);
    }
    table.close();
    connection.close();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值