org.apache.storm.topology.IRichBolt Java Examples

The following examples show how to use org.apache.storm.topology.IRichBolt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Twister2Bolt.java    From twister2 with Apache License 2.0 6 votes vote down vote up
public Twister2Bolt(String id, Object bolt, MadeASourceListener madeASourceListener) {
  this.id = id;
  this.boltDeclarer = new Twister2BoltDeclarer(madeASourceListener);
  this.outFieldsForEdge = new EdgeFieldMap(Utils.getDefaultStream(id));
  this.keyedOutEdges = new EdgeFieldMap(Utils.getDefaultStream(id));
  if (bolt instanceof IRichBolt) {
    this.stormBolt = (IRichBolt) bolt;
    this.stormBolt.declareOutputFields(this.outFieldsForEdge);
  } else if (bolt instanceof BaseWindowedBolt) {
    this.stormWindowedBolt = (BaseWindowedBolt) bolt;
    this.stormWindowedBolt.declareOutputFields(this.outFieldsForEdge);
    this.stormWindowedBoltExecutor = new WindowedBoltExecutor(this.stormWindowedBolt);
  } else {
    this.stormBasicBolt = (IBasicBolt) bolt;
    this.stormBasicBolt.declareOutputFields(this.outFieldsForEdge);
  }
}
 
Example #2
Source File: JoinBoltTest.java    From bullet-storm with Apache License 2.0 5 votes vote down vote up
private static List<BulletRecord> sendRawRecordTuplesTo(IRichBolt bolt, String id, int n, int batchSize) {
    List<BulletRecord> sent = new ArrayList<>();
    for (int i = 0; i < n; i += batchSize) {
        BulletRecord[] batch = new BulletRecord[batchSize];
        for (int j = 0; j < batchSize; ++j) {
            batch[j] = RecordBox.get().add("field", String.valueOf(i + j)).getRecord();
        }
        Tuple tuple = TupleUtils.makeIDTuple(TupleClassifier.Type.DATA_TUPLE, id, getListBytes(batch));
        bolt.execute(tuple);
        sent.addAll(asList(batch));
    }
    return sent;
}
 
Example #3
Source File: JoinBoltTest.java    From bullet-storm with Apache License 2.0 5 votes vote down vote up
private static List<BulletRecord> sendSlidingWindowWithRawRecordTuplesTo(IRichBolt bolt, String id, int n) {
    BulletRecord[] sent = new BulletRecord[n];
    for (int i = 0; i < n; ++i) {
        sent[i] = RecordBox.get().add("field", String.valueOf(i)).getRecord();
    }
    byte[] listBytes = getListBytes(sent);
    byte[] dataBytes = SerializerDeserializer.toBytes(new SlidingRecord.Data(sent.length, listBytes));
    Tuple tuple = TupleUtils.makeIDTuple(TupleClassifier.Type.DATA_TUPLE, id, dataBytes);
    bolt.execute(tuple);
    return asList(sent);
}
 
Example #4
Source File: WordCountToBolt.java    From storm-net-adapter with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();

    // Redis config parameters for the RedisStoreBolt
    JedisPoolConfig poolConfig = new JedisPoolConfig.Builder()
        .setHost("127.0.0.1").setPort(6379).build();
    // Storm tuple to redis key-value mapper
    RedisStoreMapper storeMapper = new WordCountStoreMapper();
    // The redis bolt (sink)
    IRichBolt redisStoreBolt = new RedisStoreBolt(poolConfig, storeMapper);

    // A stream of words
    builder.newStream(new TestWordSpout(), new ValueMapper<String>(0))
           /*
            * create a stream of (word, 1) pairs
            */
           .mapToPair(w -> Pair.of(w, 1))
           /*
            * aggregate the count
            */
           .countByKey()
           /*
            * The result of aggregation is forwarded to
            * the RedisStoreBolt. The forwarded tuple is a
            * key-value pair of (word, count) with ("key", "value")
            * being the field names.
            */
           .to(redisStoreBolt);

    Config config = new Config();
    String topoName = "test";
    if (args.length > 0) {
        topoName = args[0];
    }
    config.setNumWorkers(1);
    StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
}
 
Example #5
Source File: JoinBoltTest.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
private static List<BulletRecord> sendRawRecordTuplesTo(IRichBolt bolt, String id, int n) {
    return sendRawRecordTuplesTo(bolt, id, n, 1);
}
 
Example #6
Source File: JoinBoltTest.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
private static List<BulletRecord> sendRawRecordTuplesTo(IRichBolt bolt, String id) {
    return sendRawRecordTuplesTo(bolt, id, RAW_MAX_SIZE);
}
 
Example #7
Source File: JoinBoltTest.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
private static void sendRawByteTuplesTo(IRichBolt bolt, String id, List<byte[]> data) {
    for (byte[] b : data) {
        Tuple tuple = TupleUtils.makeIDTuple(TupleClassifier.Type.DATA_TUPLE, id, b);
        bolt.execute(tuple);
    }
}
 
Example #8
Source File: CustomBoltDeclarer.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
public CustomBoltDeclarer(String id, IRichBolt bolt, Number parallelism) {
    this.id = id;
    this.bolt = bolt;
    this.parallelism = parallelism;
}
 
Example #9
Source File: ComponentUtils.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
public static <T extends IRichBolt> T prepare(Map config, T bolt, TopologyContext context, IOutputCollector collector) {
    bolt.prepare(config, context, new OutputCollector(collector));
    return bolt;
}
 
Example #10
Source File: ComponentUtils.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
public static <T extends IRichBolt> T prepare(Map config, T bolt, CustomCollector collector) {
    return prepare(config, bolt, mock(TopologyContext.class), collector);
}
 
Example #11
Source File: ComponentUtils.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
public static <T extends IRichBolt> T prepare(T bolt, CustomCollector collector) {
    return prepare(new HashMap<>(), bolt, mock(TopologyContext.class), collector);
}
 
Example #12
Source File: CustomTopologyBuilder.java    From bullet-storm with Apache License 2.0 4 votes vote down vote up
@Override
public BoltDeclarer setBolt(String id, IRichBolt bolt, Number parallelism) throws IllegalArgumentException {
    CustomBoltDeclarer declarer = new CustomBoltDeclarer(id, bolt, parallelism);
    createdBolts.add(declarer);
    return declarer;
}
 
Example #13
Source File: StreamBuilder.java    From incubator-heron with Apache License 2.0 4 votes vote down vote up
protected void buildStreams(EcoExecutionContext executionContext, TopologyBuilder builder,
                            ObjectBuilder objectBuilder)
    throws IllegalAccessException, InstantiationException, ClassNotFoundException,
    NoSuchFieldException, InvocationTargetException {
  EcoTopologyDefinition topologyDefinition = executionContext.getTopologyDefinition();
  Map<String, ComponentStream> componentStreams = new HashMap<>();

  HashMap<String, BoltDeclarer> declarers = new HashMap<>();
  for (StreamDefinition stream : topologyDefinition.getStreams()) {
    Object boltObj = executionContext.getBolt(stream.getTo());
    BoltDeclarer declarer = declarers.get(stream.getTo());
    if (boltObj instanceof IRichBolt) {
      if (declarer == null) {
        declarer = builder.setBolt(stream.getTo(),
            (IRichBolt) boltObj,
            topologyDefinition.parallelismForBolt(stream.getTo()));
        declarers.put(stream.getTo(), declarer);
      }
    } else if (boltObj instanceof IBasicBolt) {
      if (declarer == null) {
        declarer = builder.setBolt(
            stream.getTo(),
            (IBasicBolt) boltObj,
            topologyDefinition.parallelismForBolt(stream.getTo()));
        declarers.put(stream.getTo(), declarer);
      }
    } else if (boltObj instanceof IWindowedBolt) {
      if (declarer == null) {
        declarer = builder.setBolt(
            stream.getTo(),
            (IWindowedBolt) boltObj,
            topologyDefinition.parallelismForBolt(stream.getTo()));
        declarers.put(stream.getTo(), declarer);
      }
    }  else {
      throw new IllegalArgumentException("Class does not appear to be a bolt: "
          + boltObj.getClass().getName());
    }

    GroupingDefinition grouping = stream.getGrouping();
    // if the streamId is defined, use it for the grouping,
    // otherwise assume default stream
    String streamId = grouping.getStreamId() == null
        ? Utils.DEFAULT_STREAM_ID : grouping.getStreamId();


    switch (grouping.getType()) {
      case SHUFFLE:
        declarer.shuffleGrouping(stream.getFrom(), streamId);
        break;
      case FIELDS:
        List<String> groupingArgs = grouping.getArgs();
        if (groupingArgs == null) {
          throw new IllegalArgumentException("You must supply arguments for Fields grouping");
        }
        declarer.fieldsGrouping(stream.getFrom(), streamId, new Fields(groupingArgs));
        break;
      case ALL:
        declarer.allGrouping(stream.getFrom(), streamId);
        break;
      case GLOBAL:
        declarer.globalGrouping(stream.getFrom(), streamId);
        break;
      case NONE:
        declarer.noneGrouping(stream.getFrom(), streamId);
        break;
      case CUSTOM:
        declarer.customGrouping(stream.getFrom(), streamId,
            buildCustomStreamGrouping(stream.getGrouping().getCustomClass(),
                executionContext,
                objectBuilder));
        break;
      default:
        throw new UnsupportedOperationException("unsupported grouping type: " + grouping);
    }
  }
  executionContext.setStreams(componentStreams);
}
 
Example #14
Source File: RulesTopologyTest.java    From streamline with Apache License 2.0 4 votes vote down vote up
protected IRichBolt createRulesBolt(RulesProcessor rulesProcessor, RuleProcessorRuntime.ScriptType scriptType) {
    return new RulesBolt(rulesProcessor, scriptType);
}
 
Example #15
Source File: TestBolt.java    From elasticsearch-hadoop with Apache License 2.0 4 votes vote down vote up
public TestBolt(IRichBolt delegate) {
    this.delegate = delegate;
}