A Filter
expects a single argument Tuple
and returns a boolean value stating whether or not the current Tuple in
the tuple stream should be discarded.
A Filter
may only be used with a
Each
pipe, and it may follow any other pipe
type.
To create a customFilter
, subclass the
class cascading.operation.BaseOperation
and implement the
interfacecascading.operation.Filter
. Because
BaseOperation
has been subclassed, the
isRemove
method, as defined on the Filter
interface, is the only method that must be implemented.
Example 5.3. Custom Filter
public class SomeFilter extends BaseOperation implements Filter { public boolean isRemove( FlowProcess flowProcess, FilterCall filterCall ) { // get the arguments TupleEntry TupleEntry arguments = filterCall.getArguments(); // initialize the return result boolean isRemove = false; // test the argument values and set isRemove accordingly return isRemove; } }
Filters should declare the number of argument values they expect.
Filters must accept 1 or more values in a Tuple as arguments, by
default they will accept any number (Operation.ANY
) of
values. Cascading will verify the number of arguments selected match the
number of arguments expected.
The number of arguments declarations must be done on the
constructor, either by passing a default value to the super
constructor, or by accepting the value from the user via a constructor
implementation.
Example 5.4. String Length Filter
public class StringLengthFilter extends BaseOperation implements Filter { public StringLengthFilter() { // expects 2 arguments, fail otherwise super( 2 ); } public boolean isRemove( FlowProcess flowProcess, FilterCall filterCall ) { // get the arguments TupleEntry TupleEntry arguments = filterCall.getArguments(); // filter out the current Tuple if the first argument length is greater // than the second argument integer value return arguments.getString( 0 ).length() > arguments.getInteger( 1 ); } }
The example above implements a fully functional
Filter
that accepts two arguments and filters out
the current Tuple if the first argument String length is greater than
the integer value of the second argument.
Copyright © 2007-2008 Concurrent, Inc. All Rights Reserved.