Filter
類爲您想要的功能奠定了框架。要創建自定義過濾器,您需要擴展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
構造函數(優先級和名稱)的前兩個參數是不是在這種情況下,相關的。一旦添加了上面的代碼,遍歷掃描器將只返回匹配正則表達式參數的鍵/值對。
來源
2013-02-04 20:01:11
Liz
謝謝利茲。我實際上挖了一下,發現了Accumulo(ASF)在其顛覆版中的一個測試用例:http://svn.apache.org/repos/asf/accumulo/branches/ACCUMULO-578/server/src/test/java /org/apache/accumulo/server/test/iterator/RegExTest.java除了你的迴應之外,這似乎基本上是全部展開的。再次感謝。 – Jack