Database Reference
In-Depth Information
The most basic local operation is the
Filter
, which is implemented by
extending the
BaseFilter
class. A single method,
isKeep
, is
implemented todecidewhetherornotaTuple'sprocessing shouldcontinue.
For example, this filter implementation randomly chooses to keep half of its
results:
public class
RandomFilter
extends
BaseFilter {
private static final long
serialVersionUID
= 1L;
Random rng =
new
Random();
public boolean
isKeep(TridentTuple tuple) {
return
rng.nextFloat() > 0.5;
}
}
The next simplest operation is the
Function
, which is implemented by
extendingthe
BaseFunction
class(someTridenttutorialsincorrectlyhave
filters extend
BaseFunction
as well, but this is not the case in 0.9.0).
In functionality,
Function
objects are closest to the
BasicBolt
in the
“traditional” Storm topology. They take in a single
Tuple
and can produce
zero or more tuples, which are appended to the original tuple. If no tuples
are produced, the original tuple is filtered from the stream. For example,
this
Function
implementation adds a random number to each incoming
Tuple
:
public class
RandomFunction
extends
BaseFunction {
private static final long
serialVersionUID
= 1L;
Random rng =
new
Random();
public void
execute(TridentTuple tuple,
TridentCollector collector) {
collector.emit(
new
Values(rng.nextDouble()));
}
}
Both
Filter
and
Function
classes use the
each
method to attach
themselves to a stream. For filters, the
each
method takes the form of
each(inputFields,filter)
: