Search in sources :

Example 21 with TridentTuple

use of storm.trident.tuple.TridentTuple in project jstorm by alibaba.

the class GroupedAggregator method aggregate.

@Override
public void aggregate(Object[] arr, TridentTuple tuple, TridentCollector collector) {
    GroupCollector groupColl = (GroupCollector) arr[0];
    Map<List, Object> val = (Map) arr[1];
    TridentTuple group = _groupFactory.create((TridentTupleView) tuple);
    TridentTuple input = _inputFactory.create((TridentTupleView) tuple);
    Object curr;
    if (!val.containsKey(group)) {
        curr = _agg.init(arr[2], groupColl);
        val.put((List) group, curr);
    } else {
        curr = val.get(group);
    }
    groupColl.currGroup = group;
    _agg.aggregate(curr, input, groupColl);
}
Also used : List(java.util.List) ComboList(storm.trident.tuple.ComboList) Map(java.util.Map) HashMap(java.util.HashMap) TridentTuple(storm.trident.tuple.TridentTuple)

Example 22 with TridentTuple

use of storm.trident.tuple.TridentTuple in project jstorm by alibaba.

the class TridentMapExample method buildTopology.

public static StormTopology buildTopology(LocalDRPC drpc) {
    FixedBatchSpout spout = new FixedBatchSpout(new Fields("word"), 3, new Values("the cow jumped over the moon"), new Values("the man went to the store and bought some candy"), new Values("four score and seven years ago"), new Values("how many apples can you eat"), new Values("to be or not to be the person"));
    spout.setCycle(true);
    TridentTopology topology = new TridentTopology();
    TridentState wordCounts = topology.newStream("spout1", spout).parallelismHint(16).flatMap(split).map(toUpper).filter(theFilter).peek(new Consumer() {

        @Override
        public void accept(TridentTuple input) {
            System.out.println(input.getString(0));
        }
    }).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    topology.newDRPCStream("words", drpc).flatMap(split).groupBy(new Fields("args")).stateQuery(wordCounts, new Fields("args"), new MapGet(), new Fields("count")).filter(new FilterNull()).aggregate(new Fields("count"), new Sum(), new Fields("sum"));
    return topology.build();
}
Also used : FixedBatchSpout(storm.trident.testing.FixedBatchSpout) FilterNull(storm.trident.operation.builtin.FilterNull) Fields(backtype.storm.tuple.Fields) Consumer(storm.trident.operation.Consumer) TridentTopology(storm.trident.TridentTopology) TridentState(storm.trident.TridentState) Values(backtype.storm.tuple.Values) MapGet(storm.trident.operation.builtin.MapGet) Sum(storm.trident.operation.builtin.Sum) Count(storm.trident.operation.builtin.Count) TridentTuple(storm.trident.tuple.TridentTuple)

Example 23 with TridentTuple

use of storm.trident.tuple.TridentTuple in project jstorm by alibaba.

the class WindowTridentProcessor method finishBatch.

@Override
public void finishBatch(ProcessorContext processorContext) {
    Object batchId = processorContext.batchId;
    Object batchTxnId = getBatchTxnId(batchId);
    LOG.debug("Received finishBatch of : [{}] ", batchId);
    // get all the tuples in a batch and add it to trident-window-manager
    List<TridentTuple> tuples = (List<TridentTuple>) processorContext.state[tridentContext.getStateIndex()];
    tridentWindowManager.addTuplesBatch(batchId, tuples);
    List<Integer> pendingTriggerIds = null;
    List<String> triggerKeys = new ArrayList<>();
    Iterable<Object> triggerValues = null;
    if (retriedAttempt(batchId)) {
        pendingTriggerIds = (List<Integer>) windowStore.get(inprocessTriggerKey(batchTxnId));
        for (Integer pendingTriggerId : pendingTriggerIds) {
            triggerKeys.add(triggerKey(pendingTriggerId));
        }
        triggerValues = windowStore.get(triggerKeys);
    }
    // if there are no trigger values in earlier attempts or this is a new batch, emit pending triggers.
    if (triggerValues == null) {
        pendingTriggerIds = new ArrayList<>();
        Queue<StoreBasedTridentWindowManager.TriggerResult> pendingTriggers = tridentWindowManager.getPendingTriggers();
        LOG.debug("pending triggers at batch: [{}] and triggers.size: [{}] ", batchId, pendingTriggers.size());
        try {
            Iterator<StoreBasedTridentWindowManager.TriggerResult> pendingTriggersIter = pendingTriggers.iterator();
            List<Object> values = new ArrayList<>();
            StoreBasedTridentWindowManager.TriggerResult triggerResult = null;
            while (pendingTriggersIter.hasNext()) {
                triggerResult = pendingTriggersIter.next();
                for (List<Object> aggregatedResult : triggerResult.result) {
                    String triggerKey = triggerKey(triggerResult.id);
                    triggerKeys.add(triggerKey);
                    values.add(aggregatedResult);
                    pendingTriggerIds.add(triggerResult.id);
                }
                pendingTriggersIter.remove();
            }
            triggerValues = values;
        } finally {
            // store inprocess triggers of a batch in store for batch retries for any failures
            if (!pendingTriggerIds.isEmpty()) {
                windowStore.put(inprocessTriggerKey(batchTxnId), pendingTriggerIds);
            }
        }
    }
    collector.setContext(processorContext);
    int i = 0;
    for (Object resultValue : triggerValues) {
        collector.emit(new ConsList(new TriggerInfo(windowTaskId, pendingTriggerIds.get(i++)), (List<Object>) resultValue));
    }
    collector.setContext(null);
}
Also used : ArrayList(java.util.ArrayList) ConsList(storm.trident.tuple.ConsList) ArrayList(java.util.ArrayList) List(java.util.List) TridentTuple(storm.trident.tuple.TridentTuple) ConsList(storm.trident.tuple.ConsList)

Example 24 with TridentTuple

use of storm.trident.tuple.TridentTuple in project jstorm by alibaba.

the class AbstractTridentWindowManager method execAggregatorAndStoreResult.

private void execAggregatorAndStoreResult(int currentTriggerId, List<T> tupleEvents) {
    List<TridentTuple> resultTuples = getTridentTuples(tupleEvents);
    // run aggregator to compute the result
    AccumulatedTuplesCollector collector = new AccumulatedTuplesCollector(delegateCollector);
    Object state = aggregator.init(currentTriggerId, collector);
    for (TridentTuple resultTuple : resultTuples) {
        aggregator.aggregate(state, resultTuple, collector);
    }
    aggregator.complete(state, collector);
    List<List<Object>> resultantAggregatedValue = collector.values;
    ArrayList<WindowsStore.Entry> entries = Lists.newArrayList(new WindowsStore.Entry(windowTriggerCountId, currentTriggerId + 1), new WindowsStore.Entry(WindowTridentProcessor.generateWindowTriggerKey(windowTaskId, currentTriggerId), resultantAggregatedValue));
    windowStore.putAll(entries);
    pendingTriggers.add(new TriggerResult(currentTriggerId, resultantAggregatedValue));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) TridentTuple(storm.trident.tuple.TridentTuple)

Example 25 with TridentTuple

use of storm.trident.tuple.TridentTuple in project jstorm by alibaba.

the class MapReducerAggStateUpdater method updateState.

@Override
public void updateState(MapState map, List<TridentTuple> tuples, TridentCollector collector) {
    Map<List<Object>, List<TridentTuple>> grouped = new HashMap<>();
    for (TridentTuple t : tuples) {
        List<Object> group = _groupFactory.create(t);
        List<TridentTuple> groupTuples = grouped.get(group);
        if (groupTuples == null) {
            groupTuples = new ArrayList<>();
            grouped.put(group, groupTuples);
        }
        groupTuples.add(_inputFactory.create(t));
    }
    List<List<Object>> uniqueGroups = new ArrayList<>(grouped.keySet());
    List<ValueUpdater> updaters = new ArrayList<>(uniqueGroups.size());
    for (List<Object> group : uniqueGroups) {
        updaters.add(new ReducerValueUpdater(_agg, grouped.get(group)));
    }
    List<Object> results = map.multiUpdate(uniqueGroups, updaters);
    for (int i = 0; i < uniqueGroups.size(); i++) {
        List<Object> group = uniqueGroups.get(i);
        Object result = results.get(i);
        collector.emit(_factory.create(new List[] { group, new Values(result) }));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Values(backtype.storm.tuple.Values) ReducerValueUpdater(storm.trident.state.ReducerValueUpdater) ValueUpdater(storm.trident.state.ValueUpdater) ReducerValueUpdater(storm.trident.state.ReducerValueUpdater) ArrayList(java.util.ArrayList) List(java.util.List) ComboList(storm.trident.tuple.ComboList) TridentTuple(storm.trident.tuple.TridentTuple)

Aggregations

TridentTuple (storm.trident.tuple.TridentTuple)25 ArrayList (java.util.ArrayList)10 List (java.util.List)8 Values (backtype.storm.tuple.Values)6 HashMap (java.util.HashMap)6 ComboList (storm.trident.tuple.ComboList)6 Map (java.util.Map)4 ValueUpdater (storm.trident.state.ValueUpdater)4 FailedException (backtype.storm.topology.FailedException)2 Fields (backtype.storm.tuple.Fields)2 TridentTopology (storm.trident.TridentTopology)2 Consumer (storm.trident.operation.Consumer)2 CombinerValueUpdater (storm.trident.state.CombinerValueUpdater)2 ReducerValueUpdater (storm.trident.state.ReducerValueUpdater)2 FixedBatchSpout (storm.trident.testing.FixedBatchSpout)2 ProjectionFactory (storm.trident.tuple.TridentTupleView.ProjectionFactory)2 HashSet (java.util.HashSet)1 IllegalClassException (org.apache.commons.lang.IllegalClassException)1 Put (org.apache.hadoop.hbase.client.Put)1 ElasticSearchException (org.elasticsearch.ElasticSearchException)1