The basic concepts of the strom framework will not be mentioned.Stream
The message stream of the custom ID. By default, both spout and bolt require interface methods.declareOutputFields
The Code is as follows:
@Overridepublic void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) { outputFieldsDeclarer.declare(new Fields("body"));}
In this case, messages are received by all defined bolts. If we need to select different bolts based on the message type, we need stream grouping.
- First
OutputFieldsDeclarer
To define the stream for transmitting multiple message streams.
Two Stream message streams are defined as follows: email and SMS
@Overridepublic void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) { outputFieldsDeclarer.declareStream("email", new Fields("body")); outputFieldsDeclarer.declareStream("sms", new Fields("body"));}
- Then, the message content is analyzed and determined to transmit the specified stream type.
@ Overridepublic void execute (tuple) {string streamtype; string value = tuple. getstringbyfield ("body"); # logically judge stub code if (value. startswith ("Email:") {streamtype = "email" ;}else {streamtype = "SMS" ;}outputcollector. emit (streamtype, new values (value ));}
- When setting the bolt message source in topology, localorshufflegrouping is used to set to only receive messages of the specified stream.
Filterbolt processes messages and specifies different streams when delivered to bolts. emailpolicybolt only receivesemail
Type stream message, smsnotifybolt only receivessms
Type of stream messages.
TopologyBuilder topologyBuilder = new TopologyBuilder();topologyBuilder.setSpout("RabbitmqSpout", new RabbitmqSpout());topologyBuilder.setBolt("FilterBolt", new FilterBolt()).shuffleGrouping("RabbitmqSpout");topologyBuilder.setBolt("EmailNotifyBolt", new EmailNotifyBolt()).localOrShuffleGrouping("FilterBolt", "email");topologyBuilder.setBolt("SmsNotifyBolt", new SmsNotifyBolt()).localOrShuffleGrouping("FilterBolt", "sms");
Storm framework: How to Select different bolts based on business conditions for downstream messaging