A Filter
expects a stream of individual
argument Tuples and returns a Boolean value for each one, stating
whether it should be discarded. Like a Function
,
a Filter
is used with an
Each
pipe, which may follow any pipe type.
To create a custom Filter
, subclass the
class cascading.operation.BaseOperation
and implement the
interface cascading.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.4. Custom Filter
public class SomeFilter extends BaseOperation implements Filter
{
public boolean isRemove( FlowProcess flowProcess, FilterCall call )
{
// get the arguments TupleEntry
TupleEntry arguments = call.getArguments();
// initialize the return result
boolean isRemove = false;
// test the argument values and set isRemove accordingly
return isRemove;
}
}
Filters must accept one or more values in a Tuple as arguments,
and should declare the number of argument values they expect. If not
specified, the default is to accept any number of values
(Operation.ANY
). Cascading verifies during planning that
the number of arguments selected matches the number of arguments
expected.
The number of arguments declaration 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.5. 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 call )
{
// get the arguments TupleEntry
TupleEntry arguments = call.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 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-2012 Concurrent, Inc. All Rights Reserved.