Strom Learning 01--Example Exclamationtopology__storm

Source: Internet
Author: User
Tags ack emit
Case Code

The simplest case of this storm is obtained from the examples of the installation package, and this example is completely based on Java.

Package storm.starter;
Import Backtype.storm.Config;
Import Backtype.storm.LocalCluster;
Import Backtype.storm.StormSubmitter;
Import Backtype.storm.task.OutputCollector;
Import Backtype.storm.task.TopologyContext;
Import Backtype.storm.testing.TestWordSpout;
Import Backtype.storm.topology.OutputFieldsDeclarer;
Import Backtype.storm.topology.TopologyBuilder;
Import Backtype.storm.topology.base.BaseRichBolt;
Import Backtype.storm.tuple.Fields;
Import Backtype.storm.tuple.Tuple;
Import backtype.storm.tuple.Values;

Import Backtype.storm.utils.Utils;

Import Java.util.Map;
 /** * is a basic example of Storm topology. */public class Exclamationtopology {public static class Exclamationbolt extends Baserichbolt {outputcollector _c

    Ollector; @Override public void Prepare (MAP conf, topologycontext context, Outputcollector collector) {_collector = Colle
    ctor @Override public void Execute (Tuple Tuple) {_collector.emit (Tuple, New Values (Tuple. getString (0) + "!!!"));
    _collector.ack (tuple); @Override public void Declareoutputfields (Outputfieldsdeclarer declarer) {declarer.declare (New Fields ("W
    Ord ")); } public static void Main (string[] args) throws Exception {//Create a topology map Topologybuilder builder = new Topol

    Ogybuilder ();
    Set the source of the data according to the Dag Strom graph Builder.setspout ("word", new Testwordspout (), 10);
    Processing Business Builder.setbolt ("Exclaim1", New Exclamationbolt (), 3). shufflegrouping ("word");


    Builder.setbolt ("exclaim2", New Exclamationbolt (), 2). shufflegrouping ("exclaim1");
    Config conf = new config ();

    Conf.setdebug (TRUE);

      if (args!= null && args.length > 0) {conf.setnumworkers (3);
    Stormsubmitter.submittopologywithprogressbar (Args[0], conf, builder.createtopology ());
      else {localcluster cluster = new Localcluster ();
      Cluster.submittopology ("Test", Conf, Builder.createtopology ());
  Utils.sleep (10000);    Cluster.killtopology ("test");
    Cluster.shutdown ();
 }
  }
}
Strom programming Model:


-DAG non-circular graph
-Spout Data source
-Bolt processing Business
First, Topologybuilder

Topologybuilder is to create a topology for storm to perform operations, topologies the bottom is Thrift structures, but because Thrift structures structure is very redundant, so Topologybuilder simplifies the creation of topology and creates topology in a very formal format.
Create Topologybuilder 1.1, create a topology map

    Topologybuilder builder = new Topologybuilder ();
1.2, the Topologybuilder has four member variables


Using the map store bolt, spout, we can see that when setting bolt,spout, the corresponding component will be obtained according to the ID.
The latter two did not provide a public access method.

    Private map<string, irichbolt> _bolts = new hashmap<string, irichbolt> ();
    Private map<string, irichspout> _spouts = new hashmap<string, irichspout> ();

    Private map<string, componentcommon> _commons = new hashmap<string, componentcommon> ();
    Private map<string, statespoutspec> _statespouts = new hashmap<string, statespoutspec> ();
1.3, set spout, the source of the data


Setspout set spout by Topologybuilder call,
The source code:
Set a spout for topology set a number of parallel tasks (Parallelism_hint),
If spout sets itself as non-distributed, the number of tasks will not work. Only one task will be assigned to the component.
ID: The representation of the component that uses the output of the component for other components as a marker for the data.
Spout: Data source spout
Parallelism_hint: Number of tasks

  Public Spoutdeclarer setspout (String ID, irichspout spout, number Parallelism_hint) {
        validateunusedid (ID); Guaranteed ID Uniqueness
        Initcommon (ID, spout, parallelism_hint);
        _spouts.put (ID, spout);//join Spout's map return
        new Spoutgetter (ID);
    }
set spout in case
    Set the source of the data  according to the Dag Strom graph
    builder.setspout ("word", new Testwordspout (), 10);

A spoutoutputcollector _collector is provided using the Spout:new testwordspout () provided by storm; As
spout the output container, the next component obtains resources from his outputcoloter (tuples), spoutoutputcollector

/** * This output collector exposes to the API for emitting tuples from a {@link backtype.storm.topology.IRichSpout}. * The main difference between this output collector and {@link Outputcollector} * for {@link backtype.storm.topology.IRic Hbolt} is this spouts can tag messages with IDs So, they can be * acked or failed in.
 This is the spout portion of the Storm's API to * guarantee which is fully processed at least.

    * * Public class Spoutoutputcollector implements Ispoutoutputcollector {Ispoutoutputcollector _delegate;
    Public Spoutoutputcollector (Ispoutoutputcollector delegate) {_delegate = delegate;
     }/** * Emits a new tuple to the specified output stream with the given message ID. * When Storm detects so this is tuple has been fully processed, or has failed * to be fully processed, the spout would Receive an ACK or fail callback respectively * with the MessageID as long as the MessageID is not null. If the MeSsageid was null, * Storm won't track the tuple and no callback would be received.
     The emitted values must be * immutable. * @return The "List of task IDs" This tuple is sent to */public list<integer> emit (String Strea
    MId, list<object> tuple, Object MessageID) {return _delegate.emit (streamid, tuple, MessageID); }/** * Emits a tuple to the specified task on the specified output stream. This output * Stream must have been declared as a direct stream, and the specified task must * use a direct grou Ping on this stream to receive the message.
     The emitted values must be * immutable. */Public list<integer> emit (String streamid, list<object> tuple, Object MessageID) {return _dele
    Gate.emit (Streamid, tuple, MessageID);
    @Override public void ReportError (Throwable error) {_delegate.reporterror (error); }
1.4, set up the Blot business processing

EXCAIM1 the spout data as input.

    Processing business
    Builder.setbolt ("Exclaim1", New Exclamationbolt (), 3). shufflegrouping ("word");
    Builder.setbolt ("exclaim2", New Exclamationbolt (), 2). shufflegrouping ("exclaim1");
Custom Bolt Exclamationbolt

Exclamaitionbolt also has a outputcollector as the output container.

  public static class Exclamationbolt extends Baserichbolt {
    outputcollector _collector;

    @Override public
    Void Prepare (Map conf, topologycontext context, Outputcollector collector) {
      _collector = Collector;
    }

    @Override public
    void execute (Tuple Tuple) {
      _collector.emit (Tuple, New Values (tuple.getstring (0) + "!!!"));
      _collector.ack (tuple);
    }

    @Override public
    void Declareoutputfields (Outputfieldsdeclarer declarer) {
      declarer.declare (new Fields (" Word));
    }


  
load configuration, execute
    Config conf = new config ();
    Conf.setdebug (true);

    if (args!= null && args.length > 0) {
      conf.setnumworkers (3);
      Cluster mode
      Stormsubmitter.submittopologywithprogressbar (Args[0], conf, builder.createtopology ());
    }
    else {
      //submit local cluster mode
      localcluster cluster = new Localcluster ();
      Cluster.submittopology ("Test", Conf, Builder.createtopology ());
      Utils.sleep (10000);
      Cluster.killtopology ("test");
      Cluster.shutdown ();
    }

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.