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);
}
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();
}
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);
}
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));
}
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) }));
}
}
Aggregations