Create a Maven project and add the following configuration to Pom. xml:
<dependency> <groupId>org.apache.storm</groupId> <artifactId>storm-core</artifactId> <type>jar</type> <version>0.9.3-rc1</version> </dependency>
Create the simplespout class to obtain data streams:
1 package COM. hirain. storm. helloworld; 2 3 Import Java. util. map; 4 Import Java. util. random; 5 6 Import backtype. storm. spout. spoutoutputcollector; 7 Import backtype. storm. task. topologycontext; 8 Import backtype. storm. topology. outputfieldsdeclarer; 9 Import backtype. storm. topology. base. baserichspout; 10 Import backtype. storm. tuple. fields; 11 import backtype. storm. tuple. values; 12 13 public class Simples Pout extends baserichspout {14 15/** 16*17 */18 Private Static final long serialversionuid = 1l; 19 20 // tool class for data transmission 21 private spoutoutputcollector collector; 22 23 Private Static string [] info = new string [] {24 "comaple \ t, 12424, 44w46, 654,12424, 44w46, 654,", 25 "Lisi \ t, 435435,6537, 12424, 44w46, 654, ", 26" Lipeng \ t, 45735,6757, 12424, 44w46, 654, ", 27" hujintao \ t, 45735,6757, 12424, 44w46, 654 ,", 28 "Jiangmin/t, 235 45,6457, 2455,7576, qr44453 ", 29" Beijing \ t, 435435,6537, 12424, 44w46, 654, ", 30" Xiaoming \ t, 46654,8579, w3675, large ,", 31 "Xiaozhang \ t, 9789,788,979 78,656, 345235,09889,", 32 "CEO \ t, 46654,8579, w3675, clerk,", 33 "CTO \ t, 46654,8579, w3675, clerk, ", 34" zhansan \ t, 46654,8579, w3675, 85877,077998, "}; 35 36 random = new random (); 37 38 39/** 40 * is called in the spouttracker class. Each call can send a piece of data (a tuple element) to the storm cluster. Group), this method will be continuously called 41 */42 Public void nexttuple () {43 try {44 string MSG = info [random. nextint (11)]; 45 // call the emission method 46 collector. emit (new values (MSG); 47 // simulate waiting for 100ms48 thread. sleep (100); 49} catch (interruptedexception e) {50 e. printstacktrace (); 51} 52} 53/** 54 * initialize collector55 */56 public void open (MAP Conf, topologycontext context, spoutoutputcollector collector) {57 This. collector = collector; 58 59} 6 0 61 62/** 63 * defines the field ID. This ID is useless in simple mode, but it is very useful in field grouping mode. 64 * The declarer variable plays a major role. We can also call declarer. declarestream (); To define stramid, which can be used to define a more complex stream topology 65 */66 public void declareoutputfields (outputfieldsdeclarer declarer) {67 declarer. declare (new fields ("Source"); // collector. emit (new values (MSG); the parameter must correspond to 68} 69 70}
Create a simplebolt class to process data:
1 package com.hirain.storm.helloworld; 2 3 import backtype.storm.topology.BasicOutputCollector; 4 import backtype.storm.topology.OutputFieldsDeclarer; 5 import backtype.storm.topology.base.BaseBasicBolt; 6 import backtype.storm.tuple.Fields; 7 import backtype.storm.tuple.Tuple; 8 import backtype.storm.tuple.Values; 9 10 11 12 public class SimpleBolt extends BaseBasicBolt {13 14 /**15 * 16 */17 private static final long serialVersionUID = 1L;18 19 public void execute(Tuple input,BasicOutputCollector collector) {20 try {21 String msg = input.getString(0);22 if (msg != null){23 //System.out.println("msg="+msg);24 collector.emit(new Values(msg + "msg is processed!"));25 }26 27 } catch (Exception e) {28 e.printStackTrace(); 29 }30 31 }32 33 public void declareOutputFields(34 OutputFieldsDeclarer declarer) {35 declarer.declare(new Fields("info"));36 37 }38 39 }
Create the main method to configure storm topology and start running in Local Mode:
1 package COM. hirain. storm. helloworld; 2 3 Import backtype. storm. config; 4 Import backtype. storm. localcluster; 5 import backtype. storm. stormsubmitter; 6 Import backtype. storm. topology. topologybuilder; 7 8 public class simpletopology {9 10 11 public static void main (string [] ARGs) {12 try {13 // instantiate the topologybuilder class. 14 topologybuilder = new topologybuilder (); 15 // set the number of concurrent nodes and allocate them. This number controls the number of threads of the object in the cluster. 16 topologybuilder. setspout ("simplespout", new simplespout (), 1); 17 // set the data processing node and allocate concurrency. Specify the policy that the node receives the eruption node as a random mode. 18 topologybuilder. setbolt ("simplebolt", new simplebolt (), 3 ). shufflegrouping ("simplespout"); 19 config Config = new config (); 20 config. setdebug (true); 21 if (ARGs! = NULL & args. length> 0) {22 config. setnumworkers (1); 23 stormsubmitter. submittopology (ARGs [0], config, topologybuilder. createtopology (); 24} else {25 // here is the startup code running in local mode. 26 config. setmaxtaskparallelism (1); 27 localcluster cluster = new localcluster (); 28 cluster. submittopology ("simple", config, topologybuilder. createtopology (); 29} 30 31} catch (exception e) {32 E. printstacktrace (); 33} 34} 35}
The preceding simple helloworld of storm is for reference only.
Storm getting started-Local Mode helloworld