System.out.println("OK");
}
private static StormTopology externalState(LocalDRPC drpc, FeederBatchSpout spout) {
TridentTopology topology = new TridentTopology();
// You can reference existing data sources as well.
// Here we are mocking up a "database"
StateFactory stateFactory = new StateFactory() {
@Override
public State makeState(Map conf, IMetricsContext metrics, int partitionIndex, int numPartitions) {
MemoryMapState<Integer> name_to_age = new MemoryMapState<Integer>("name_to_age");
// This is a bit hard to read but it's just pre-populating the state
List<List<Object>> keys = getKeys("ted", "mary", "jason", "tom", "chuck");
name_to_age.multiPut(keys, ImmutableList.of(32, 21, 45, 52, 18));
return name_to_age;
}
};
TridentState nameToAge =
topology.newStaticState(stateFactory);
// 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"))
;
return topology.build();
}