Java Code Examples for storm.trident.tuple.TridentTuple#getValueByField()

The following examples show how to use storm.trident.tuple.TridentTuple#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: WindowsStateUpdater.java    From jstorm with Apache License 2.0 6 votes vote down vote up
@Override
public void updateState(WindowsState state, List<TridentTuple> tuples, TridentCollector collector) {
    Long currentTxId = state.getCurrentTxId();
    LOG.debug("Removing triggers using WindowStateUpdater, txnId: [{}] ", currentTxId);
    for (TridentTuple tuple : tuples) {
        try {
            Object fieldValue = tuple.getValueByField(WindowTridentProcessor.TRIGGER_FIELD_NAME);
            if (!(fieldValue instanceof WindowTridentProcessor.TriggerInfo)) {
                throw new IllegalClassException(WindowTridentProcessor.TriggerInfo.class, fieldValue.getClass());
            }
            WindowTridentProcessor.TriggerInfo triggerInfo = (WindowTridentProcessor.TriggerInfo) fieldValue;
            String triggerCompletedKey = WindowTridentProcessor.getWindowTriggerInprocessIdPrefix(triggerInfo.windowTaskId) + currentTxId;

            LOG.debug("Removing trigger key [{}] and trigger completed key [{}] from store: [{}]", triggerInfo, triggerCompletedKey, windowsStore);

            windowsStore.removeAll(Lists.newArrayList(triggerInfo.generateTriggerKey(), triggerCompletedKey));
        } catch (Exception ex) {
            LOG.warn(ex.getMessage());
            collector.reportError(ex);
            throw new FailedException(ex);
        }
    }
}
 
Example 2
Source File: Decider.java    From first-stories-twitter with MIT License 6 votes vote down vote up
@Override
public NearNeighbour init(TridentTuple tuple) {
	Tweet tw = (Tweet) tuple.getValueByField("tweet_obj");
	Tweet bucketTweet = (Tweet) tuple.getValueByField("coltweet_obj");
	double cosineBuckets = tuple.getDoubleByField("cosSimBckts");
	Object closestRecentObj = tuple.getValueByField("nnRecentTweet");
	NearNeighbour closestRecent = null;
	if (closestRecentObj != null)
		closestRecent = (NearNeighbour) closestRecentObj;

	if (closestRecent != null) {
		if (cosineBuckets >= closestRecent.getCosine())
			return new NearNeighbour(cosineBuckets, bucketTweet);
		else
			return closestRecent;
	} else {
		return new NearNeighbour(cosineBuckets, bucketTweet);
	}
}
 
Example 3
Source File: Extractor.java    From first-stories-twitter with MIT License 5 votes vote down vote up
@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
	NearNeighbour nn = (NearNeighbour) tuple.getValueByField("nn");
	Long nnId = 0L;
	Double cosine = nn.getCosine();
	Tweet nnTweet = nn.getTweet();
	String nnText="Null text";		
	if (nnTweet!=null){
		nnId = nnTweet.getID();
		nnText = nnTweet.getBody();
		}
	
	collector.emit(new Values(nnId, nnText, cosine));
}
 
Example 4
Source File: FirstN.java    From jstorm with Apache License 2.0 5 votes vote down vote up
@Override
public PriorityQueue init(Object batchId, TridentCollector collector) {
    return new PriorityQueue(_n, new Comparator<TridentTuple>() {
        @Override
        public int compare(TridentTuple t1, TridentTuple t2) {
            Comparable c1 = (Comparable) t1.getValueByField(_sortField);
            Comparable c2 = (Comparable) t2.getValueByField(_sortField);
            int ret = c1.compareTo(c2);
            if (_reverse)
                ret *= -1;
            return ret;
        }
    });
}
 
Example 5
Source File: ComparisonAggregator.java    From jstorm with Apache License 2.0 5 votes vote down vote up
protected T valueFromTuple(TridentTuple tuple) {
    // when there is no input field then the whole tuple is considered for comparison.
    Object value = null;
    if (inputFieldName != null && tuple != null) {
        value =  tuple.getValueByField(inputFieldName);
    } else {
        value = tuple;
    }

    log.debug("value from tuple is [{}] with input field [{}] and tuple [{}]", value, inputFieldName, tuple);

    return (T) value;
}
 
Example 6
Source File: QuerySearchIndexQuery.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public List<Collection<T>> batchRetrieve(ESIndexState<T> indexState, List<TridentTuple> inputs) {
    List<Collection<T>> res = new LinkedList<>( );
    for(TridentTuple input : inputs) {
        String query = (String)input.getValueByField("query");
        List<String> types = (List) input.getValueByField("types");
        List<String> indices = (List) input.getValueByField("indices");
        res.add(indexState.searchQuery(query, indices, types));
    }
    return res;
}
 
Example 7
Source File: ComputeDistance.java    From first-stories-twitter with MIT License 5 votes vote down vote up
@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
	Tweet possibleNeighbour = (Tweet) tuple.getValueByField("coltweet_obj");
	Tweet newTweet = (Tweet) tuple.getValueByField("tweet_obj");
       NearNeighbour possibleClosest = tools.computeCosineSimilarity(possibleNeighbour, newTweet);
       
       collector.emit(new Values(possibleClosest.getCosine()));
}
 
Example 8
Source File: RecentTweetsStateQuery.java    From first-stories-twitter with MIT License 5 votes vote down vote up
@Override
public List<NearNeighbour> batchRetrieve(RecentTweetsDB state,
		List<TridentTuple> args) {
	List<NearNeighbour> tweets = new ArrayList<NearNeighbour>();
	for (TridentTuple tuple : args) {
		Tweet tw = (Tweet) tuple.getValueByField("tweet_obj");
		double cosSimBuckets = tuple.getDoubleByField("cosSimBckts");

		NearNeighbour closestNeighbour = null;
		// check if comparisons are needed and iterate over all recent
		// tweets and find the closest
		if (cosSimBuckets <= threshold)
			closestNeighbour = state.getClosestNeighbour(tw);
		// else a close enough tweet has been found in the buckets

		tweets.add(closestNeighbour);

		//not every tweet should be inserted into the most recent tweets of each state
		//Insert in all partitions in a round-robin fashion
		//we sacrifice fault-tolerance here. If a partition fails and recovers, 
		//two partitions may insert at the same time
		myturn++;
		if (myturn == numPartitions) {
			state.insert(tw);
			myturn = 0;
		}
	}

	return tweets;
}
 
Example 9
Source File: FirstNAggregator.java    From first-stories-twitter with MIT License 5 votes vote down vote up
@Override
public PriorityQueue init(Object batchId, TridentCollector collector) {
    return new PriorityQueue(_n, new Comparator<TridentTuple>() {
        @Override
        public int compare(TridentTuple t1, TridentTuple t2) {
            Comparable c1 = (Comparable) t1.getValueByField(_sortField);
            Comparable c2 = (Comparable) t2.getValueByField(_sortField);
            int ret = c1.compareTo(c2);
            if(_reverse) ret *= -1;
            return ret;
        }                
    });
}
 
Example 10
Source File: IndexMapStateTest.java    From storm-trident-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Tweet reduce(Tweet tweet, TridentTuple objects) {

    Document<String> doc  = (Document) objects.getValueByField("document");
    if( tweet == null)
        tweet = new Tweet(doc.getSource(), 1);
    else {
        tweet.incrementCount();
    }

    return tweet;
}
 
Example 11
Source File: GetContentName.java    From trident-tutorial with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(TridentTuple objects, TridentCollector tridentCollector) {
    Content content = (Content) objects.getValueByField("content");
    tridentCollector.emit(new Values(content.getContentName()));
}
 
Example 12
Source File: OnlyUrls.java    From trident-tutorial with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isKeep(TridentTuple tuple) {
    Content content = (Content) tuple.getValueByField("content");
    return "url".equals(content.getContentType());
}
 
Example 13
Source File: TridentMinMaxOfVehiclesTopology.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(TridentTuple tuple1, TridentTuple tuple2) {
    Vehicle vehicle1 = (Vehicle) tuple1.getValueByField(Vehicle.FIELD_NAME);
    Vehicle vehicle2 = (Vehicle) tuple2.getValueByField(Vehicle.FIELD_NAME);
    return Integer.compare(vehicle1.maxSpeed, vehicle2.maxSpeed);
}
 
Example 14
Source File: TridentMinMaxOfDevicesTopology.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(TridentTuple tuple1, TridentTuple tuple2) {
    Vehicle vehicle1 = (Vehicle) tuple1.getValueByField(Vehicle.FIELD_NAME);
    Vehicle vehicle2 = (Vehicle) tuple2.getValueByField(Vehicle.FIELD_NAME);
    return Integer.compare(vehicle1.maxSpeed, vehicle2.maxSpeed);
}
 
Example 15
Source File: TridentMinMaxOfDevicesTopology.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(TridentTuple tuple1, TridentTuple tuple2) {
    Vehicle vehicle1 = (Vehicle) tuple1.getValueByField(Vehicle.FIELD_NAME);
    Vehicle vehicle2 = (Vehicle) tuple2.getValueByField(Vehicle.FIELD_NAME);
    return Double.compare(vehicle1.efficiency, vehicle2.efficiency);
}
 
Example 16
Source File: TridentMinMaxOfVehiclesTest.java    From jstorm with Apache License 2.0 4 votes vote down vote up
@Override
public int compare(TridentTuple tuple1, TridentTuple tuple2) {
    Vehicle vehicle1 = (Vehicle) tuple1.getValueByField(Vehicle.FIELD_NAME);
    Vehicle vehicle2 = (Vehicle) tuple2.getValueByField(Vehicle.FIELD_NAME);
    return Integer.compare(vehicle1.speed, vehicle2.speed);
}
 
Example 17
Source File: TweetIdExtractor.java    From trident-tutorial with Apache License 2.0 4 votes vote down vote up
@Override
public void execute(TridentTuple objects, TridentCollector tridentCollector) {
    Content content = (Content) objects.getValueByField("content");
    tridentCollector.emit(new Values(content.getTweetId()));
}
 
Example 18
Source File: SimpleTridentHBaseMapper.java    From storm-hbase with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] rowKey(TridentTuple tuple) {
    Object objVal = tuple.getValueByField(this.rowKeyField);
    return toBytes(objVal);
}