Storm getting started

Source: Internet
Author: User

Storm framework Entry 1 Topology

Compared with Mapreduce, which is also a computing framework, Job is run on the Mapreduce cluster, and Topology is run on the Storm cluster. However, after the Job is run, it stops automatically. However, Topology can only be manually killed, otherwise it will continue to run.

Storm clusters have two types of nodes: one is a control node (Nimbus node) and the other is a worker node (Supervisor node ). All Topology tasks must be submitted on the Storm client node (), and The Nimbus node is allocated to other Supervisor nodes for processing. The Nimbus node first splits the submitted Topology into tasks and submits the information related to the Task and Supervisor to the zookeeper cluster. The Supervisor will claim the Task from the zookeeper cluster, notify your Worker process to process tasks. The overall Topology processing flowchart is:

 

Each Topology is composed of Spout and Bolt. The basic unit for transferring information between Spout and Bolt is Tuple, the continuous Tuple sent by Spout and its Child Tuple processed on the corresponding Bolt are called a Steam. The name of each Stream is when its first Tuple is sent by Spout, storm uses the internal Ackor mechanism to ensure that each Tuple is reliably processed.

Tuple can be understood as a key-value pair. The key is the Fields field defined in the declareStream method, and the value is the Values field sent in the emit method.

2 Configuration

Before Running Topology, you can configure some parameters to adjust the running status. The parameter configuration is done through the conf/Storm. yaml file in the storm framework deployment directory. In the next file, you can configure the Storm local directory path during running and the number of workers during running.

In the code, you can also set some Config parameters, but the priority is different. The priority order of Config parameters configured in different locations is as follows:

Frequently Used options in storm. yaml are:

Configuration option name

Role of configuration options

Topology. max. task. parallelism

Maximum number of executors for each Topology

Topology. workers

The default number of workers for each Topology running. If this parameter is set in the code, this option value is overwritten.

Storm. zookeeper. servers

Node list of the zookeeper Cluster

Storm. local. dir

Storm local storage directory used to store jar packages and temporary files

Storm. zookeeper. root

The root directory of Storm in the zookeeper cluster. The default value is "/".

Ui. port

The UI address port number of the Storm cluster, which is 8080 by default.

Nimbus. host:

Nimbus node host

Supervisor. slots. ports

The worker placeholder slots of the Supervisor node. All the topologies in the cluster share these slots, even if a large number of slots are set during submission, the system also allocates the remaining slots based on the actual number of slots in the current cluster. When all slots are allocated, the newly submitted Topology can only wait, the system will always monitor whether there are any blank slots. If there are any slots, allocate them to the newly submitted Topology.

Supervisor. worker. timeout. secs

The timeout time of a Worker, measured in seconds. After the timeout, Storm determines that the current worker process is dead and will re-allocate the running task.

Drpc. servers

List of servers of the drpc server when using the drpc Service

Drpc. port

The Service port of the drpc server when the drpc service is used

 

3 Spouts

Spout is the message source of Stream. The implementation of the Spout component can be accomplished by inheriting the BaseRichSpout class or other * Spout class, or by implementing the IRichSpout interface.

There are several important methods to implement the Spout class as needed:

 

3.1 open method

This open method is called when a Task is initialized. Generally, the SpoutOutputCollector object sending Tuple and the configuration object TopologyContext are initialized in this method.

Example:

            _collector =  }

 

3.2 declareOutputFields Method

This method is used to declare the Tuple sending stream of the current Spout. Stream is defined through the OutputFieldsDeclare. declareStream method. The parameters include the sent field Fields.

Example:

            declarer.declare( Fields("word"      }

 

3.3 getComponentConfiguration Method

This method is used to declare special Configuration configurations for the current component.

Example:

  Map<String, Object>          (!              Map<String, Object> ret =  HashMap<String, Object>              ret.put(Config.TOPOLOGY_MAX_TASK_PARALLELISM, 3                        }                        }

 

Here we set the maximum number of threads of the current Component in Topology.

3.4 nextTuple Method

This is the most important method in the Spout class. This method is used to transmit a Tuple to the Topology.

Example:

            Utils.sleep(100           String[] words =  String[] {"twitter","facebook","google"           Random rand =            String word =          _collector.emit(      }

 

Here we randomly select a word from an array as a Tuple and then send it to Topology through _ collector.

In addition to the preceding methods, there are also the ack, fail, and close methods. Storm will call the ack method after detecting that a Tuple is successfully processed. If the processing fails, the fail method will be called. These two methods have been implicitly implemented in the BaseRichSpout class.

4 Bolts

The Bolt class receives Tuple from the Spout or other upstream bolts and processes it. The implementation of the Bolt component can be completed by inheriting the BasicRichBolt class or the IRichBolt interface.

The main implementation methods of the Bolt class are as follows:

 

4.1 prepare method

This method is similar to the open method in Spout. It provides the OutputCollector for bolts to send Tuple from bolts. In Bolt, Tuple can be sent in the prepare method, execute method, cleanup, and other methods, generally in execute.

Example:

            _collector =      }

 

4.2 declareOutputFields Method

It is used to declare fields contained in the Tuple sent by bolts, which is similar to those in Spout.

Example:

            declarer.declare( Fields("obj", "count", "actualWindowLengthInSeconds"  }

 

In this example, the Tuple sent by the Bolt class contains three fields: "obj", "count", "actualWindowLengthInSeconds ".

4.3 getComponentConfiguration Method

Like the Spout class, the getComponentConfiguration method can also be used in bolts.

Example:

  Map<String, Object>          Map<String, Object> conf =  HashMap<String, Object>                  }

 

This example defines the frequency at which Tuple is sent from the system component stream to the current Bolt. When the system needs to perform specific processing at intervals of time, the features of this system component can be used.

4.4 execute Method

This is the most critical method in bolts. You can use this method to process Tuple. The specific sending is also completed through the emit method. There are two cases: one is that the emit method has two parameters, and the other is that there is one parameter.

(1) emit has a parameter: the unique parameter is the Tuple sent to the downstream bolts. In this case, the old Tuple sent from the upstream is separated here, the new Tuple and the old Tuple no longer belong to the same Tuple tree. The new Tuple creates a new Tuple tree.

(2) emit has two parameters: the first parameter is the input stream of the old Tuple, and the second parameter is the new Tuple stream sent to the downstream Bolt. At this time, the new Tuple and the old Tuple still belong to the same Tuple tree. That is, if the downstream Bolt fails to process the Tuple, it will be passed up to the current Bolt, the current Bolt continues the upstream transfer based on the old Tuple stream and applies for a Tuple that fails to be resold. Ensures the reliability of Tuple processing.

These two situations must be determined based on your own scenarios.

Example:

            _collector.emit(tuple,  Values(tuple.getString(0) + "!!!"                 _collector.emit( Values(tuple.getString(0) + "!!!"      }

 

In addition, there are ack, fail, and cleanup methods. The cleanup method is similar to the close method in Spout, which is called when the current Component is disabled. However, for real-time computing, these two methods are rarely used unless required in some special scenarios.

5 Stream grouping

This article introduces the basic components of Topology, Spout and Bolt. In Topology, Tuple processing of data streams is continuously completed by calling different spouts and bolts. The upstream-downstream relationship between bolts and spouts is defined in the entry class. Example:

 builder =   builder.setSpout(spoutId,  TestWordSpout(), 5  builder.setBolt(counterId,  RollingCountBolt(9, 3), 4).fieldsGrouping(spoutId,  Fields("word"  builder.setBolt(intermediateRankerId,  IntermediateRankingsBolt(TOP_N), 4).fieldsGrouping(counterId,  Fields("obj" builder.setBolt(totalRankerId,  9

 

In this example, the builder is a TopologyBuilder object. You can use its createTopology method to create a Topology object. At the same time, this builder also defines the Spout and Bolt objects used in the current Topology, the setSpout and setBolt methods are used respectively.

The first parameter in the setSpout method and setBolt method is the Stream ID of the current Component. The second parameter is the construction of the specific Component implementation class; the third parameter is the number of threads for concurrent execution of the current Component. Storm will determine the number of tasks in Topology based on the sum of the number. The last small tail * Grouping refers to how a Stream allocates data to the Task on the Bolt. Currently, Storm Stream Grouping has the following types:

Different Grouping needs to be set according to different scenarios.

6 Topology run 6.1 Topology run Mode

The running of Topology can be divided into local mode and distributed mode. The Mode settings can be set in the configuration file or in the code.

(1) local running submission method:

 LocalCluster cluster =       cluster.shutdown();

 

(2) distributed submission:

StormSubmitter.submitTopology(topologyName, topologyConfig, builder.createTopology());

 

Note that after Storm code is compiled, You need to package it into a jar package and run it in Nimbus. During packaging, you do not need to input all the dependent jar packages, otherwise, if the dependency storm. if the jar package is entered, duplicate configuration file errors will occur during the runtime, causing Topology to fail to run. Because the local storm. yaml configuration file will be loaded before the Topology runs.

Run the following command on Nimbus:

Storm jarStormTopology. jar maincalss args

6.2 Topology running process

< p>

(1) After Storm is submitted, the code will be first stored in the inbox directory of the Nimbus node, and a stormconf will be generated based on the configuration currently running Storm. the ser file is placed in the stormdist directory of the Nimbus node, and the serialized Topology code file is also contained in this directory;

(2) When setting the Spouts and Bolts associated with Topology, you can set the number of executors and tasks of the current Spout and Bolt at the same time. By default, the total number of tasks in a Topology is the same as the total number of executors. Then, the system allocates the execution of these tasks evenly based on the number of workers. The supervisor node on which the worker runs is determined by storm itself;

(3) After the task is assigned, the Nimbes node submits the task information to the zookeeper cluster, and there will be workerbeats nodes in the zookeeper cluster, the heartbeat information of all worker processes in the current Topology is stored here;

(4) The Supervisor node will continuously poll the zookeeper cluster and save all Task Assignment information, code storage directory, and association between tasks in the assignments node of zookeeper, by polling the content of this node, the Supervisor obtains its own task and starts the worker process to run;

(5) After a Topology is run, Stream streams are continuously sent through Spouts, and received Stream streams are continuously processed through Bolts. Stream streams are unbounded.

The last step will be executed continuously, unless the Topology is manually terminated.

6.3 Topology method call Process

The method calling process for Stream processing in Topology is as follows:

 

Note the following points:

6.4 Topology concurrency

There are several concepts related to parallelism in the execution unit of Topology.

(1) worker: each worker belongs to a specific Topology. Each worker node of a Supervisor can have multiple workers, and each worker uses a separate port, it runs one or more executor threads on each component in Topology to provide the task running service.

(2) executor: executor is a thread generated in the worker process. It executes one or more tasks of the same component.

(3) task: the actual data processing is completed by the task. In the lifecycle of Topology, the number of tasks of each component does not change, but the number of executors is not necessarily. The number of executors is smaller than or equal to the number of tasks. By default, the two executors are equal.

When running a Topology, you can set the number of workers, tasks, and executors according to the actual situation, and set the positions in multiple places.

6.5 terminate Topology

Terminate the running of a Topology by using the following command on the Nimbus node:

Storm kill TopologyName

After kill, you can view the topology status on the UI interface, which will first change to the KILLED status. After clearing the local directory and the information related to the current Topology in the zookeeper cluster, this Topology will completely disappear.

7. Topology tracking

After the Topology is submitted, you can view it on the web interface of the Nimbus node. The default address is

8. Storm Application

The above shows how to compile the Storm Framework Task Topology. In which scenarios can the Storm framework be used? The following describes several typical application scenarios of the Storm framework.

(1) using the Storm Framework's DRPC for a large number of concurrent function calls, that is, implementing distributed RPC;

(2) Use the Storm Framework's Transaction Topology, in this scenario, you can perform real-time batch update or query database operations or applications that require parallel processing of messages in the same batch and messages between batches. In this case, Topology can only have one TrasactionalSpout;

(3) use the Sliding Window logic and Storm framework to calculate the products with the most sales volume and Top N regions with the most buyers in a certain period of time;

(4) Precise advertising push: when users browse products, the browsing records are collected in real time and sent to bolts. bolts can use the browser information based on the user's account information (if any) after completing product classification statistics, Product Correlation query, and other logic calculations, push the computing results to users;

(5) Real-time log processing. Storm can be combined with a distributed storage to send data from multiple data sources to the processing logic Bolts in real time. After Bolts completes some logic processing, distributed storage framework for storage. In this case, multiple spouts can be created;

(6) Real-time Monitoring of Public Opinion hotspots. For example, bolts can generate a data source Spout during user queries based on semantic analysis, summarize Current Public Opinion hotspots;

(7) Real-time aggregation of data streams.

9 Reference URL

Http://xumingming.sinaapp.com/138/twitter-storm%E5%85%A5%E9%97%A8/

Https://github.com/nathanmarz/storm/wiki

Http://nathanmarz.github.io/storm/doc/index-all.html

Https://github.com/nathanmarz

 

If your understanding is not correct, you are welcome to criticize and correct your questions ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.