In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to define Blots program in Storm". In daily operation, I believe many people have doubts about how to define Blots program in Storm. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to define Blots program in Storm". Next, please follow the editor to study!
Specific content
There are two important core concepts in the process of Storm program development: Spout and Blot. Spout will serve as the starting point of the data, which may come from various sources, such as the message content passed by Kafka. After each Spout receives the data, it will pass the data to Blot if necessary, and multiple Blot will handle the data. Think of each Blot as a data filter, and the last Blot will be used as data storage, while the general storage devices are often files, Redis databases.
In the processing of this program, Spouts will use random numbers to generate the corresponding age data, and there will be three Blot for data processing, these data processing has its own strict syntax requirements. If you want to implement Storm development, you need to configure all the related library files of Storm into ClassPath. If you want to develop Spout, you often need to implement an IRickSpout interface, but there are many methods in this interface, so it is recommended to inherit a subclass of this interface: BaseRichSpout. Example: define InfoCreateSpouts
Package cn.mldn.info.spouts;import java.util.Map;import java.util.Random;import org.apache.storm.spout.SpoutOutputCollector;import org.apache.storm.task.TopologyContext;import org.apache.storm.topology.OutputFieldsDeclarer;importorg.apache.storm.topology.base.BaseRichSpout;import org.apache.storm.tuple.Fields;import org.apache.storm.tuple.Values;@SuppressWarnings ("serial") publi cclass InfoCreateSpoutextendsBaseRichSpout {private SpoutOutputCollector collector=null;private String nameStr= "aBcDefghIjklmnopQrStuvwxyz"; / / assume the username private Random rand=new Random ()
@ SuppressWarnings ("rawtypes") @ Overridepublic void open (Mapconf,TopologyContextcontext,SpoutOutputCollectorcollector) {/ / initialize the Spout method, which is executed only once This.collector=collector;// receives the SpoutOutputCollector object in the initialization method} @ OverridepublicvoidnextTuple () {/ / automatically finds this method when executing the Spout program, this method is to send a message / / from a normal development point of view, the data here should be passed by the messaging system to String nameInfo=String.valueOf (this.nameStr.charAt (rand.nextInt (nameStr.length (); intageInfo=this.rand.nextInt (150) / / randomly generate an age / / finally, if you want to send data, structure: name, age, all Spouts data should be handed over to Blot for completion. This.collector.emit (newValues (nameInfo,ageInfo));} @ OverridepublicvoiddeclareOutputFields (OutputFieldsDeclarerdeclare) {/ / need to define the name of the data saved in each transfer declare.declare (newFields ("name", "age")); / / this is the text label of the message when Tuple is sent back}}
In the whole code, nextTuple () is called automatically by the system, and Spout generates the data needed by Blot.
Define Blots programs
Three Blot are required in this process, and the functions are as follows:
AgeJudementBlot: determine whether the age of incoming data is adult (ADULT) or young (YOUNG); NameUpperBlot: because names are uppercase and lowercase, the information is changed to uppercase for unified management; FinalBlot: data preservation processing. However, it should be noted that at this time, only a separate Blot is defined, and the connection between them needs to be completed through the program. Example: Blot implementation for age determination.
Package cn.mldn.info.blots;import org.apache.storm.topology.BasicOutputCollector;importorg.apache.storm.topology.OutputFieldsDeclarer;importorg.apache.storm.topology.base.BaseBasicBolt;import org.apache.storm.tuple.Fields;import org.apache.storm.tuple.Tuple;import org.apache.storm.tuple.Values;@SuppressWarnings ("serial") public class AgeJudementBlotextendsBaseBasicBolt {@ Override public void execute (Tupletuple,BasicOutputCollectorcollector) {
Example: handling name case operations
Example: add a Blot output to a file
These defined Blot must receive data from Spout, but there is no direct connection between these Blot, and all relationships must be dynamically configured through the program.
Write a test program
The biggest advantage of Storm is that it provides local windows simulation testing directly, but these bolts relationships need to be configured during the configuration process. Example: writing test programs
At this point, the study on "how to define Blots programs in Storm" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.