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

The following examples show how to use backtype.storm.tuple.Tuple#getValue() . 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: FusionBoltAction.java    From storm-solr with Apache License 2.0 6 votes vote down vote up
public SpringBolt.ExecuteResult execute(Tuple input, OutputCollector collector) {
  String docId = input.getString(0);
  Map<String,Object> values = (Map<String,Object>)input.getValue(1);

  Map<String,Object> json = new HashMap<String,Object>(10);
  json.put("id", docId);
  List fieldList = new ArrayList();
  for (String field : values.keySet())
    fieldList.add(buildField(field, values.get(field)));
  json.put("fields", fieldList);

  try {
    fusionPipelineClient.postBatchToPipeline(Collections.singletonList(json));
  } catch (Exception e) {
    log.error("Failed to send doc "+docId+" to Fusion due to: "+e);
    throw new RuntimeException(e);
  }

  return SpringBolt.ExecuteResult.ACK;
}
 
Example 2
Source File: IPZoneDataEnrichBolt.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Override
public void executeWithEnrich(Tuple input, Map<String, IPZoneEntity> map) {
    try {
        Map<String, Object> toBeCopied = (Map<String, Object>) input.getValue(1);
        Map<String, Object> event = new TreeMap<String, Object>(toBeCopied); // shallow copy
        IPZoneEntity e = null;
        if (map != null) {
            e = map.get(event.get("host"));
        }
        event.put("securityZone", e == null ? "NA" : e.getSecurityZone());
        if (LOG.isDebugEnabled()) {
            LOG.debug("After IP zone lookup: " + event);
        }
        collector.emit(Arrays.asList(event.get("user"), event));
    } catch (Exception ex) {
        LOG.error("error joining data, ignore it", ex);
    } finally {
        collector.ack(input);
    }
}
 
Example 3
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 4
Source File: SlidingWindowTestAvgBolt.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(TupleWindow inputWindow) {
    int sum = 0;
    List<Tuple> tuplesInWindow = inputWindow.get();
    if (tuplesInWindow.size() > 0) {
        for (Tuple tuple : tuplesInWindow) {
            sum += (int) tuple.getValue(0);
        }
        asmCounter.update(sum / tuplesInWindow.size());
    }
}
 
Example 5
Source File: WindowTestTotalRankingsBolt.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
void updateRankingsWithTuple(Tuple tuple) {
    Rankings rankingsToBeMerged = (Rankings) tuple.getValue(0);
    super.getRankings().updateWith(rankingsToBeMerged);
    super.getRankings().pruneZeroCounts();

    LOG.info("TotalRankingsBolt updateRankingsWithTuple " + getRankings());
}
 
Example 6
Source File: SimpleSpout.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple input, BasicOutputCollector collector) {
    BatchId batchId = (BatchId) input.getValue(0);
    
    for (int i = 0; i < batchSize; i++) {
        long value = rand.nextInt(10);
        collector.emit(new Values(batchId, value));
    }
}
 
Example 7
Source File: HdfsSensitivityDataEnrichBolt.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void executeWithEnrich(Tuple input, Map<String, HdfsSensitivityEntity> map) {
    try {
        Map<String, Object> toBeCopied = (Map<String, Object>) input.getValue(0);
        Map<String, Object> event = new TreeMap<String, Object>(toBeCopied);
        HdfsSensitivityEntity e = null;
        if (LOG.isDebugEnabled()) {
            LOG.debug("Receive map: " + map + "event: " + event);
        }

        String src = (String) event.get("src");
        if (map != null && src != null) {
            String simplifiedPath = new SimplifyPath().build(src);
            for (String fileDir : map.keySet()) {
                Pattern pattern = Pattern.compile(simplifiedPath, Pattern.CASE_INSENSITIVE);
                Matcher matcher = pattern.matcher(fileDir);
                boolean isMatched = matcher.matches();
                if (isMatched) {
                    e = map.get(fileDir);
                    break;
                }
            }
        }
        event.put("sensitivityType", e == null ? "NA" : e.getSensitivityType());
        if (LOG.isDebugEnabled()) {
            LOG.debug("After file sensitivity lookup: " + event);
        }
        // LOG.info(">>>> After file sensitivity lookup: " + event);
        collector.emit(Arrays.asList(event.get("user"), event));
    } catch (Exception ex) {
        LOG.error("error joining data, ignore it", ex);
    } finally {
        collector.ack(input);
    }
}
 
Example 8
Source File: TMUdfBolt.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple input) {
    Object value = input.getValue(0);
    if (input.getSourceStreamId().equals(TopologyMaster.USER_DEFINED_STREAM)) {
        TMUdfMessage message = (TMUdfMessage) value;
        LOG.info("Received TM UDF message trigged by task-{}", message.spoutTaskId);
    } else {
        LOG.info("Received unkown message: {}", input);
    }
}
 
Example 9
Source File: CoordinatedBolt.java    From jstorm with Apache License 2.0 5 votes vote down vote up
public void execute(Tuple tuple) {
    Object id = tuple.getValue(0);
    TrackingInfo track;
    TupleType type = getTupleType(tuple);
    synchronized (_tracked) {
        track = _tracked.get(id);
        if (track == null) {
            track = new TrackingInfo();
            if (_idStreamSpec == null)
                track.receivedId = true;
            _tracked.put(id, track);
        }
    }

    if (type == TupleType.ID) {
        synchronized (_tracked) {
            track.receivedId = true;
        }
        checkFinishId(tuple, type);
    } else if (type == TupleType.COORD) {
        int count = (Integer) tuple.getValue(1);
        synchronized (_tracked) {
            track.reportCount++;
            track.expectedTupleCount += count;
        }
        checkFinishId(tuple, type);
    } else {
        synchronized (_tracked) {
            _delegate.execute(tuple);
        }
    }
}
 
Example 10
Source File: HBaseResourceSensitivityDataJoinBolt.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void executeWithEnrich(Tuple input, Map<String, HBaseSensitivityEntity> map) {
    try {
        Map<String, Object> event = (Map<String, Object>) input.getValue(0);
        LOG.info(">>>> event: " + event + " >>>> map: " + map);

        String resource = (String) event.get("scope");

        HBaseSensitivityEntity sensitivityEntity = null;

        if (map != null && resource != "") {
            for (String key : map.keySet()) {
                Pattern pattern = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
                if (pattern.matcher(resource).find()) {
                    sensitivityEntity = map.get(key);
                    break;
                }
            }
        }
        Map<String, Object> newEvent = new TreeMap<String, Object>(event);
        newEvent.put("sensitivityType", sensitivityEntity == null
            ? "NA" : sensitivityEntity.getSensitivityType());
        newEvent.put("scope", resource);
        if (LOG.isDebugEnabled()) {
            LOG.debug("After hbase resource sensitivity lookup: " + newEvent);
        }
        LOG.info("After hbase resource sensitivity lookup: " + newEvent);
        // push to Kafka sink
        collector.emit(Arrays.asList(newEvent.get("user"), newEvent));
    } catch (Exception ex) {
        LOG.error("error joining data, ignore it", ex);
    } finally {
        collector.ack(input);
    }
}
 
Example 11
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 12
Source File: TransactionFastWordCountWindowedState.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple tuple, IKvState<String, Integer> state, TimeWindow window) {
    String word = (String) tuple.getValue(0);
    Integer count = state.get(word);
    if (count == null)
        count = 0;
    state.put(word, ++count);
}
 
Example 13
Source File: MRRunningAppMetricBolt.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Tuple input) {
    List<AppInfo> runningApps = (List<AppInfo>) input.getValue(0);
    if (runningApps == null || runningApps.isEmpty()) {
        LOG.warn("App list is empty");
    }
    try {
        Map<String, GenericMetricEntity> appMetrics = parseRunningAppMetrics(runningApps);
        List<YarnAppAPIEntity> acceptedApps = parseAcceptedApp();
        flush(appMetrics, acceptedApps);
    } catch (Exception e) {
        LOG.error("Fetal error is caught {}", e.getMessage(), e);
    }
}
 
Example 14
Source File: CoordinatedBolt.java    From jstorm with Apache License 2.0 5 votes vote down vote up
public void fail(Tuple tuple) {
    Object id = tuple.getValue(0);
    synchronized (_tracked) {
        TrackingInfo track = _tracked.get(id);
        if (track != null)
            track.failed = true;
    }
    checkFinishId(tuple, TupleType.REGULAR);
    _delegate.fail(tuple);
}
 
Example 15
Source File: StormProcessingItem.java    From samoa with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(Tuple input) {
	Object sentObject = input.getValue(0);
	ContentEvent sentEvent = (ContentEvent)sentObject;
	processor.process(sentEvent);
}
 
Example 16
Source File: TaskHeartbeatUpdater.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
public void process(Object event) throws Exception {
    synchronized (_lock) {
        if (event instanceof UpdateConfigEvent) {
            //TODO assure update topology, but this tmHandler can't receive this message.
            update(((UpdateConfigEvent) event).getConf());
            return;
        }

        Tuple input = (Tuple) event;
        int sourceTask = input.getSourceTask();
        int uptime = (Integer) input.getValue(0);
        TaskStatus executorStatus = new TaskStatus();
        if (input.getValues().size() < 2) {
            // for compatibility
            executorStatus.setStatus(TaskStatus.RUN);
        } else {
            executorStatus.setStatus((byte) input.getValue(1));
        }
        boolean isSendSpoutTaskFinishStream = false;

        if (spoutsExecutorStatusMap.containsKey(sourceTask)) {
            spoutsExecutorStatusMap.put(sourceTask, executorStatus);
        } else if (boltsExecutorStatusMap.containsKey(sourceTask)) {
            boltsExecutorStatusMap.put(sourceTask, executorStatus);
        } else if (sourceTask != taskId) {
            LOG.warn("received invalid task heartbeat {}", input);
        }
        if (executorStatus.getStatus() == TaskStatus.INIT && spoutsExecutorStatusMap.get(sourceTask) != null) {
            boolean existInitStatusBolt = false;
            for (TaskStatus status : boltsExecutorStatusMap.values()) {
                if (status.getStatus() == TaskStatus.INIT || status.getStatus() == TaskStatus.SHUTDOWN) {
                    existInitStatusBolt = true;
                    break;
                }
            }
            if (!existInitStatusBolt)
                isSendSpoutTaskFinishStream = true;
        }

        if (client == null) {
            client = new NimbusClientWrapper();
            client.init(stormConf);
        }

        // Update the heartbeat for source task, but don't update it if task is initial status
        if (executorStatus.getStatus() != TaskStatus.INIT && uptime > 0) {
            TaskHeartbeat taskHb = taskHbMap.get().get(sourceTask);
            if (taskHb == null) {
                taskHb = new TaskHeartbeat(TimeUtils.current_time_secs(), uptime);
                TaskHeartbeat tmpTaskHb = taskHbMap.get().putIfAbsent(sourceTask, taskHb);
                if (tmpTaskHb != null) {
                    taskHb = tmpTaskHb;
                }
            }
            taskHb.set_time(TimeUtils.current_time_secs());
            taskHb.set_uptime(uptime);
        } else if (isSendSpoutTaskFinishStream) {
            TopoMasterCtrlEvent finishInitEvent = new TopoMasterCtrlEvent(TopoMasterCtrlEvent.EventType.topologyFinishInit);
            ((BoltCollector) (collector.getDelegate())).emitDirectCtrl(
                    sourceTask, Common.TOPOLOGY_MASTER_CONTROL_STREAM_ID, null, new Values(finishInitEvent));
            LOG.info("all bolts' task finish init operation, so tm will notify the spout task-{}", sourceTask);
        }

        // Send heartbeat info of all tasks to nimbus
        if (sourceTask == taskId) {
            uploadHB();
        }
    }
}
 
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: MergeRecord.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(Tuple input) {
    
    tpsCounter.count();
    
    Long tupleId = input.getLong(0);
    Pair pair = (Pair) input.getValue(1);
    
    Pair trade = null;
    Pair customer = null;
    
    Tuple tradeTuple = null;
    Tuple customerTuple = null;
    
    if (input.getSourceComponent().equals(SequenceTopologyDef.CUSTOMER_BOLT_NAME)) {
        customer = pair;
        customerTuple = input;
        
        tradeTuple = tradeMap.remove(tupleId);
        if (tradeTuple == null) {
            customerMap.put(tupleId, input);
            return;
        }
        
        trade = (Pair) tradeTuple.getValue(1);
        
    } else if (input.getSourceComponent().equals(SequenceTopologyDef.TRADE_BOLT_NAME)) {
        trade = pair;
        tradeTuple = input;
        
        customerTuple = customerMap.remove(tupleId);
        if (customerTuple == null) {
            tradeMap.put(tupleId, input);
            return;
        }
        
        customer = (Pair) customerTuple.getValue(1);
    } else {
        LOG.info("Unknow source component: " + input.getSourceComponent());
        collector.fail(input);
        return;
    }
    
    tradeSum.addAndGet(trade.getValue());
    customerSum.addAndGet(customer.getValue());
    
    collector.ack(tradeTuple);
    collector.ack(customerTuple);
    
    TradeCustomer tradeCustomer = new TradeCustomer();
    tradeCustomer.setTrade(trade);
    tradeCustomer.setCustomer(customer);
    collector.emit(new Values(tupleId, tradeCustomer));
}
 
Example 19
Source File: AvroGenericRecordBolt.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
protected void writeTuple(Tuple tuple) throws IOException {
    GenericRecord avroRecord = (GenericRecord) tuple.getValue(0);
    avroWriter.append(avroRecord);
    offset = this.out.getPos();
}
 
Example 20
Source File: SequenceTestMergeRecord.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(Tuple input) {
    Long tupleId = input.getLong(0);
    Pair pair = (Pair) input.getValue(1);

    Pair trade = null;
    Pair customer = null;

    Tuple tradeTuple = null;
    Tuple customerTuple = null;

    if (input.getSourceComponent().equals(SequenceTopologyDef.CUSTOMER_BOLT_NAME)) {
        customer = pair;
        customerTuple = input;

        tradeTuple = tradeMap.remove(tupleId);
        if (tradeTuple == null) {
            customerMap.put(tupleId, input);
            return;
        }

        trade = (Pair) tradeTuple.getValue(1);

    } else if (input.getSourceComponent().equals(SequenceTopologyDef.TRADE_BOLT_NAME)) {
        trade = pair;
        tradeTuple = input;

        customerTuple = customerMap.remove(tupleId);
        if (customerTuple == null) {
            tradeMap.put(tupleId, input);
            return;
        }

        customer = (Pair) customerTuple.getValue(1);
    } else {
        collector.fail(input);
        return;
    }

    tradeSum.addAndGet(trade.getValue());
    customerSum.addAndGet(customer.getValue());

    collector.ack(tradeTuple);
    collector.ack(customerTuple);

    TradeCustomer tradeCustomer = new TradeCustomer();
    tradeCustomer.setTrade(trade);
    tradeCustomer.setCustomer(customer);
    collector.emit(new Values(tupleId, tradeCustomer));
    emitCounter.inc();
}