Java Code Examples for backtype.storm.tuple.Tuple#getValueByField()

The following examples show how to use backtype.storm.tuple.Tuple#getValueByField() . 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: POSTaggerBolt.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<String> preprocessedTokens = (List<String>) tuple
      .getValueByField("preprocessedTokens");

  // POS Tagging
  List<TaggedToken> taggedTokens = tag(preprocessedTokens);

  if (m_logging) {
    LOG.info("Tweet: " + taggedTokens);
  }

  // Emit new tuples
  collector.emit(new Values(text, taggedTokens));
}
 
Example 3
Source File: HealthCheckParseBolt.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Tuple tuple) {
    HealthCheckParseAPIEntity result = null;
    try {
        result = (HealthCheckParseAPIEntity) tuple.getValueByField("f1");
        Map<String, Object> map = new TreeMap<>();
        map.put("status", result.getStatus());
        map.put("timestamp", result.getTimeStamp());
        map.put("role", result.getRole());
        map.put("host", result.getHost());
        map.put("site", result.getSite());

        if (LOG.isDebugEnabled()) {
            LOG.debug("emitted " + map);
        }
        collector.emit(Arrays.asList(result.getHost(), map));
    } catch (Exception ex) {
        LOG.error("Failing parse security log message, and ignore this message", ex);
    } finally {
        collector.ack(tuple);
    }
}
 
Example 4
Source File: CheckpointTupleForwarder.java    From jstorm with Apache License 2.0 5 votes vote down vote up
/**
 * Invokes handleCheckpoint once checkpoint tuple is received on
 * all input checkpoint streams to this component.
 */
private void processCheckpoint(Tuple input) {
    Action action = (Action) input.getValueByField(CheckpointSpout.CHECKPOINT_FIELD_ACTION);
    long txid = input.getLongByField(CheckpointSpout.CHECKPOINT_FIELD_TXID);
    if (shouldProcessTransaction(action, txid)) {
        LOG.debug("Processing action {}, txid {}", action, txid);
        try {
            if (txid >= lastTxid) {
                handleCheckpoint(input, action, txid);
                if (action == CheckPointState.Action.ROLLBACK) {
                    lastTxid = txid - 1;
                } else {
                    lastTxid = txid;
                }
            } else {
                LOG.debug("Ignoring old transaction. Action {}, txid {}", action, txid);
                collector.ack(input);
            }
        } catch (Throwable th) {
            LOG.error("Got error while processing checkpoint tuple", th);
            collector.fail(input);
            collector.reportError(th);
        }
    } else {
        LOG.debug("Waiting for action {}, txid {} from all input tasks. checkPointInputTaskCount {}, " +
                "transactionRequestCount {}", action, txid, checkPointInputTaskCount, transactionRequestCount);
        collector.ack(input);
    }
}
 
Example 5
Source File: KvStatefulBoltExecutor.java    From jstorm with Apache License 2.0 5 votes vote down vote up
private IKvState<K, V> getSpecifiedKeyRangeState(Tuple input, Fields fields) {
    Object key = null;
    if (fields.size() == 1) {
        key = input.getValueByField(fields.get(0));
    } else {
        List<Object> fieldedValuesTobeHash = Lists.newArrayList();
        for (String field : fields) {
            fieldedValuesTobeHash.add(input.getValueByField(field));
        }
        key = fieldedValuesTobeHash;
    }
    return keyRangeState.getRangeStateByKey(key);
}
 
Example 6
Source File: MQBolt.java    From jea with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void execute(Tuple input, OutputCollector collector) {
	// TODO Auto-generated method stub
	Set<String> queues = (Set<String>) input.getValueByField("queue");
	for(String queue : queues){
		new Thread(ConsumeDefinition.findByQueue(queue)).start();
	}
	collector.ack(input);
}
 
Example 7
Source File: FrameSerializer.java    From StormCV with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected Frame createObject(Tuple tuple) throws IOException {
	byte[] buffer = tuple.getBinaryByField(IMAGE);
	Frame frame;
	if(buffer == null){
		frame = new Frame(tuple, tuple.getStringByField(IMAGETYPE), null, tuple.getLongByField(TIMESTAMP), (Rectangle)tuple.getValueByField(BOUNDINGBOX));
	}else{
		frame = new Frame(tuple, tuple.getStringByField(IMAGETYPE), buffer, tuple.getLongByField(TIMESTAMP), (Rectangle)tuple.getValueByField(BOUNDINGBOX));
	}
	frame.getFeatures().addAll((List<Feature>)tuple.getValueByField(FEATURES));
	return frame;
}
 
Example 8
Source File: FeatureSerializer.java    From StormCV with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected Feature createObject(Tuple tuple) throws IOException {
	List<Descriptor> sparseDescriptors = (List<Descriptor>) tuple.getValueByField(SPARSE_DESCR);
	float[][][] denseDescriptors = (float[][][])tuple.getValueByField(DENSE_DESCR);
	Feature feature = new Feature(tuple, tuple.getStringByField(NAME), tuple.getLongByField(DURATION), sparseDescriptors, denseDescriptors);
	return feature;
}
 
Example 9
Source File: FlowInfo.java    From flowmix with Apache License 2.0 5 votes vote down vote up
public FlowInfo(Tuple tuple) {
  flowId = tuple.getStringByField(FLOW_ID);
  event = (Event) tuple.getValueByField(EVENT);
  idx = tuple.getIntegerByField(FLOW_OP_IDX);
  idx++;
  streamName = tuple.getStringByField(STREAM_NAME);
  previousStream = tuple.getStringByField(LAST_STREAM);

  if(tuple.contains(PARTITION))
    partition = tuple.getStringByField(PARTITION);
}
 
Example 10
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 11
Source File: AlertPublisherBolt.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple input) {
    try {
        streamContext.counter().incr("receive_count");
        PublishPartition partition = (PublishPartition) input.getValueByField(AlertConstants.FIELD_0);
        AlertStreamEvent event = (AlertStreamEvent) input.getValueByField(AlertConstants.FIELD_1);
        if (logEventEnabled) {
            LOG.info("Alert publish bolt {}/{} with partition {} received event: {}", this.getBoltId(), this.context.getThisTaskId(), partition, event);
        }
        DedupKey dedupKey = new DedupKey(event.getPolicyId(), event.getStreamId());
        if (deduplicatorMap != null && deduplicatorMap.containsKey(dedupKey)) {
            List<AlertStreamEvent> eventList = deduplicatorMap.get(dedupKey).dedup(event);
            if (eventList == null || eventList.isEmpty()) {
                collector.ack(input);
                return;
            }
            event.setDuplicationChecked(true);
        }

        AlertStreamEvent filteredEvent = alertFilter.filter(event);
        if (filteredEvent != null) {
            alertPublisher.nextEvent(partition, filteredEvent);
        }
        this.collector.ack(input);
        streamContext.counter().incr("ack_count");
    } catch (Throwable ex) {
        streamContext.counter().incr("fail_count");
        LOG.error(ex.getMessage(), ex);
        collector.reportError(ex);
    }
}
 
Example 12
Source File: StateLocatorBolt.java    From StormTweetsSentimentAnalysis with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public final void execute(final Tuple input) {
	final Status status = (Status) input.getValueByField("tweet");
	final Optional<String> stateOptional = getStateFromTweet(status);
	if(stateOptional.isPresent()) {
		final String state = stateOptional.get();
		//Emit the state and also the complete tweet to the next Bolt.
		this._outputCollector.emit(new Values(state, status));
	}
}
 
Example 13
Source File: SentimentCalculatorBolt.java    From StormTweetsSentimentAnalysis with Apache License 2.0 5 votes vote down vote up
@Override
public final void execute(final Tuple input) {
	final String state = (String) input.getValueByField("state");
	final Status status = (Status) input.getValueByField("tweet");
	final int sentimentCurrentTweet = getSentimentOfTweet(status);

	final int sentimentUpdated = stateSentimentMap.getOrDefault(state, 0) + sentimentCurrentTweet;
	stateSentimentMap.put(state, sentimentUpdated);

	if (logIntervalInSeconds <= stopwatch.elapsed(TimeUnit.SECONDS)) {
		logSentimentsOfStates();
		stopwatch.reset();
		stopwatch.start();
	}
}
 
Example 14
Source File: KafkaBolt.java    From storm-kafka-0.8-plus with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple input) {
    K key = null;
    if (input.contains(BOLT_KEY)) {
        key = (K) input.getValueByField(BOLT_KEY);
    }
    V message = (V) input.getValueByField(BOLT_MESSAGE);
    try {
        producer.send(new KeyedMessage<K, V>(topic, key, message));
    } catch (Exception ex) {
        LOG.error("Could not send message with key '" + key + "' and value '" + message + "'", ex);
    } finally {
        collector.ack(input);
    }
}
 
Example 15
Source File: SimpleHBaseMapper.java    From storm-hbase with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] rowKey(Tuple tuple) {
    Object objVal = tuple.getValueByField(this.rowKeyField);
    return toBytes(objVal);
}
 
Example 16
Source File: AbstractProcessingBolt.java    From cognition with Apache License 2.0 4 votes vote down vote up
@Override
final protected void execute(Tuple input, RecordCollector collector) {
  LogRecord record = (LogRecord) input.getValueByField(RECORD);
  process(record);
  collector.emit(record);
}
 
Example 17
Source File: SpoutExecutors.java    From jstorm with Apache License 2.0 4 votes vote down vote up
private Runnable processTupleEvent(Tuple event) {
    Runnable runnable = null;
    Tuple tuple = event;
    if (event.getSourceStreamId().equals(Common.TOPOLOGY_MASTER_CONTROL_STREAM_ID)) {
        TopoMasterCtrlEvent ctrlEvent = (TopoMasterCtrlEvent) tuple.getValueByField("ctrlEvent");
        if (ctrlEvent.isTransactionEvent()) {
            if (spout instanceof ICtrlMsgSpout) {
                runnable = new CtrlMsgSpout((ICtrlMsgSpout) spout, ctrlEvent);
            }
        } else if (ctrlEvent.isFinishInitEvent()) {
            LOG.info("spout task-{} received topology finish init operation message", taskId);
            taskHbTrigger.updateExecutorStatus(TaskStatus.RUN);
            this.checkTopologyFinishInit = true;
        } else {
            LOG.warn("Received unexpected control event, {}", ctrlEvent);
        }
    } else if (event.getSourceStreamId().equals(Common.TOPOLOGY_MASTER_REGISTER_METRICS_RESP_STREAM_ID)) {
        this.metricsReporter.updateMetricMeta((Map<String, Long>) tuple.getValue(0));
    } else {
        Object id = tuple.getValue(0);
        Object obj = pending.remove((Long) id);

        if (obj == null) {
            if (JStormDebugger.isDebug(id)) {
                LOG.info("Pending map no entry:" + id);
            }
            runnable = null;
        } else {
            TupleInfo tupleInfo = (TupleInfo) obj;

            String stream_id = tuple.getSourceStreamId();

            if (stream_id.equals(Acker.ACKER_ACK_STREAM_ID)) {
                runnable = new AckSpoutMsg(id, spout, tuple, tupleInfo, taskStats);
            } else if (stream_id.equals(Acker.ACKER_FAIL_STREAM_ID)) {
                runnable = new FailSpoutMsg(id, spout, tupleInfo, taskStats);
            } else {
                LOG.warn("Receive one unknown source Tuple " + idStr);
                runnable = null;
            }
        }

        taskStats.recv_tuple(tuple.getSourceComponent(), tuple.getSourceStreamId());
    }
    return runnable;
}
 
Example 18
Source File: GroupOfFramesSerializer.java    From StormCV with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected GroupOfFrames createObject(Tuple tuple) throws IOException {
	return new GroupOfFrames(tuple, (List<Frame>)tuple.getValueByField(FRAME_LIST));
}
 
Example 19
Source File: DescriptorSerializer.java    From StormCV with Apache License 2.0 4 votes vote down vote up
@Override
protected Descriptor createObject(Tuple tuple) throws IOException {
	return new Descriptor(tuple, (Rectangle)tuple.getValueByField(BOUNDINGBOX), (Long)tuple.getValueByField(DURATION), (float[])tuple.getValueByField(VALUES));
}
 
Example 20
Source File: MetricsUpdater.java    From jstorm with Apache License 2.0 4 votes vote down vote up
private void updateMetrics(Tuple input) {
    String workerSlot = (String) input.getValueByField(TopologyMaster.FIELD_METRIC_WORKER);
    WorkerUploadMetrics metrics = (WorkerUploadMetrics) input.getValueByField(TopologyMaster.FIELD_METRIC_METRICS);
    topologyMetricContext.addToMemCache(workerSlot, metrics.get_allMetrics());
    metricLogger.info("received metrics from:{}, size:{}", workerSlot, metrics.get_allMetrics().get_metrics_size());
}