Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of spout and bolt java api in storm

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Xiaobian to share with you what is the use of spout and bolt java api in storm, I hope you have gained something after reading this article, let's discuss it together!

Component

In Storm, Spout and Bolt are its components. So Storm defines a general interface called IComponent.

The spectrum is as follows:

The green part is our most common and simple part. The red part is related to the transaction, which will be explained in detail in later articles.

BaseComponent is a "lazy" class provided by Storm. Why so? It and its subclasses implement more or less some of the methods defined by its interface. This way, we can inherit the class directly when we use it, instead of writing all the methods ourselves every time. However, it is worth mentioning that the class defined by BaseXXX, the methods it implements, are empty and return null directly.

Spout

in that basic example above, we implement a Random Spout, so let's look at its clas diagram

The topmost abstraction of Spout is the ISpout interface.

The open method is an initialization action. Allows you to do some actions during the initialization of the spout, passing in the context, and facilitating some data from the context.

The close method executes before the spout is closed, but there is no guarantee that it will be executed. Spout is a process that runs inside a worker as a task. In cluster mode, the supervisor will kill -9 walker directly, so that it cannot execute. In local mode, as long as it is not kill -9, if it is sending a stop command, it can guarantee the execution of close.

activate and deactivate: A spout can be temporarily activated and deactivated, and these two methods are called at the corresponding time.

nextTuple is used to transmit data.

ack(Object)

The Object passed in is actually an id that uniquely represents a tuple. This method is executed after the tuple corresponding to this id has been successfully processed.

fail(Object)

The same as ack, but executed when tuple processing fails.

Since our RandomSpout inherits BaseRichSpout, we don't need to implement close, activate, deactivate, ack, fail, and getComponentConfiguration methods, only care about the most basic core parts.

Conclusion:

Under normal circumstances (except Shell and transactional), to implement a Spout, you can directly implement the interface IRichSpout, if you do not want to write redundant code, you can directly inherit BaseRichSpout.

Bolt

Class diagram of ExclaimBasicBolt:

Here you can see a strange question:

Why isn't IBasicBolt inheriting IBolt?

We look down with questions.

IBolt defines three methods:

IBolt inherits java.io.Serializable. After we submit the topology on nimbus, the bolt created will be serialized and sent to the specific worker. When the worker executes the Bolt, it will first call the prepare method and pass the current execution context.

execute accepts a tuple for processing, and feeds back the processing result with the ack method (indicating success) or fail (indicating failure) of OutputCollector passed in the prepare method.

cleanup Same as ISpout's close method, called before closing. There is also no guarantee that it will be enforced.

The red part is the place that must be paid attention to when Bolt is implemented. Storm provides the IBasicBolt interface, and its purpose is to implement the Bolt of the interface without providing feedback results in the code. Storm will automatically feedback success.

If you do want to report a failure, throw a FailedException.

Let's write another Bolt that inherits BaseRichBolt instead of ExclaimBasicBolt. The code is as follows:

public class ExclaimRichBolt extends BaseRichBolt { private OutputCollector collector; @Override public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) { this .collector = collector; } @Override public void execute(Tuple tuple) { this .collector.emit(tuple, new Values(tuple.getString( 0 )+ "! " )); this .collector.ack(tuple); } @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declare( new Fields( "after_excl" )); } }

Changing topology

//builder.setBolt("exclaim", new ExclaimBasicBolt(), 2).shuffleGrouping("spout"); builder.setBolt( "exclaim" , new ExclaimRichBolt(), 2 ).shuffleGrouping( "spout" );

Run, the results are consistent.

Conclusion:

Usually, to implement a Bolt, you can implement IRichBolt interface or inherit BaseRichBolt. If you don't want to handle the result feedback yourself, you can implement IBasicBolt interface or inherit BaseBasicBolt. It is actually equivalent to automatically doing away with the prepare method and collector.emit.ack(inputTuple);

After reading this article, I believe you have a certain understanding of "what is the use of spout and bolt java api in storm". If you want to know more about it, welcome to pay attention to the industry information channel. Thank you for reading!

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.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report