Jstorm Series-1: Getting Started

Source: Internet
Author: User

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

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.