With the basic concept, let's use Jstorm to do a little bit of things.
Do a very boring thing: given a timestamp, output the corresponding greeting
The rules are: the 10 digits corresponding to the timestamp correspond to different time periods, 0-2 for the morning, 3 for noon, 4-6 for the afternoon, and 7-9 for the evening, respectively, for the morning, noon, afternoon and evening.
We use spout to send timestamps, bolts to process timestamps and output the corresponding greeting, and count the number of greetings for each time period to determine whether the timestamp generation is random.
The code is as follows:
Public classTimestampspoutImplementsirichspout{ Private Static Final LongSerialversionuid = 1L; PrivateSpoutoutputcollector collector; @Override .... All the unused functions don't show@Override Public voidnexttuple () { Longnow =System.currenttimemillis (); Values tuple=NewValues (now); System.out.println ("Spout:" +tuple); This. Collector.emit (tuple); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {E.printstacktrace (); }} @Override Public voidOpen (Map conf, topologycontext context, Spoutoutputcollector collector) { This. Collector =collector; } @Override Public voiddeclareoutputfields (Outputfieldsdeclarer declarer) {Declarer.declare (NewFields ("Timestamp")); }}
Public classGreetingboltImplementsirichbolt{Private Static Final LongSerialversionuid = 1L; Private Staticmap<string, integer> count =NewTreemap<string, integer>(); Private FinalString morning = "Morning"; Private FinalString noon = "Noon"; Private FinalString afternoon = "Afternoon"; Private FinalString evening = "Evening"; Private StaticInteger Total = 0; @Override Public voidCleanup () {} @Override Public voidExecute (Tuple input) {LongTimestamp= Input.getlong (0);//System.out.println ("Bolt:" +timestamp);Total+=1;//get 10-digit number LongSecond = (TIMESTAMP/10)%10; if(second<3) {System.out.println ("Bolt:" +morning); Count.put (morning, (Count.get (morning)==NULL)? 1:count.get (morning) +1); } Else if(second==3) {System.out.println ("Bolt:" +noon); Count.put (Noon, (Count.get (noon)==NULL)? 1:count.get (Noon) +1); } Else if(second<8) {System.out.println ("Bolt:" +afternoon); Count.put (Afternoon, (Count.get (afternoon)==NULL)? 1:count.get (afternoon) +1); } Else{System.out.println ("Bolt:" +evening); Count.put (Evening, (Count.get (evening)==NULL? 1:count.get (Evening) +1)); } if(total%10==0) {System.out.println ("Distribution Show as followed:"); System.out.println (Morning+ ":" +1.0* ((Count.get (morning) = =NULL)? 0:count.get (morning))/Total ); System.out.println (Noon+ ":" +1.0* ((Count.get (noon) = =NULL? 0:count.get (Noon))/Total ); System.out.println (Afternoon+ ":" +1.0* ((Count.get (afternoon) = =NULL? 0:count.get (afternoon))/Total ); System.out.println (Evening+ ":" +1.0* ((Count.get (evening) = =NULL? 0:count.get (evening))/Total ); } }}
Public classTesttopology { Public Static voidMain (string[] args) {Topologybuilder builder=NewTopologybuilder (); Builder.setspout ("Timestampspout",Newtimestampspout ()); Builder.setbolt ("Greetingbolt",NewGreetingbolt ()). Shufflegrouping ("Timestampspout"); Localcluster Cluster=NewLocalcluster (); Config config=NewConfig (); Cluster.submittopology ("Test", config, builder.createtopology ()); }}
P
Jstorm Series-1: Getting Started