The Filter
class lays the framework for the functionality you want. To create a custom filter, you need to extend Filter
and implement the accept(Key k, Value v)
method. If you are only looking to filter based on regular expressions, you can avoid writing your own filter by using RegExFilter
.
Using a RegExFilter
is straightforward. Here is an example:
//first connect to Accumulo
ZooKeeperInstance inst = new ZooKeeperInstance(instanceName, zooServers);
Connector connect = inst.getConnector(user, password);
//initialize a scanner
Scanner scan = connect.createScanner(myTableName, myAuthorizations);
//to use a filter, which is an iterator, you must create an IteratorSetting
//specifying which iterator class you are using
IteratorSetting iter = new IteratorSetting(15, "myFilter", RegExFilter.class);
//next set the regular expressions to match. Here, I want all key/value pairs in
//which the column family begins with "J"
String rowRegex = null;
String colfRegex = "J.*";
String colqRegex = null;
String valueRegex = null;
boolean orFields = false;
RegExFilter.setRegexs(iter, rowRegex, colfRegex, colqRegex, valueRegex, orFields);
//now add the iterator to the scanner, and you're all set
scan.addScanIterator(iter);
The first two parameters of the iteratorSetting
constructor (priority and name) are not relevant in this case. Once you've added the above code, iterating through the scanner will only return key/value pairs that match the regex parameters.