Databases Reference
In-Depth Information
Multiple Anchoring
To use a bolt to join or aggregate streams, you'll need to buffer tuples in memory. In
order to message guarantee in this scenario you have to anchor the stream to more than
one tuple. This is done by calling
emit
with a
List
of tuples.
...
List
<
Tuple
>
anchors
=
new
ArrayList
<
Tuple
>();
anchors
.
add
(
tuple1
);
anchors
.
add
(
tuple2
);
_collector
.
emit
(
anchors
,
values
);
...
That way, any time a bolt acks or fails, it notifies the tree, and because the stream is
anchored to more than one tuple, all spouts involved are notified.
Using IBasicBolt to Ack Automatically
As you probably noticed, there are lots of use cases in which you need message guar-
antees. To make things easier, Storm provides another interface for bolts called
IBasic
Bolt
, which encapsulates the pattern of calling
ack
right after the
execute
method. An
implementation of this interface,
BaseBasicBolt
, is used to do the acking automatically.
class
SplitSentence
extends
BaseBasicBolt
{
public
void
execute
(
Tuple
tuple
,
BasicOutputCollector
collector
)
{
String
sentence
=
tuple
.
getString
(
0
);
for
(
String
word:
sentence
.
split
(
" "
))
{
collector
.
emit
(
new
Values
(
word
));
}
}
public
void
declareOutputFields
(
OutputFieldsDeclarer
declarer
)
{
declarer
.
declare
(
new
Fields
(
"word"
));
}
}
Tuples emitted to
BasicOutputCollector
are automatically anchored to
the input tuple.