hbase中删除表中的行键_java – 如何大量删除hbase中的多行?

你真的想在shell中做,因为有其他更好的方法。一种方法是使用本机java API

>构造数组删除列表

>将此数组列表传递给Table.delete方法

方法1:如果您已经知道键的范围。

public void massDelete(byte[] tableName) throws IOException {

HTable table=(HTable)hbasePool.getTable(tableName);

String tablePrefix = "user_";

int startRange = 500;

int endRange = 999;

List listOfBatchDelete = new ArrayList();

for(int i=startRange;i<=endRange;i++){

String key = tablePrefix+i;

Delete d=new Delete(Bytes.toBytes(key));

listOfBatchDelete.add(d);

}

try {

table.delete(listOfBatchDelete);

} finally {

if (hbasePool != null && table != null) {

hbasePool.putTable(table);

}

}

}

方法2:如果要根据扫描结果进行批量删除。

public bulkDelete(final HTable table) throws IOException {

Scan s=new Scan();

List listOfBatchDelete = new ArrayList();

//add your filters to the scanner

s.addFilter();

ResultScanner scanner=table.getScanner(s);

for (Result rr : scanner) {

Delete d=new Delete(rr.getRow());

listOfBatchDelete.add(d);

}

try {

table.delete(listOfBatchDelete);

} catch (Exception e) {

LOGGER.log(e);

}

}

现在来使用CoProcessor。只有一个建议,’不要使用CoProcessor’,除非你是HBase的专家。

如果您需要我们可以为您提供详细的描述,CoProcessors有许多内置的问题。

其次,当您从HBase中删除任何东西时,它从未直接从Hbase中删除,所以将墓碑标记附加到该记录,然后在主压缩中被删除,因此不需要使用高度资源的协处理器。

修改代码支持批量操作。

int batchSize = 50;

int batchCounter=0;

for(int i=startRange;i<=endRange;i++){

String key = tablePrefix+i;

Delete d=new Delete(Bytes.toBytes(key));

listOfBatchDelete.add(d);

batchCounter++;

if(batchCounter==batchSize){

try {

table.delete(listOfBatchDelete);

listOfBatchDelete.clear();

batchCounter=0;

}

}}

创建HBase conf并获取表实例。

Configuration hConf = HBaseConfiguration.create(conf);

hConf.set("hbase.zookeeper.quorum", "Zookeeper IP");

hConf.set("hbase.zookeeper.property.clientPort", ZookeeperPort);

HTable hTable = new HTable(hConf, tableName);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值