Storm-wordcount Example

Source: Internet
Author: User
Tags emit

1. Define topology:

public class Topologymain {
public static void Main (string[] args) throws Alreadyaliveexception, Invalidtopologyexception, interruptedexception {
Spout
Wordcountspout wcs = new Wordcountspout ();
Bolts
Wordgroupbolt WGB = new Wordgroupbolt ();
Wordcountbolt WCB = new Wordcountbolt ();
Reportbolt RB = new Reportbolt ();
Topology
Topologybuilder TB = new Topologybuilder ();
Tb.setspout ("Word-spout", WCS);
Tb.setbolt ("Group-bolt", WGB). Shufflegrouping ("Word-spout");
Tb.setbolt ("Count-bolt", WCB). Fieldsgrouping ("Group-bolt", New fields ("word"));
Tb.setbolt ("Report-bolt", RB). Globalgrouping ("Count-bolt");
Config
Config config = new config ();
Stormsubmitter.submittopology ("word-count-topology", config, tb.createtopology ());
Thread.Sleep (10000);
}
}

2, Spout:

public class Wordcountspout extends Baserichspout {

Private static final long serialversionuid = 1L;
Analog data source, each issue sentence as a tuple
Private string[] Wordarr = {"My dog has fleas", "I like cold beverages", "the dog ate my homework"};
Private Spoutoutputcollector collector;
private int index = 0;

@Override
public void Open (Map conf, topologycontext context, Spoutoutputcollector collector) {//spout called when initializing
This.collector = collector;
}

@Override
public void Nexttuple () {//Output tuple
This.collector.emit (New Values (Wordarr[index));
Index + +;
if (index >= wordarr.length) {
index = 0;
}
}

@Override
public void Declareoutputfields (Outputfieldsdeclarer declarer) {
Declarer.declare ("word");//Set the fields that each emitted tuple contains, which contains only one word field
}

}

3. Bolt:

public class Wordgroupbolt extends Baserichbolt {

Private static final long serialversionuid = 1L;
Private Outputcollector collector;

@Override
public void Prepare (Map stormconf, Topologycontext context, Outputcollector collector) {
This.collector = collector;
}

@Override
public void execute (Tuple input) {
String Word = Input.getstringbyfield ("word");
string[] Words = Word.split ("");
for (String w:words) {
This.collector.emit (New Values (w));
}
}

@Override
public void Declareoutputfields (Outputfieldsdeclarer declarer) {
Declarer.declare ("word");//corresponds to the field defined in spout
}

}

public class Wordcountbolt extends Baserichbolt {

Private static final long serialversionuid = 1L;
Private Outputcollector collector;
Private hashmap<string, long> countmap = null;

@Override
public void Prepare (Map stormconf, Topologycontext context, Outputcollector collector) {
This.collector = collector;
This.countmap = new hashmap<string, long> ();
}

@Override
public void execute (Tuple input) {
String Word = Input.getstringbyfield ("word");
Long count = This.countMap.get (word);
if (null = = count) {
Count = 0L;
}
Count + +;
This.countMap.put (Word, count);
This.collector.emit (New Values (Word, count));
}

@Override
public void Declareoutputfields (Outputfieldsdeclarer declarer) {
Declarer.declare (New fields ("word", "count"));
}

}

public class Reportbolt extends Baserichbolt {

Private static final long serialversionuid = 1L;
Private hashmap<string, long> countmap = null;
private static final Logger Logger = Loggerfactory.getlogger (Reportbolt.class);

@Override
public void Prepare (Map stormconf, Topologycontext context, Outputcollector collector) {
This.countmap = new hashmap<string, long> ();
}

@Override
public void execute (Tuple input) {
Logger.info ("Reportbolt print Result:");
String Word = Input.getstringbyfield ("word");
Long count = Input.getlongbyfield ("Count");
This.countMap.put (Word, count);
Logger.info (Word + "-----------------" + count);
}

@Override
public void Declareoutputfields (Outputfieldsdeclarer declarer) {

}

@Override
public void Cleanup () {
Super.cleanup ();
}

}

View the log, which can be queried through the UI, such as ip:8080 can see the submitted topology name, point in, the point port number can be viewed on the day

You can also log into the Storm installation directory of the corresponding work node under the Logs folder, each port number will generate a log file to view the corresponding port number of the log file to view the printing information

Storm-wordcount Example

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.