use of org.apache.storm.trident.TridentState in project storm by apache.
the class MapStateTest method wordsTest.
public void wordsTest(StateFactory factory) throws Exception {
FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 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"));
spout.setCycle(false);
TridentTopology topology = new TridentTopology();
TridentState wordCounts = topology.newStream("spout1", spout).each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")).persistentAggregate(factory, new Count(), new Fields("state")).parallelismHint(1);
LocalCluster cluster = new LocalCluster();
LocalDRPC client = new LocalDRPC(cluster.getMetricRegistry());
topology.newDRPCStream("words", client).each(new Fields("args"), new Split(), new Fields("word")).groupBy(new Fields("word")).stateQuery(wordCounts, new Fields("word"), new MapGet(), new Fields("state")).each(new Fields("state"), new FilterNull()).aggregate(new Fields("state"), new Sum(), new Fields("sum"));
logger.info("Submitting topology.");
cluster.submitTopology("test", new HashMap(), topology.build());
logger.info("Waiting for something to happen.");
int count;
do {
Thread.sleep(2000);
count = session.execute(QueryBuilder.select().all().from("words_ks", "words_table")).getAvailableWithoutFetching();
logger.info("Found {} records", count);
} while (count < 24);
logger.info("Starting queries.");
// 5
assertEquals("[[5]]", client.execute("words", "cat dog the man"));
// 0
assertEquals("[[0]]", client.execute("words", "cat"));
// 0
assertEquals("[[0]]", client.execute("words", "dog"));
// 4
assertEquals("[[4]]", client.execute("words", "the"));
// 1
assertEquals("[[1]]", client.execute("words", "man"));
cluster.shutdown();
}
use of org.apache.storm.trident.TridentState in project storm by apache.
the class TridentMapExample method buildTopology.
public static StormTopology buildTopology() {
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, new Fields("uppercased")).filter(theFilter).peek(new Consumer() {
@Override
public void accept(TridentTuple input) {
System.out.println(input.getString(0));
}
}).groupBy(new Fields("uppercased")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
topology.newDRPCStream("words").flatMap(split, new Fields("word")).groupBy(new Fields("word")).stateQuery(wordCounts, new Fields("word"), new MapGet(), new Fields("count")).filter(new FilterNull()).aggregate(new Fields("count"), new Sum(), new Fields("sum"));
return topology.build();
}
Aggregations