jn5047
jn5047

Reputation: 101

Deleting all HBase rows with key filters

So the following HBase command works to list the key/value where the key's prefix does NOT match PREFIX1 or PREFIX2

scan 'MYTABLE', {FILTER=>"RowFilter(!=, 'regexstring:(PREFIX1)|(PREFIX2).*')"} 

My goal is to perform the deleteall command for those key/values

deleteall 'MYTABLE', {FILTER=>"RowFilter(!=, 'regexstring:(PREFIX1)|(PREFIX2).*')"} 

However, the output is...

0 row(s) in 0.0030 seconds

and the previous scan still shows the same result as if nothing got deleted.

I tried

deleteall 'MYTABLE', 'HELLO\x00WORLD' 

but it did not delete all the key 'HELLO\x00WORLD' in 'MYTABLE'.

How do I go about deleting all the row with matching prefix in the key?

Upvotes: 0

Views: 4305

Answers (1)

jn5047
jn5047

Reputation: 101

This cannot be done using HBase shell command.

This better done using HBase API in Java.

Here I select the set of table using RegEx (tableRegEx) and delete all rows that does not match the pattern rowKeyRegEx.

        String tableRegEx = ".*_SUFFIX";
        String rowKeyRegEx = "(PREFIX1)|(PREFIX2).*";

        HTableDescriptor[] tableDescriptor = admin.listTables();
        List<Table> tables = new ArrayList<Table>();

        for (HTableDescriptor tableDesc: tableDescriptor) {
            String tableName = tableDesc.getNameAsString();
            if (tableName.matches(tableRegEx)) {
                Table table = connection.getTable(TableName.valueOf(tableName));
                tables.add(table);
            }
        }

        Filter regExFilter = new RowFilter(CompareOp.NOT_EQUAL, new RegexStringComparator(rowKeyRegEx));

        for (Table table : tables) {
            List<Delete> deleteList = new ArrayList<Delete>();
            Scan scan = new Scan();
            scan.setFilter(regExFilter);
            ResultScanner scanner = table.getScanner(scan);
            for (Result rr : scanner) {
                Delete d = new Delete(rr.getRow());
                deleteList.add(d);
            }
            scanner.close();
            try {
                table.delete(deleteList);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Upvotes: 1

Related Questions