Eclipse-based storm application development and debugging under Windows

Source: Internet
Author: User

original articles, reproduced please specify: Reproduced from http://www.cnblogs.com/tovin/p/3971113.html

This article explains how to develop storm applications with a simple example.

1. Create Maven Project

To create a MAVEN project under Eclipse, you can refer to the http://www.cnblogs.com/tovin/p/3822985.html

  

2. Modify POM.XM Add Dependency Package

Use the Maven-assembly-plugin plugin to package the jar that the project relies on together

 Storm's <scope> Set up provided, primarily because storm packs are required for compilation, and do not package them when running in a storm cluster.

<Projectxmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelversion>4.0.0</modelversion>  <groupId>Com.test</groupId>  <Artifactid>Storm-example</Artifactid>  <version>0.0.1-snapshot</version>  <Packaging>Jar</Packaging>  <name>Storm-example</name>  <URL>http://maven.apache.org</URL>  <Properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </Properties>  <Dependencies>        <Dependency>           <groupId>Org.apache.storm</groupId>         <Artifactid>Storm-core</Artifactid>         <version>0.9.2-incubating</version>         <Scope>Provided</Scope>        </Dependency>  </Dependencies>    <Build>    <Plugins>      <plugin>        <Artifactid>Maven-assembly-plugin</Artifactid>        <version>2.4</version>        <Configuration>          <Descriptorrefs>            <Descriptorref>Jar-with-dependencies</Descriptorref>          </Descriptorrefs>        </Configuration>        <executions>          <Execution>            <ID>make-assembly</ID>             <Phase>Package</Phase>            <Goals>              <goal>Single</goal>            </Goals>          </Execution>        </executions>      </plugin>    </Plugins>  </Build></Project>

3, write topology

(1) Writing spout

ImportBacktype.storm.spout.SpoutOutputCollector;ImportBacktype.storm.task.TopologyContext;ImportBacktype.storm.topology.OutputFieldsDeclarer;Importbacktype.storm.topology.base.BaseRichSpout;ImportBacktype.storm.tuple.Fields;Importbacktype.storm.tuple.Values; Public classRandomspoutextendsbaserichspout{PrivateSpoutoutputcollector collector; Private Staticstring[] Words = {"Happy", "excited", "angry"}; /*(non-javadoc) * @see Backtype.storm.spout.ispout#open (Java.util.Map, Backtype.storm.task.TopologyContext, Backtype.storm.spout.SpoutOutputCollector)*/     Public voidOpen (Map arg0, Topologycontext arg1, Spoutoutputcollector arg2) {//TODO auto-generated Method Stub         This. Collector =arg2; }        /*(non-javadoc) * @see backtype.storm.spout.ispout#nexttuple ()*/     Public voidnexttuple () {//TODO auto-generated Method StubString Word = words[NewRandom (). Nextint (Words.length)]; Collector.emit (NewValues (word)); }        /*(non-javadoc) * @see Backtype.storm.topology.icomponent#declareoutputfields ( Backtype.storm.topology.OutputFieldsDeclarer)*/     Public voiddeclareoutputfields (Outputfieldsdeclarer arg0) {//TODO auto-generated Method StubArg0.declare (NewFields ("RandomString")); }}

(2) Writing bolts

ImportBacktype.storm.topology.BasicOutputCollector;ImportBacktype.storm.topology.OutputFieldsDeclarer;ImportBacktype.storm.topology.base.BaseBasicBolt;Importbacktype.storm.tuple.Tuple; Public classSenqueceboltextendsbasebasicbolt{/*(non-javadoc) * @see Backtype.storm.topology.ibasicbolt#execute (backtype.storm.tuple.Tuple, Backtype.storm.topology.BasicOutputCollector)*/     Public voidExecute (Tuple input, basicoutputcollector collector) {//TODO auto-generated Method StubString Word = (string) input.getvalue (0); String out= "I ' m" + Word + "!"; System.out.println ("Out=" +Out ); }        /*(non-javadoc) * @see Backtype.storm.topology.icomponent#declareoutputfields ( Backtype.storm.topology.OutputFieldsDeclarer)*/     Public voiddeclareoutputfields (Outputfieldsdeclarer declarer) {//TODO auto-generated Method Stub    }}

(3) Writing topo

Cluster and local two operating modes are available, so we can easily run Firsttopo locally to debug our program.

ImportBacktype.storm.Config;ImportBacktype.storm.LocalCluster;ImportBacktype.storm.StormSubmitter;ImportBacktype.storm.topology.TopologyBuilder;Importbacktype.storm.utils.Utils; Public classFirsttopo { Public Static voidMain (string[] args)throwsException {topologybuilder builder=NewTopologybuilder (); Builder.setspout ("Spout",Newrandomspout ()); Builder.setbolt ("Bolt",NewSenquecebolt ()). shufflegrouping ("spout"); Config conf=NewConfig (); Conf.setdebug (false); if(Args! =NULL&& args.length > 0) {conf.setnumworkers (3); Stormsubmitter.submittopology (args[0], conf, builder.createtopology ()); } Else{localcluster cluster=NewLocalcluster (); Cluster.submittopology ("Firsttopo", Conf, builder.createtopology ()); Utils.sleep (100000); Cluster.killtopology ("Firsttopo");          Cluster.shutdown (); }      }  }

(4) Operation result

Output from the local runtime, in Eclipse:

    

Run-time output in a storm cluster can be viewed through the Storm UI

    

original articles, reproduced please specify: Reproduced from http://www.cnblogs.com/tovin/p/3971113.html

Eclipse-based storm application development and debugging under Windows

Related Article

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.