2012-12-29 20 views
0

我已经添加到FilterLists的层次,让我从HBase的表需要我只能过滤多行筛选器,类似如下:HBase的FilterList.Operator.MUST_PASS_NONE功能

FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE); 
FilterList uidList = new FilterList(FilterList.Operator.MUST_PASS_ALL); 
if(term.length()>longSize){ 
    long highValue = Long.parseLong(term.substring(longSize)); 
    uidList.addFilter(new RowFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL, 
          new BinaryComparator(Bytes.add(prefix, Bytes.toBytes(addLongPadding(lowValue)))))); 
    uidList.addFilter(new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, 
          new BinaryComparator(Bytes.add(prefix, Bytes.toBytes(addLongPadding(highValue)))))); 
} else 
    uidList.addFilter(new RowFilter(CompareFilter.CompareOp.EQUAL, 
          new BinaryComparator(Bytes.add(prefix, Bytes.toBytes(addLongPadding(lowValue)))))); 
list.addFilter(uidList); 

这只是举例来说,在list中添加了针对特定案例的更多过滤器。有没有一种方法可以使用FilterList运算符,以便我可以否定过滤器?对于这个例子,我需要过滤不在lowValuehighValue之间的所有行,对于其他分支的所有行都是NOT_EQUAL到我的值(lowValue == highValue)。

我知道运营商是基于NANDNOR所以从技术上来说这可能是可能的,但我不知道如何。

回答

0

hbase列之间没有运算符。如果你想要这些类型的过滤器,你需要根据自己的要求自己写。

检查这个代码片段:

public ArrayList<HashMap<String, String>> between(String tableName,String colFamilyName, String [] colName,String betweenColName,String lowerValue,String upperValue) 
{ 
    ResultScanner rs=null; 
    ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String, String>>(); 
    Result res = null; 
    try 
    { 
     HTable table = new HTable(conf, tableName); 
     Scan scan = new Scan(); 
     SingleColumnValueFilter singleColumnValueFilterA = new SingleColumnValueFilter(
       Bytes.toBytes(colFamilyName), Bytes.toBytes(betweenColName), CompareOp.GREATER, Bytes.toBytes(lowerValue)); 
       singleColumnValueFilterA.setFilterIfMissing(true); 
     SingleColumnValueFilter singleColumnValueFilterB = new SingleColumnValueFilter(
       Bytes.toBytes(colFamilyName), Bytes.toBytes(betweenColName), CompareOp.LESS_OR_EQUAL, Bytes.toBytes(upperValue)); 
     singleColumnValueFilterB.setFilterIfMissing(true); 
     FilterList filter = new FilterList(Operator.MUST_PASS_ALL, Arrays.asList((Filter) singleColumnValueFilterA, 
       singleColumnValueFilterB)); 
     scan.setFilter(filter); 
     rs = table.getScanner(scan); 
     while((res=rs.next()) != null) 
     { 
      HashMap<String, String> map = new HashMap<String,String>(); 
      String s = null; 
      for(int j=0 ; j < colName.length ; j++) 
      { 
       byte[] obtainedRow = res.getValue(Bytes.toBytes(colFamilyName),Bytes.toBytes(colName[j])); 
       s = Bytes.toString(obtainedRow); 
       map.put(colName[j],s); 
      }   
      al.add(map); 
     } 
    } catch (IOException e) 
    { 
     System.out.println("Exception occured in retrieving data"); 
    } 
    finally 
    { 
     rs.close(); 
    } 
    return al; 
} 
+0

的'setFilterIfMissing'是一个不错的把戏在'SingleColumnValueFilter'使用,有效地否定你写的过滤器,但它受限于这种类型的过滤器,所以它似乎到目前为止,其他类型的过滤器,你只需要写oposite过滤器... –