该类Filter
为您想要的功能奠定了框架。要创建自定义过滤器,您需要扩展Filter
和实现该accept(Key k, Value v)
方法。如果您只想基于正则表达式进行过滤,则可以避免使用RegExFilter
.
使用 aRegExFilter
很简单。这是一个例子:
//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);
在这种情况下,构造函数的前两个参数iteratorSetting
(优先级和名称)不相关。添加上述代码后,遍历扫描器将仅返回与正则表达式参数匹配的键/值对。