backtype.storm.topology.BasicOutputCollector Java Examples

The following examples show how to use backtype.storm.topology.BasicOutputCollector. 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: ParseReferrerBolt.java    From aws-big-data-blog with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple input,  BasicOutputCollector collector) {
    Record record = (Record)input.getValueByField(DefaultKinesisRecordScheme.FIELD_RECORD);
    ByteBuffer buffer = record.getData();
    String data = null; 
    try {
        data = decoder.decode(buffer).toString();
        JSONObject jsonObject = new JSONObject(data);

        String referrer = jsonObject.getString("referrer");

        int firstIndex = referrer.indexOf('.');
        int nextIndex = referrer.indexOf('.',firstIndex+1);
        collector.emit(new Values(referrer.substring(firstIndex+1,nextIndex)));

    } catch (CharacterCodingException|JSONException|IllegalStateException e) {
        LOG.error("Exception when decoding record ", e);
    }
}
 
Example #2
Source File: CheckOrderBolt.java    From storm-kafka-examples with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
	String nowData = df.format(new Date()); // new Date()为获取当前系统时间,检测是否为最新数据

	String data = tuple.getString(0);
	//订单号		用户id	     原金额	                      优惠价	          标示字段		下单时间
	//id		memberid  	totalprice					preprice		sendpay		createdate
	if(data!=null && data.length()>0) {
		String[] values = data.split("\t");
		if(values.length==6) {
			String id = values[0];
			String memberid = values[1];
			String totalprice = values[2];
			String preprice = values[3];
			String sendpay = values[4];
			String createdate = values[5];
			
			if(StringUtils.isNotEmpty(id)&&StringUtils.isNotEmpty(memberid)&&StringUtils.isNotEmpty(totalprice)) {
				if(DateUtils.isValidDate(createdate, nowData)) {
					collector.emit(new Values(id,memberid,totalprice,preprice,sendpay,createdate));
				}
			}
		}
	}
}
 
Example #3
Source File: SVMBolt.java    From senti-storm with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
  String text = tuple.getStringByField("text");
  Map<Integer, Double> featureVector = (Map<Integer, Double>) tuple
      .getValueByField("featureVector");

  // Create feature nodes
  svm_node[] testNodes = new svm_node[featureVector.size()];
  int i = 0;
  for (Map.Entry<Integer, Double> feature : featureVector.entrySet()) {
    svm_node node = new svm_node();
    node.index = feature.getKey();
    node.value = feature.getValue();
    testNodes[i] = node;
    i++;
  }

  double predictedClass = svm.svm_predict(m_model, testNodes);

  if (m_logging) {
    LOG.info("Tweet: " + text + " predictedSentiment: "
        + SentimentClass.fromScore(m_dataset, (int) predictedClass));
  }
}
 
Example #4
Source File: FeatureGenerationBolt.java    From senti-storm with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
  String text = tuple.getStringByField("text");
  List<TaggedToken> taggedTokens = (List<TaggedToken>) tuple
      .getValueByField("taggedTokens");

  // Generate Feature Vector
  Map<Integer, Double> featureVector = m_fvg
      .generateFeatureVector(taggedTokens);

  if (m_logging) {
    LOG.info("Tweet: " + text + " FeatureVector: " + featureVector);
  }

  // Emit new tuples
  collector.emit(new Values(text, featureVector));
}
 
Example #5
Source File: SpeedLimitBolt.java    From ignite-book-code-samples with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
    String line = (String)tuple.getValue(0);
    if(!line.isEmpty()){
        String[] elements = line.split(",");
        // we are interested in speed and the car registration number
        int speed = Integer.valueOf((elements[1]).trim());
        String car = elements[0];
        if(speed > SPEED_THRESHOLD){
            TreeMap<String, Integer> carValue = new TreeMap<String, Integer>();
            carValue.put(car, speed);
            basicOutputCollector.emit(new Values(carValue));
            LOGGER.info("Speed violation found:"+ car + " speed:" + speed);
        }
    }
}
 
Example #6
Source File: WordCount.java    From ignite-book-code-samples with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    //Get the word contents from the tuple
    String word = tuple.getString(0);
    //Have we counted any already?
    Integer count = counts.get(word);
    if (count == null)
        count = 0;
    //Increment the count and store it
    count++;
    counts.put(word, count);
    //Emit the word and the current count
    //collector.emit(new Values(IGNITE_FIELD, count));
    TreeMap<String, Integer> words = new TreeMap<>();
    words.put(word,count);

    collector.emit(new Values(words));
    //Log information
    logger.info("Emitting a count of " + count + " for word " + word);
}
 
Example #7
Source File: SplitSentence.java    From ignite-book-code-samples with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    //Get the sentence content from the tuple
    String sentence = tuple.getString(0);
    //An iterator to get each word
    BreakIterator boundary=BreakIterator.getWordInstance();
    //Give the iterator the sentence
    boundary.setText(sentence);
    //Find the beginning first word
    int start=boundary.first();
    //Iterate over each word and emit it to the output stream
    for (int end = boundary.next(); end != BreakIterator.DONE; start=end, end=boundary.next()) {
        //get the word
        String word=sentence.substring(start,end);
        //If a word is whitespace characters, replace it with empty
        word=word.replaceAll("\\s+","");
        //if it's an actual word, emit it
        if (!word.equals("")) {
            collector.emit(new Values(word));
        }
    }
}
 
Example #8
Source File: ElasticSearchJsonBoltTest.java    From cognition with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute(
        @Injectable Tuple input,
        @Injectable BasicOutputCollector collector,
        @Injectable LogRecord logRecord) throws Exception {

    bolt.esJsonField = "field";

    new Expectations(bolt) {{
        input.getValueByField(AbstractLogRecordBolt.RECORD);
        result = logRecord;
        bolt.indexRecord(logRecord);
        logRecord.getValue(bolt.esJsonField);
        result = "value";

        collector.emit(new Values(logRecord));
        collector.emit(ElasticSearchJsonBolt.ES_JSON, new Values("value"));
    }};
    bolt.execute(input, collector);
}
 
Example #9
Source File: TransactionalWordsTest.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    TransactionAttempt attempt = (TransactionAttempt) tuple.getValue(0);
    int curr = tuple.getInteger(2);
    Integer prev = tuple.getInteger(3);

    int currBucket = curr / BUCKET_SIZE;
    Integer prevBucket = null;
    if (prev != null) {
        prevBucket = prev / BUCKET_SIZE;
    }

    if (prevBucket == null) {
        collector.emit(new Values(attempt, currBucket, 1));
    } else if (currBucket != prevBucket) {
        collector.emit(new Values(attempt, currBucket, 1));
        collector.emit(new Values(attempt, prevBucket, -1));
    }
}
 
Example #10
Source File: AuditLoginsCounterBolt.java    From Kafka-Storm-ElasticSearch with Apache License 2.0 6 votes vote down vote up
public void execute(Tuple input, BasicOutputCollector collector){
  	
  	Map<String,String> auditAttributes = (Map<String, String>) input.getValues().get(0);
  	
  	String host_user = new String(auditAttributes.get("host")).concat("|")
  			.concat(auditAttributes.get("user"));
  	String type = auditAttributes.get("type");
  	int counterValue = getCounterValue(host_user);
  	String document = "{\"counter\": ";
  	    	
  	if (type.equals("USER_LOGIN")){
  		counterValue++;
  		document = document + counterValue + "}";
        	collector.emit(tuple(host_user,index, this.type, document));
  	} else
if (type.equals("USER_LOGOUT") && counterValue > 0){
	counterValue--;
	document = document + counterValue + "}";
	collector.emit(tuple(host_user, index, this.type, document));
}
  }
 
Example #11
Source File: RollingSort.java    From storm-benchmark with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
  if (TupleHelpers.isTickTuple(tuple)) {
    Arrays.sort(data);
    basicOutputCollector.emit(new Values(data));
    LOG.info("index = " + index);
  } else {
    Object obj = tuple.getValue(0);
    if (obj instanceof Comparable) {
      data[index].set((Comparable) obj);
    } else {
      throw new RuntimeException("tuple value is not a Comparable");
    }
    index = (index + 1 == chunkSize) ? 0 : index + 1;
  }
}
 
Example #12
Source File: WordCountTest.java    From storm-benchmark with Apache License 2.0 6 votes vote down vote up
@Test
public void countBoltShouldCountAndEmitNumberOfEveryWord() {
  String[] words = { "word", "word", "count"};
  WordCount.Count bolt = new WordCount.Count();
  Tuple tuple = mock(Tuple.class);
  BasicOutputCollector collector = mock(BasicOutputCollector.class);

  for (String w : words) {
    when(tuple.getString(0)).thenReturn(w);
    bolt.execute(tuple, collector);
  }
  verify(collector, times(3)).emit(any(Values.class));

  assertThat(bolt.counts.get("word")).isEqualTo(2);
  assertThat(bolt.counts.get("count")).isEqualTo(1);
}
 
Example #13
Source File: TridentSpoutCoordinator.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    TransactionAttempt attempt = (TransactionAttempt) tuple.getValue(0);

    if (tuple.getSourceStreamId().equals(MasterBatchCoordinator.SUCCESS_STREAM_ID)) {
        _state.cleanupBefore(attempt.getTransactionId());
        _coord.success(attempt.getTransactionId());
    } else {
        long txid = attempt.getTransactionId();
        Object prevMeta = _state.getPreviousState(txid);
        Object meta = _coord.initializeTransaction(txid, prevMeta, _state.getState(txid));
        _state.overrideState(txid, meta);
        collector.emit(MasterBatchCoordinator.BATCH_STREAM_ID, new Values(attempt, meta));
    }

}
 
Example #14
Source File: SplitRecord.java    From jstorm with Apache License 2.0 6 votes vote down vote up
public void execute(Tuple tuple, BasicOutputCollector collector) {
    tpsCounter.count();
    
    Long tupleId = tuple.getLong(0);
    Object obj = tuple.getValue(1);
    
    if (obj instanceof TradeCustomer) {
        
        TradeCustomer tradeCustomer = (TradeCustomer) obj;
        
        Pair trade = tradeCustomer.getTrade();
        Pair customer = tradeCustomer.getCustomer();
        
        collector.emit(SequenceTopologyDef.TRADE_STREAM_ID, new Values(tupleId, trade));
        
        collector.emit(SequenceTopologyDef.CUSTOMER_STREAM_ID, new Values(tupleId, customer));
    } else if (obj != null) {
        LOG.info("Unknow type " + obj.getClass().getName());
    } else {
        LOG.info("Nullpointer ");
    }
}
 
Example #15
Source File: InOrderTestBolt.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple input, BasicOutputCollector collector) {
    Integer c1 = input.getInteger(0);
    Integer c2 = input.getInteger(1);

    Integer expect = expected.get(c1);
    if (expect == null)
        expect = 0;

    if (c2.intValue() == expect.intValue())
        successCounter.inc();
    else
        failCounter.inc();

    expect = c2 + 1;
    expected.put(c1, expect);
}
 
Example #16
Source File: SequenceTestSplitRecord.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple input, BasicOutputCollector collector) {
    Long tupleId = input.getLong(0);
    Object object = input.getValue(1);

    if (object instanceof TradeCustomer) {

        TradeCustomer tradeCustomer = (TradeCustomer) object;

        Pair trade = tradeCustomer.getTrade();
        Pair customer = tradeCustomer.getCustomer();

        collector.emit(SequenceTopologyDef.TRADE_STREAM_ID, new Values(tupleId, trade));
        collector.emit(SequenceTopologyDef.CUSTOMER_STREAM_ID, new Values(tupleId, customer));
        emitCounter.update(2);
    }
}
 
Example #17
Source File: BatchMetaSpout.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple input, BasicOutputCollector collector) {
	
	String streamId = input.getSourceStreamId();
	if (streamId.equals(BatchMetaRebalance.REBALANCE_STREAM_ID)) {
		try {
			metaClient.rebalanceMqList();
		} catch (Exception e) {
			LOG.warn("Failed to do rebalance operation", e);
		}
	}else {
		BatchId batchId = (BatchId) input.getValue(0);

		emitBatch(batchId, collector);
	}
	
}
 
Example #18
Source File: CoordinatedBolt.java    From jstorm with Apache License 2.0 6 votes vote down vote up
public void prepare(Map conf, TopologyContext context, OutputCollector collector) {

        taskId = String.valueOf(context.getThisTaskId());
        taskName = context.getThisComponentId() + "_" + context.getThisTaskId();

        this.basicCollector = new BasicOutputCollector(collector);
        this.collector = collector;

        if (delegate instanceof ICommitter) {
            isCommiter = true;
            commited = new TimeCacheMap<>(context.maxTopologyMessageTimeout());
            mkCommitDir(conf);
        }

        delegate.prepare(conf, context);

    }
 
Example #19
Source File: SequenceTestPairCount.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple input, BasicOutputCollector collector) {
    Long tupleId = input.getLong(0);
    Pair pair = (Pair) input.getValue(1);
    emitCounter.inc();
    collector.emit(new Values(tupleId, pair));
}
 
Example #20
Source File: SimpleBatchTestSpout.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {

    BatchId batchId = (BatchId) tuple.getValue(0);
    if(batchId.getId() > 100)
    {
        JStormUtils.sleepMs(1000);
        return;
    }

    for (int i = 0; i < BATCH_SIZE; i++) {
        long value = random.nextInt(100);
        basicOutputCollector.emit(new Values(batchId, value));
    }
}
 
Example #21
Source File: InOrderDeliveryTest.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    Integer c1 = tuple.getInteger(0);
    Integer c2 = tuple.getInteger(1);
    Integer exp = expected.get(c1);
    if (exp == null)
        exp = 0;
    if (c2.intValue() != exp.intValue()) {
        System.out.println(c1 + " " + c2 + " != " + exp);
        throw new FailedException(c1 + " " + c2 + " != " + exp);
    }
    exp = c2 + 1;
    expected.put(c1, exp);
}
 
Example #22
Source File: AbstractRankerBolt.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * This method functions as a template method (design pattern).
 */
@Override
public final void execute(Tuple tuple, BasicOutputCollector collector) {
    if (TupleUtils.isTick(tuple)) {
        getLogger().debug("Received tick tuple, triggering emit of current rankings");
        emitRankings(collector);
    } else {
        updateRankingsWithTuple(tuple);
    }
}
 
Example #23
Source File: WordCounterBolt.java    From storm-camel-example with Apache License 2.0 5 votes vote down vote up
@Override
public final void execute(final Tuple tuple, final BasicOutputCollector collector) {
    final String word = tuple.getString(0);
    int count = counts.getOrDefault(word, 0);
    count++;
    counts.put(word, count);

    collector.emit(new Values(word, count));
}
 
Example #24
Source File: LogBolt.java    From galaxy-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector basicOutputCollector) {
    String body = tuple.getString(0);
    cache.add(body);
    if (random.nextInt(100) > 95) {
        LOGGER.info("Message body: " + body);
        LOGGER.info("Message cache size: " + cache.size());
    }

}
 
Example #25
Source File: ElasticSearchJsonBolt.java    From cognition with Apache License 2.0 5 votes vote down vote up
@Override
final public void execute(Tuple input, BasicOutputCollector collector) {
  LogRecord record = (LogRecord) input.getValueByField(AbstractLogRecordBolt.RECORD);

  try {
    indexRecord(record);
  } catch (Exception e) {
    logger.error("Error indexing record", e);
    throw new FailedException("Error indexing record", e);
  }
  collector.emit(new Values(record));
  collector.emit(ES_JSON, new Values(record.getValue(esJsonField)));
}
 
Example #26
Source File: EmailCounter.java    From C2-Github-commit-count with MIT License 5 votes vote down vote up
@Override
public void execute(Tuple tuple,
                    BasicOutputCollector outputCollector) {
  String email = tuple.getStringByField("email");
  counts.put(email, countFor(email) + 1);
  printCounts();
}
 
Example #27
Source File: EmailExtractor.java    From C2-Github-commit-count with MIT License 5 votes vote down vote up
@Override
public void execute(Tuple tuple,
                    BasicOutputCollector outputCollector) {
  String commit = tuple.getStringByField("commit");
  String[] parts = commit.split(" ");
  outputCollector.emit(new Values(parts[1]));
}
 
Example #28
Source File: TotalWordCounter.java    From storm-hbase with Apache License 2.0 5 votes vote down vote up
public void execute(Tuple input, BasicOutputCollector collector) {
    total = total.add(new BigInteger(input.getValues().get(1).toString()));
    collector.emit(tuple(total.toString()));
    //prints the total with low probability.
    if(RANDOM.nextInt(1000) > 995) {
        LOG.info("Running total = " + total);
    }
}
 
Example #29
Source File: WordCountTopology.java    From flink-perf with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
  String word = tuple.getString(0);
  Integer count = counts.get(word);
  if (count == null)
    count = 0;
  count++;
  counts.put(word, count);
  collector.emit(new Values(word, count));
}
 
Example #30
Source File: RollingBoltTest.java    From storm-benchmark with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "getRollingBolt")
public void shouldEmitSomethingIfAtLeastOneObjectWasCountedAndTickTupleIsReceived(RollingBolt bolt) {
  Tuple normalTuple = mockNormalTuple(new Object());
  Tuple tickTuple = MockTupleHelpers.mockTickTuple();
  BasicOutputCollector collector = mock(BasicOutputCollector.class);

  bolt.execute(normalTuple, collector);
  bolt.execute(tickTuple, collector);

  verify(collector).emit(any(Values.class));
}