}
private static StormTopology basicStateAndDRPC(LocalDRPC drpc, FeederBatchSpout spout) throws IOException {
TridentTopology topology = new TridentTopology();
// persistentAggregate persists the result of aggregation into data stores,
// which you can use from other applications.
// You can also use it in other topologies by using the TridentState object returned.
//
// The state is commonly backed by a data store like memcache, cassandra etc.
// Here we are simply using a hash map
TridentState countState =
topology
.newStream("spout", spout)
.groupBy(new Fields("actor"))
.persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count"));
// There are a few ready-made state libraries that you can use
// Below is an example to use memcached
// List<InetSocketAddress> memcachedServerLocations = ImmutableList.of(new InetSocketAddress("some.memcached.server",12000));
// TridentState countStateMemcached =
// topology
// .newStream("spout", spout)
// .groupBy(new Fields("actor"))
// .persistentAggregate(MemcachedState.transactional(memcachedServerLocations), new Count(), new Fields("count"));
// DRPC stands for Distributed Remote Procedure Call
// You can issue calls using the DRPC client library
// A DRPC call takes two Strings, function name and function arguments
//
// In order to call the DRPC defined below, you'd use "count_per_actor" as the function name
// The function arguments will be available as "args"
/*
topology
.newDRPCStream("ping", drpc)
.each(new Fields("args"), new Split(" "), new Fields("reply"))
.each(new Fields("reply"), new RegexFilter("ping"))
.project(new Fields("reply"));
// You can apply usual processing primitives to DRPC streams as well
topology
.newDRPCStream("count", drpc)
.each(new Fields("args"), new Split(" "), new Fields("split"))
.each(new Fields("split"), new RegexFilter("a.*"))
.groupBy(new Fields("split"))
.aggregate(new Count(), new Fields("count")); */
// More usefully, you can query the state you created earlier
topology
.newDRPCStream("count_per_actor", drpc)
.stateQuery(countState, new Fields("args"), new MapGet(), new Fields("count"));
// Here is a more complex example
topology
.newDRPCStream("count_per_actors", drpc)
.each(new Fields("args"), new Split(" "), new Fields("actor"))
.groupBy(new Fields("actor"))
.stateQuery(countState, new Fields("actor"), new MapGet(), new Fields("individual_count"))
.each(new Fields("individual_count"), new FilterNull())
.aggregate(new Fields("individual_count"), new Sum(), new Fields("count"));
// For how to call DRPC calls, go back to the main method
return topology.build();
}