Storm practice wordcount

Source: Internet
Author: User
Tags emit

 

I. Environment Construction

Eclipse project creation key and jar package import.

2. Coding

1. Write the spout code of the component to launch the data source.

Package com; 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; import Org. apache. storm. topology. base. baserichspout; import Org. apache. storm. tuple. fields; import Org. apache. storm. tuple. values; public class extends baserichspout {// used to collect spout output tupleprivate spoutoutputcollector collector; // Private random rand; Private Static final long srialversionuid = 1l; @ overridepublic void nexttuple () {// string [] DATA = {"Hello zhangsan", "nice to meet", "You zhangsan hello", "Lisi welcome to BJ"}; // collector. emit (new values (data [Rand. nextint (data. length-1)]); string [] datas = {"Hello zhangsan Nice To Meet You zhangsan Hello Lisi welcome to BJ "}; values values = new values (datas [0]); // the collected data. emit (values); try {thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}/// initialization operation. Only @ overridepublic void open (MAP Conf, topologycontext context, spoutoutputcollector collector) {This. collector = collector;} // Add a unique identifier for the transmitted data, @ overridepublic void declareoutputfields (outputfieldsdeclarer declarer) {declarer. declare (new fields ("Spout "));}}

2. Write the code of the bolt component to cut fields.

Package com; 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; import Org. apache. storm. topology. base. baserichspout; import Org. apache. storm. tuple. fields; import Org. apache. storm. tuple. values; public class extends baserichspout {// used to collect spout output tupleprivate spoutoutputcollector collector; // Private random rand; Private Static final long srialversionuid = 1l; @ overridepublic void nexttuple () {// string [] DATA = {"Hello zhangsan", "nice to meet", "You zhangsan hello", "Lisi welcome to BJ"}; // collector. emit (new values (data [Rand. nextint (data. length-1)]); string [] datas = {"Hello zhangsan Nice To Meet You zhangsan Hello Lisi welcome to BJ "}; values values = new values (datas [0]); collector. emit (values); try {thread. sleep (1000);} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}/// initialization operation. Only @ overridepublic void open (MAP Conf, topologycontext context, spoutoutputcollector collector) {This. collector = collector ;}@ overridepublic void declareoutputfields (outputfieldsdeclarer declarer) {declarer. declare (new fields ("Spout "));}}

3. Write the code of the bolt component to count the number of fields.

package com;import java.util.HashMap;import java.util.Map;import org.apache.storm.task.OutputCollector;import org.apache.storm.task.TopologyContext;import org.apache.storm.topology.OutputFieldsDeclarer;import org.apache.storm.topology.base.BaseRichBolt;import org.apache.storm.tuple.Fields;import org.apache.storm.tuple.Tuple;import org.apache.storm.tuple.Values;public class WordCount extends BaseRichBolt{private static final Long SrialversionUID=1l;private OutputCollector collector;Map<String,Integer>map=new HashMap<String,Integer>();@Overridepublic void execute(Tuple value) {String data = value.getStringByField("word");if(map.containsKey(data)){map.put(data, map.get(data)+1);}else{map.put(data,1);} System.out.println(map);}@Overridepublic void prepare(Map arg0, TopologyContext arg1, OutputCollector collector) {this.collector=collector;}@Overridepublic void declareOutputFields(OutputFieldsDeclarer d) {//d.declare(new Fields("words","int"));}}

4. Write the submission class

Package com; import Org. apache. storm. config; import Org. apache. storm. localcluster; import Org. apache. storm. stormsubmitter; import Org. apache. storm. generated. alreadyaliveexception; import Org. apache. storm. generated. authorizationexception; import Org. apache. storm. generated. invalidtopologyexception; import Org. apache. storm. topology. topologybuilder; import Org. apache. storm. tuple. fields; public class Mian {public static void main (string [] ARGs) {topologybuilder = new topologybuilder (); topologybuilder. setspout ("Spout", new randomsentencespout (); topologybuilder. setbolt ("wordbolt", new wordbolt ()). shufflegrouping ("Spout"); topologybuilder. setbolt ("wordint", new wordcount ()). fieldsgrouping ("wordbolt", new fields ("word"); config Config = new config (); If (ARGs = NULL | args. length = 0 ){
// Cluster mode localcluster = new localcluster (); localcluster. submittopology ("wordcount", config, topologybuilder. createtopology ();} else {
// Standalone mode config. setnumworkers (1); try {stormsubmitter. submittopology (ARGs [0], config, topologybuilder. createtopology ();} catch (alreadyaliveexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (invalidtopologyexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (authorizationexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}}

5. compress the package into a jar package and upload it to the server for running. Note that only the class of the main class should be typed. do not join the jar in the project together. Otherwise, an error is reported on the cluster.

 

 

 

Storm practice wordcount

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.