2013-01-16 41 views
4

我之前使用過對存儲在Accumulo中的數據的掃描,並獲得了整個結果集(無論我指定的Range是什麼)。問題是,我想在客戶端收到它們之前,從Accumulo過濾服務器端的那些。我希望有人有一個簡單的代碼如何做到這一點的例子。如何使用正則表達式篩選Accumulo上的掃描

從我的理解,Filter提供了一些(所有?)這個功能,但是在實踐中如何使用API​​呢?我使用shell客戶端上的篩選看到一個例子,從Accumulo文檔瀏覽:http://accumulo.apache.org/user_manual_1.3-incubating/examples/filter.html

基於在任何數據的正則表達式我找不到任何代碼示例在線過濾掃描一個簡單方式,儘管我認爲這應該是相對容易的事情。

回答

9

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構造函數(優先級和名稱)的前兩個參數是不是在這種情況下,相關的。一旦添加了上面的代碼,遍歷掃描器將只返回匹配正則表達式參數的鍵/值對。

+0

謝謝利茲。我實際上挖了一下,發現了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