// Let's setup another state that keeps track of actor's appearance counts per location
TridentState countState =
topology
.newStream("spout", spout)
.groupBy(new Fields("actor","location"))
.persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count"));
// Now, let's calculate the average age of actors seen
topology
.newDRPCStream("age_stats", drpc)
.stateQuery(countState, new TupleCollectionGet(), new Fields("actor", "location"))
.stateQuery(nameToAge, new Fields("actor"), new MapGet(), new Fields("age"))
.each(new Fields("actor","location","age"), new Print())
.groupBy(new Fields("location"))
.chainedAgg()
.aggregate(new Count(), new Fields("count"))
.aggregate(new Fields("age"), new Sum(), new Fields("sum"))
.chainEnd()
.each(new Fields("sum", "count"), new DivideAsDouble(), new Fields("avg"))
.project(new Fields("location", "count", "avg"))
;