我之前使用过对存储在 Accumulo 中的数据进行扫描,并获得了整个结果集(无论 Range
我指定了什么)。问题是,我想在客户端接收它们之前从 Accumulo 过滤服务器端的那些。我希望有人有一个简单的代码示例来说明这是如何完成的。
据我了解,Filter
提供了一些(全部?)这个功能,但是在实践中如何使用 API 来使用它?我从这里的 Accumulo 文档中看到了一个在 shell 客户端上使用过滤器的示例:http://accumulo.apache.org/user_manual_1.3-incubating/examples/filter.html
我在网上找不到任何基于正则表达式过滤任何数据的扫描的简单方法的代码示例,尽管我认为这应该是相对容易的事情。
最佳答案
Filter
class 为您想要的功能奠定了框架。要创建自定义过滤器,您需要扩展 Filter
并实现 accept(Key k, Value v)
方法。如果您只想基于正则表达式进行过滤,则可以使用 RegExFilter
避免编写自己的过滤器。 .
使用 RegExFilter
很简单。下面是一个例子:
//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
的前两个参数在这种情况下,构造函数(优先级和名称)不相关。添加上述代码后,遍历扫描器将仅返回与正则表达式参数匹配的键/值对。
关于regex - 如何使用 RegEx 在 Accumulo 上过滤扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14365239/