Skip to content
edwardcapriolo edited this page Jan 22, 2013 · 1 revision

Terminology

  • CREATEFILTER: Creates and loads a filter
  • FILTERMODE: Enables or disables filters

High level description

A Filter is a component that processes data (typically a get, slice, or query) after it has been read from Cassandra and before it is stored in the response. Filters can be used to:

  • Eliminate columns that do not match a specific criteria
  • Modify data in columns

API

A Filter is input a Map with the properties of the column and can return a Map or null. Returning null eliminates the column from the results.

package org.usergrid.vx.experimental;
import java.util.Map;

public interface Filter {
  public Map filter(Map row);
}

Example

req.add( Operations.setKeyspaceOp("filterks") ); //0
req.add( Operations.createKsOp("filterks", 1)); //1
req.add( Operations.createCfOp("filtercf")); //2
req.add( Operations.setColumnFamilyOp("filtercf") ); //3
req.add( Operations.setAutotimestampOp() ); //4
req.add( Operations.assumeOp("filterks", "filtercf", "value", "UTF-8"));//5

Insert two columns, one with a value of 20 and one with a value greater then 20

req.add( Operations.setOp("rowa", "col1", "20")); //6
req.add( Operations.setOp("rowa", "col2", "22")); //7

We will create a filter that returns rows that only have a column value greater then 21.

req.add( Operations.createFilterOp("over21", "groovy", 
  "public class Over21 implements org.usergrid.vx.experimental.Filter { \n"+
    " public Map filter(Map row){ \n" +
    "   if (Integer.parseInt( row.get(\"value\") ) >21){ \n"+
    "     return row; \n" +
    "   } else { return null; } \n" +
    " } \n" +
    "} \n"
 )); //8

Enabled the filter. The results from any get or slice requests are filtered server side.

 req.add( Operations.filterModeOp("over21", true)); //9
 req.add( Operations.sliceOp("rowa", "col1", "col3", 10)); //10

The slice without the filter would have returned two columns but with the filter it only returns one.

 List<Map> results = (List<Map>) res.getOpsRes().get(10);
 Assert.assertEquals( "22", results.get(0).get("value") );
 Assert.assertEquals( 1, results.size());