你真的想在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);