Package backtype.storm.topology

Examples of backtype.storm.topology.TopologyBuilder


    submitTopology(builder);

  }

  private static TopologyBuilder setupBuilder() throws Exception {
    TopologyBuilder builder = new TopologyBuilder();

    int writerParallel = JStormUtils.parseInt(
        conf.get("topology.writer.parallel"), 1);

    int spoutParallel = JStormUtils.parseInt(
        conf.get("topology.spout.parallel"), 1);

    builder.setSpout("MetaSpout", new MetaSpout(), spoutParallel);

    builder.setBolt(WRITER_COMPONENT, new WriterBolt(), writerParallel)
        .shuffleGrouping("MetaSpout");

    return builder;
  }
View Full Code Here


       
        return ret;
    }
   
    public StormTopology buildTopology() {       
        TopologyBuilder builder = new TopologyBuilder();
        Map<GlobalStreamId, String> batchIdsForSpouts = fleshOutStreamBatchIds(false);
        Map<GlobalStreamId, String> batchIdsForBolts = fleshOutStreamBatchIds(true);

        Map<String, List<String>> batchesToCommitIds = new HashMap<String, List<String>>();
        Map<String, List<ITridentSpout>> batchesToSpouts = new HashMap<String, List<ITridentSpout>>();
       
        // This set is used to indicate if the masterCoordSpout needs to
        // send out COMMIT_STREAM message in this batchGroup.
        Set<String> commitBatchGroup = new HashSet<String>();
       
        for(String id: _spouts.keySet()) {
            TransactionalSpoutComponent c = _spouts.get(id);
            if(c.spout instanceof IRichSpout) {
               
                //TODO: wrap this to set the stream name
                builder.setSpout(id, (IRichSpout) c.spout, c.parallelism);
            } else {
                String batchGroup = c.batchGroupId;
                if(!batchesToCommitIds.containsKey(batchGroup)) {
                    batchesToCommitIds.put(batchGroup, new ArrayList<String>());
                }
                batchesToCommitIds.get(batchGroup).add(c.commitStateId);

                if(!batchesToSpouts.containsKey(batchGroup)) {
                    batchesToSpouts.put(batchGroup, new ArrayList<ITridentSpout>());
                }
                batchesToSpouts.get(batchGroup).add((ITridentSpout) c.spout);
               
               
                BoltDeclarer scd =
                      builder.setBolt(spoutCoordinator(id), new TridentSpoutCoordinator(c.commitStateId, (ITridentSpout) c.spout))
                        .globalGrouping(masterCoordinator(c.batchGroupId), MasterBatchCoordinator.BATCH_STREAM_ID)
                        .globalGrouping(masterCoordinator(c.batchGroupId), MasterBatchCoordinator.SUCCESS_STREAM_ID);
               
                for(Map m: c.componentConfs) {
                    scd.addConfigurations(m);
                }
               
                Map<String, TridentBoltExecutor.CoordSpec> specs = new HashMap();
                specs.put(c.batchGroupId, new CoordSpec());
                BoltDeclarer bd = builder.setBolt(id,
                        new TridentBoltExecutor(
                          new TridentSpoutExecutor(
                            c.commitStateId,
                            c.streamName,
                            ((ITridentSpout) c.spout)),
                            batchIdsForSpouts,
                            specs),
                        c.parallelism);
                bd.allGrouping(spoutCoordinator(id), MasterBatchCoordinator.BATCH_STREAM_ID);
                bd.allGrouping(masterCoordinator(batchGroup), MasterBatchCoordinator.SUCCESS_STREAM_ID);
                if(c.spout instanceof ICommitterTridentSpout) {
                    bd.allGrouping(masterCoordinator(batchGroup), MasterBatchCoordinator.COMMIT_STREAM_ID);
                    if (commitBatchGroup.contains(batchGroup) == false) commitBatchGroup.add(batchGroup);
                }
                for(Map m: c.componentConfs) {
                    bd.addConfigurations(m);
                }
            }
        }
       
        for(String id: _batchPerTupleSpouts.keySet()) {
            SpoutComponent c = _batchPerTupleSpouts.get(id);
            SpoutDeclarer d = builder.setSpout(id, new RichSpoutBatchTriggerer((IRichSpout) c.spout, c.streamName, c.batchGroupId), c.parallelism);
           
            for(Map conf: c.componentConfs) {
                d.addConfigurations(conf);
            }
        }
               
        for(String id: _bolts.keySet()) {
            Component c = _bolts.get(id);
           
            Map<String, CoordSpec> specs = new HashMap();
           
            for(GlobalStreamId s: getBoltSubscriptionStreams(id)) {
                String batch = batchIdsForBolts.get(s);
                if(!specs.containsKey(batch)) specs.put(batch, new CoordSpec());
                CoordSpec spec = specs.get(batch);
                CoordType ct;
                if(_batchPerTupleSpouts.containsKey(s.get_componentId())) {
                    ct = CoordType.single();
                } else {
                    ct = CoordType.all();
                }
                spec.coords.put(s.get_componentId(), ct);
            }
           
            for(String b: c.committerBatches) {
                specs.get(b).commitStream = new GlobalStreamId(masterCoordinator(b), MasterBatchCoordinator.COMMIT_STREAM_ID);
            }
           
            BoltDeclarer d = builder.setBolt(id, new TridentBoltExecutor(c.bolt, batchIdsForBolts, specs), c.parallelism);
            for(Map conf: c.componentConfs) {
                d.addConfigurations(conf);
            }
           
            for(InputDeclaration inputDecl: c.declarations) {
               inputDecl.declare(d);
            }
           
            Map<String, Set<String>> batchToComponents = getBoltBatchToComponentSubscriptions(id);
            for(String b: batchToComponents.keySet()) {
                for(String comp: batchToComponents.get(b)) {
                    d.directGrouping(comp, TridentBoltExecutor.COORD_STREAM(b));
                }
            }
           
            for(String b: c.committerBatches) {
                d.allGrouping(masterCoordinator(b), MasterBatchCoordinator.COMMIT_STREAM_ID);
                if (commitBatchGroup.contains(b) == false) commitBatchGroup.add(b);
            }
        }
       
        for(String batch: batchesToCommitIds.keySet()) {
            List<String> commitIds = batchesToCommitIds.get(batch);
            boolean batchCommit = false;
            if (commitBatchGroup.contains(batch)) batchCommit = true;
            builder.setSpout(masterCoordinator(batch), new MasterBatchCoordinator(commitIds, batchesToSpouts.get(batch), batchCommit));
        }

        return builder.createTopology();
    }
View Full Code Here

  private TopologyBuilder topologyBuilder;

  private SpoutDeclarer spoutDeclarer;

  public BatchTopologyBuilder(String topologyName) {
    topologyBuilder = new TopologyBuilder();

    spoutDeclarer = topologyBuilder.setSpout(BatchDef.SPOUT_TRIGGER,
        new BatchSpoutTrigger(), 1);
  }
View Full Code Here

  private StormTopology createTopology(DRPCSpout spout) {
    final String SPOUT_ID = "spout";
    final String PREPARE_ID = "prepare-request";

    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout(SPOUT_ID, spout);
    builder.setBolt(PREPARE_ID, new PrepareRequest())
        .noneGrouping(SPOUT_ID);
    int i = 0;
    for (; i < _components.size(); i++) {
      Component component = _components.get(i);

      Map<String, SourceArgs> source = new HashMap<String, SourceArgs>();
      if (i == 1) {
        source.put(boltId(i - 1), SourceArgs.single());
      } else if (i >= 2) {
        source.put(boltId(i - 1), SourceArgs.all());
      }
      IdStreamSpec idSpec = null;
      if (i == _components.size() - 1
          && component.bolt instanceof FinishedCallback) {
        idSpec = IdStreamSpec.makeDetectSpec(PREPARE_ID,
            PrepareRequest.ID_STREAM);
      }
      BoltDeclarer declarer = builder.setBolt(boltId(i),
          new CoordinatedBolt(component.bolt, source, idSpec),
          component.parallelism);

      for (Map conf : component.componentConfs) {
        declarer.addConfigurations(conf);
      }

      if (idSpec != null) {
        declarer.fieldsGrouping(idSpec.getGlobalStreamId()
            .get_componentId(), PrepareRequest.ID_STREAM,
            new Fields("request"));
      }
      if (i == 0 && component.declarations.isEmpty()) {
        declarer.noneGrouping(PREPARE_ID, PrepareRequest.ARGS_STREAM);
      } else {
        String prevId;
        if (i == 0) {
          prevId = PREPARE_ID;
        } else {
          prevId = boltId(i - 1);
        }
        for (InputDeclaration declaration : component.declarations) {
          declaration.declare(prevId, declarer);
        }
      }
      if (i > 0) {
        declarer.directGrouping(boltId(i - 1),
            Constants.COORDINATED_STREAM_ID);
      }
    }

    IRichBolt lastBolt = _components.get(_components.size() - 1).bolt;
    OutputFieldsGetter getter = new OutputFieldsGetter();
    lastBolt.declareOutputFields(getter);
    Map<String, StreamInfo> streams = getter.getFieldsDeclaration();
    if (streams.size() != 1) {
      throw new RuntimeException(
          "Must declare exactly one stream from last bolt in LinearDRPCTopology");
    }
    String outputStream = streams.keySet().iterator().next();
    List<String> fields = streams.get(outputStream).get_output_fields();
    if (fields.size() != 2) {
      throw new RuntimeException(
          "Output stream of last component in LinearDRPCTopology must contain exactly two fields. The first should be the request id, and the second should be the result.");
    }

    builder.setBolt(boltId(i), new JoinResult(PREPARE_ID))
        .fieldsGrouping(boltId(i - 1), outputStream,
            new Fields(fields.get(0)))
        .fieldsGrouping(PREPARE_ID, PrepareRequest.RETURN_STREAM,
            new Fields("request"));
    i++;
    builder.setBolt(boltId(i), new ReturnResults()).noneGrouping(
        boltId(i - 1));
    return builder.createTopology();
  }
View Full Code Here

    return batchTopologyBuilder.getTopologyBuilder();
  }

  public static void SetLocalTopology() throws Exception {
    TopologyBuilder builder = SetBuilder();

    LocalCluster cluster = new LocalCluster();
    cluster.submitTopology(topologyName, conf, builder.createTopology());

    Thread.sleep(600000);

    cluster.shutdown();
  }
View Full Code Here

  }

  public static void SetRemoteTopology() throws AlreadyAliveException,
      InvalidTopologyException, TopologyAssignException {

    TopologyBuilder builder = SetBuilder();

    StormSubmitter.submitTopology(topologyName, conf,
        builder.createTopology());

  }
View Full Code Here

TOP

Related Classes of backtype.storm.topology.TopologyBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.