Getting Started demo storm

Getting Started demo introduces a .storm

       storm entry helloworld There are two ways, one is the local, the other is remote.

  Local implementation:

    Local Written after demo, do not put up storm cluster, download storm-related jar package to implement the relevant operation of storm

  Remote implementation:

    After local written Demo, packets need to be labeled jar, then the jar package can be run nimbus

    Local Packaging Considerations:

      Since the jar package will lay it onto the storm clusters, so the storm already contains relevant operating environment, but requires storm-core arranged by packing provided scope when Maven, need not be related to the storm-core class into the jar package, in order to avoid conflicts

 

II. For local demo of

  Download storm required jar package storm-core

Data sources to achieve 1.spout

 


public
class RandomStringSpout extends BaseRichSpout{ private final static Map<Integer,String> map = new HashMap<Integer, String>(); private SpoutOutputCollector collector; public RandomStringSpout(){ map.put(0, "kafka"); map.put(1, "nifi"); map.put(2, "flink"); map.put(3, "storm"); map.put(4, "spark"); } //在Spout组件初始化时被调用 public voidOpen (the arg0 the Map, TopologyContext topologyContextrg1, SpoutOutputCollector spoutOutputCollector) { System.err.println ( "============== Open" ); the this .collector = spoutOutputCollector; } // nextTuple () method It is the core Spout achieve. // is mainly performing a method for outputting information, transmitted through collector.emit method public void nextTuple () { // send data collector.emit ( new new Values (as map.get (ThreadLocalRandom.current (). The nextInt (. 4 ) ))); the try { TimeUnit.SECONDS.sleep ( . 5 ); } the catch(InterruptedException E) { e.printStackTrace (); } } // for declaring the data format, i.e., a Tuple outputted, comprising several fields public void declareOutputFields (OutputFieldsDeclarer declarer) { declarer.declare ( new new Fields ( "Stream" )); } }

 

2.Bolt data filtering

public class WrapStarBolt extends BaseBasicBolt{

    public void execute(Tuple tuple, BasicOutputCollector Collector) {
        String value = tuple.getStringByField("stream");
        System.err.println("******"+value);
    }

    public void declareOutputFields(OutputFieldsDeclarer declarer) {
         //nothing to do 
    }
}
public class WrapWellBolt extends BaseBasicBolt{

    public void execute(Tuple tuple, BasicOutputCollector collector) {
        String value = tuple.getStringByField("stream");
        System.err.println("#######"+value);
    }

    public void declareOutputFields(OutputFieldsDeclarer arg0) {
        //nothing to do 
    }
}

3. Create a topology

//所有的spout bolt 会组成一个topology
public
class RadomStringTopologyLocal { public static void main(String[] args) throws InterruptedException { TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("RandomStringSpout", new RandomStringSpout()); builder.setBolt("wrapStarBolt", new WrapStarBolt()).shuffleGrouping("RandomStringSpout"); builder.setBolt("wrapWellBolt", new WrapWellBolt()).shuffleGrouping("RandomStringSpout"); Config config = new new config (); config.setDebug ( to true ); LocalCluster Cluster = new new LocalCluster (); cluster.submitTopology ( "RadomStringTopologyLocal" , config, builder.createTopology ()); System.err.println ( "The First Topology Start running local AT iS " ); TimeUnit.SECONDS.sleep ( 30 ); cluster.killTopology ( " RadomStringTopologyLocal " ); cluster.shutdown (); } }
// from the results it can be seen running write operation has demo

III. The remote demo

  1. On a

 

 

 

 

      

Guess you like

Origin www.cnblogs.com/MrRightZhao/p/11005174.html