本文整理汇总了Java中storm.trident.TridentTopology.newStaticState方法的典型用法代码示例。如果您正苦于以下问题:Java TridentTopology.newStaticState方法的具体用法?Java TridentTopology.newStaticState怎么用?Java TridentTopology.newStaticState使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类storm.trident.TridentTopology
的用法示例。
在下文中一共展示了TridentTopology.newStaticState方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
/**
* Topology builder
* @return
*/
public static StormTopology buildTopology() {
TridentTopology topology = new TridentTopology();
TridentState urlsState = topology.newStaticState(new UrlsDBFactory());
//define the stream
topology.newStream("countStream", new Spout())
.each(new Fields(Spout.CLICK), new LongUrlEmitter(), new Fields(LongUrlEmitter.URL))
// .parallelismHint(5)
.partitionBy(new Fields(LongUrlEmitter.URL))
.partitionPersist(new UrlsDBFactory(), new Fields(LongUrlEmitter.URL), new ProbCountBolt(),
new Fields(LongUrlEmitter.URL, ProbCountBolt.CURR_URL_COUNT))
// .parallelismHint(5)
.newValuesStream()
.shuffle()
.aggregate(new Fields(LongUrlEmitter.URL, ProbCountBolt.CURR_URL_COUNT),
new FirstNAggregator(1, ProbCountBolt.CURR_URL_COUNT, true),
new Fields(LongUrlEmitter.URL, ProbCountBolt.CURR_URL_COUNT))
.each(new Fields(LongUrlEmitter.URL, ProbCountBolt.CURR_URL_COUNT), new Debug());
return topology.build();
}
示例2: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(LocalDRPC drpc) {
TridentTopology topology = new TridentTopology();
TridentState urlToTweeters =
topology.newStaticState(
new StaticSingleKeyMapState.Factory(TWEETERS_DB));
TridentState tweetersToFollowers =
topology.newStaticState(
new StaticSingleKeyMapState.Factory(FOLLOWERS_DB));
topology.newDRPCStream("reach", drpc)
.stateQuery(urlToTweeters, new Fields("args"), new MapGet(), new Fields("tweeters"))
.each(new Fields("tweeters"), new ExpandList(), new Fields("tweeter"))
.shuffle()
.stateQuery(tweetersToFollowers, new Fields("tweeter"), new MapGet(), new Fields("followers"))
.each(new Fields("followers"), new ExpandList(), new Fields("follower"))
.groupBy(new Fields("follower"))
.aggregate(new One(), new Fields("one"))
.aggregate(new Fields("one"), new Sum(), new Fields("reach"));
return topology.build();
}
示例3: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
private static StormTopology buildTopology(LocalDRPC drpc) {
TridentTopology topology = new TridentTopology();
TridentState urlToTweeters = topology.newStaticState(new StaticSingleKeyMapState.Factory(TWEETERS_DB));
TridentState tweetersToFollowers = topology.newStaticState(new StaticSingleKeyMapState.Factory(FOLLOWERS_DB));
topology.newDRPCStream("reach", drpc)
.stateQuery(urlToTweeters, new Fields("args"), new MapGet(), new Fields("tweeters"))
.each(new Fields("tweeters"), new ExpandList(), new Fields("tweeter")).shuffle()
.stateQuery(tweetersToFollowers, new Fields("tweeter"), new MapGet(), new Fields("followers"))
.each(new Fields("followers"), new ExpandList(), new Fields("follower"))
.groupBy(new Fields("follower")).aggregate(new One(), new Fields("one"))
.aggregate(new Fields("one"), new Sum(), new Fields("reach"));
return topology.build();
}
示例4: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(LocalDRPC drpc) {
TridentTopology topology = new TridentTopology();
TridentState urlToTweeters = topology.newStaticState(new StaticSingleKeyMapState.Factory(TWEETERS_DB));
TridentState tweetersToFollowers = topology.newStaticState(new StaticSingleKeyMapState.Factory(FOLLOWERS_DB));
topology.newDRPCStream("reach", drpc).stateQuery(urlToTweeters, new Fields("args"), new MapGet(), new Fields(
"tweeters")).each(new Fields("tweeters"), new ExpandList(), new Fields("tweeter")).shuffle().stateQuery(
tweetersToFollowers, new Fields("tweeter"), new MapGet(), new Fields("followers")).each(new Fields("followers"),
new ExpandList(), new Fields("follower")).groupBy(new Fields("follower")).aggregate(new One(), new Fields(
"one")).aggregate(new Fields("one"), new Sum(), new Fields("reach"));
return topology.build();
}
示例5: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(LocalDRPC drpc) {
TridentTopology topology = new TridentTopology();
TridentState urlToTweeters = topology.newStaticState(new StaticSingleKeyMapState.Factory(TWEETERS_DB));
TridentState tweetersToFollowers = topology.newStaticState(new StaticSingleKeyMapState.Factory(FOLLOWERS_DB));
topology.newDRPCStream("reach", drpc)
.stateQuery(urlToTweeters, new Fields("args"), new MapGet(), new Fields("tweeters"))
.each(new Fields("tweeters"), new ExpandList(), new Fields("tweeter")).shuffle()
.stateQuery(tweetersToFollowers, new Fields("tweeter"), new MapGet(), new Fields("followers"))
.each(new Fields("followers"), new ExpandList(), new Fields("follower")).groupBy(new Fields("follower"))
.aggregate(new One(), new Fields("one")).aggregate(new Fields("one"), new Sum(), new Fields("reach"));
return topology.build();
}
示例6: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(Config conf, LocalDRPC drpc) {
TridentTopology topology = new TridentTopology();
//Kafka Spout
BrokerHosts zk = new ZkHosts(conf.get(CrawlerConfig.KAFKA_CONSUMER_HOST_NAME) + ":" +conf.get(CrawlerConfig.KAFKA_CONSUMER_HOST_PORT));
TridentKafkaConfig kafkaConfig = new TridentKafkaConfig(zk, (String) conf.get(CrawlerConfig.KAFKA_TOPIC_DOCUMENT_NAME));
kafkaConfig.scheme = new SchemeAsMultiScheme(new StringScheme());
OpaqueTridentKafkaSpout spout = new OpaqueTridentKafkaSpout(kafkaConfig);
//ElasticSearch Persistent State
Settings esSettings = ImmutableSettings.settingsBuilder()
.put("storm.elasticsearch.cluster.name", conf.get(CrawlerConfig.ELASTICSEARCH_CLUSTER_NAME))
.put("storm.elasticsearch.hosts", conf.get(CrawlerConfig.ELASTICSEARCH_HOST_NAME) + ":" + conf.get(CrawlerConfig.ELASTICSEARCH_HOST_PORT))
.build();
StateFactory esStateFactory = new ESIndexState.Factory<JSONObject>(new ClientFactory.NodeClient(esSettings.getAsMap()), JSONObject.class);
TridentState esStaticState = topology.newStaticState(esStateFactory);
String esIndex = (String)(conf.get(CrawlerConfig.ELASTICSEARCH_INDEX_NAME));
topology.newStream("docstream",spout)
.each( new Fields("str"), new SplitDocStreamArgs(), new Fields("filename", "task", "user", "content"))
.each( new Fields("filename", "task", "user"), new PrintFilter("Kafka"))
.each( new Fields("filename","task","user","content"), new PrepareDocForElasticSearch(), new Fields("index","type","id","source") )
.partitionPersist(esStateFactory, new Fields("index","type","id","source"), new ESIndexUpdater<String>(new ESTridentTupleMapper()), new Fields());
return topology.build();
}
示例7: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
@Override
public StormTopology buildTopology( ) {
ESIndexMapState.Factory<Tweet> state = ESIndexMapState.nonTransactional(getLocalClient(), Tweet.class);
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"),
new Values("to be or not to be the person"));
spout.setCycle(true);
TridentTopology topology = new TridentTopology();
TridentState staticState = topology.newStaticState(new ESIndexState.Factory<>(getLocalClient(), Tweet.class));
topology.newStream("tweets", spout)
.each(new Fields("sentence"), new DocumentBuilder(), new Fields("document"))
.each(new Fields("document"), new ExtractDocumentInfo(), new Fields("id", "index", "type"))
.groupBy(new Fields("index", "type", "id"))
.persistentAggregate(state, new Fields("document"), new TweetBuilder(), new Fields("tweet"))
.parallelismHint(1);
topology.newDRPCStream("search", drpc)
.each(new Fields("args"), new ExtractSearchArgs(), new Fields("query", "indices", "types"))
.groupBy(new Fields("query", "indices", "types"))
.stateQuery(staticState, new Fields("query", "indices", "types"), new QuerySearchIndexQuery(), new Fields("tweet"))
.each(new Fields("tweet"), new FilterNull())
.each(new Fields("tweet"), new CreateJson(), new Fields("json"))
.project(new Fields("json"));
return topology.build();
}
示例8: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(TransactionalTridentKafkaSpout spout)
throws IOException {
TridentTopology topology = new TridentTopology();
/**
* As a first thing, we need a stream of tweets which we can parse and extract
* only the text and its id. As you will notice, we're going to store the stream
* using the {@link ElasticSearchState} implementation using its {@link StateUpdater}.
* Check their implementations for details.
*/
topology
.newStream("tweets", spout)
.each(new Fields("str"), new ParseTweet(), new Fields("text", "content", "user"))
.each(new Fields("text", "content"), new TweetIdExtractor(), new Fields("tweetId"))
.project(new Fields("tweetId", "text"))
.each(new Fields("tweetId", "text"), new Print())
.partitionPersist(new ElasticSearchStateFactory(), new Fields("tweetId", "text"), new ElasticSearchStateUpdater());
/**
* Now we need a DRPC stream to query the state where the tweets are stored.
* To do that, as shown below, we need an implementation of {@link QueryFunction} to
* access our {@link ElasticSearchState}.
*/
TridentState elasticSearchState = topology.newStaticState(new ElasticSearchStateFactory());
topology
.newDRPCStream("search")
.each(new Fields("args"), new Split(" "), new Fields("keywords")) // let's split the arguments
.stateQuery(elasticSearchState, new Fields("keywords"), new TweetQuery(), new Fields("ids")) // and pass them as query parameters
.project(new Fields("ids"));
return topology.build();
}
示例9: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(LocalDRPC drpc) {
TridentTopology topology = new TridentTopology();
TridentState urlToTweeters = topology.newStaticState(new StaticSingleKeyMapState.Factory(TWEETERS_DB));
TridentState tweetersToFollowers = topology.newStaticState(new StaticSingleKeyMapState.Factory(FOLLOWERS_DB));
topology.newDRPCStream("reach", drpc)
.stateQuery(urlToTweeters, new Fields("args"), new MapGet(), new Fields("tweeters"))
.each(new Fields("tweeters"), new ExpandList(), new Fields("tweeter")).shuffle()
.stateQuery(tweetersToFollowers, new Fields("tweeter"), new MapGet(), new Fields("followers"))
.each(new Fields("followers"), new ExpandList(), new Fields("follower")).groupBy(new Fields("follower"))
.aggregate(new One(), new Fields("one")).aggregate(new Fields("one"), new Sum(), new Fields("reach"));
return topology.build();
}
示例10: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(String hbaseRoot){
Fields fields = new Fields("word", "count");
FixedBatchSpout spout = new FixedBatchSpout(fields, 4,
new Values("storm", 1),
new Values("trident", 1),
new Values("needs", 1),
new Values("javadoc", 1)
);
spout.setCycle(true);
TridentHBaseMapper tridentHBaseMapper = new SimpleTridentHBaseMapper()
.withColumnFamily("cf")
.withColumnFields(new Fields("word"))
.withCounterFields(new Fields("count"))
.withRowKeyField("word");
HBaseValueMapper rowToStormValueMapper = new WordCountValueMapper();
HBaseProjectionCriteria projectionCriteria = new HBaseProjectionCriteria();
projectionCriteria.addColumn(new HBaseProjectionCriteria.ColumnMetaData("cf", "count"));
HBaseState.Options options = new HBaseState.Options()
.withConfigKey(hbaseRoot)
.withDurability(Durability.SYNC_WAL)
.withMapper(tridentHBaseMapper)
.withProjectionCriteria(projectionCriteria)
.withRowToStormValueMapper(rowToStormValueMapper)
.withTableName("WordCount");
StateFactory factory = new HBaseStateFactory(options);
TridentTopology topology = new TridentTopology();
Stream stream = topology.newStream("spout1", spout);
stream.partitionPersist(factory, fields, new HBaseUpdater(), new Fields());
TridentState state = topology.newStaticState(factory);
stream = stream.stateQuery(state, new Fields("word"), new HBaseQuery(), new Fields("columnName","columnValue"));
stream.each(new Fields("word","columnValue"), new PrintFunction(), new Fields());
return topology.build();
}
示例11: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(Config conf, LocalDRPC localDrpc) {
TridentTopology topology = new TridentTopology();
//Kafka Spout
BrokerHosts zk = new ZkHosts(conf.get(CrawlerConfig.KAFKA_CONSUMER_HOST_NAME) + ":" +conf.get(CrawlerConfig.KAFKA_CONSUMER_HOST_PORT));
TridentKafkaConfig kafkaConfig = new TridentKafkaConfig(zk, (String) conf.get(CrawlerConfig.KAFKA_TOPIC_NAME));
kafkaConfig.scheme = new SchemeAsMultiScheme(new StringScheme());
OpaqueTridentKafkaSpout spout = new OpaqueTridentKafkaSpout(kafkaConfig);
//ElasticSearch Persistent State
Settings esSettings = ImmutableSettings.settingsBuilder()
.put("storm.elasticsearch.cluster.name", conf.get(CrawlerConfig.ELASTICSEARCH_CLUSTER_NAME))
.put("storm.elasticsearch.hosts", conf.get(CrawlerConfig.ELASTICSEARCH_HOST_NAME) + ":" + conf.get(CrawlerConfig.ELASTICSEARCH_HOST_PORT))
.build();
StateFactory esStateFactory = new ESIndexState.Factory<String>(new ClientFactory.NodeClient(esSettings.getAsMap()), String.class);
TridentState esStaticState = topology.newStaticState(esStateFactory);
//Topology
topology.newStream("crawlKafkaSpout", spout).parallelismHint(5)
//Splits url and depth information on receiving from Kafka
.each(new Fields("str"), new SplitKafkaInput(), new Fields("url", "depth"))
//Bloom Filter. Filters already crawled URLs
.each(new Fields("url"), new URLFilter())
//Download and Parse Webpage
.each(new Fields("url"), new GetAdFreeWebPage(), new Fields("content_html", "title", "href"))//TODO Optimize
//Add Href URls to Kafka queue
.each(new Fields("href", "depth"), new KafkaProducerFilter())//TODO Replace with kafka persistent state.
//Insert to Elasticsearch
.each(new Fields("url", "content_html", "title"), new PrepareForElasticSearch(), new Fields("index", "type", "id", "source"))
.partitionPersist(esStateFactory, new Fields("index", "type", "id", "source"), new ESIndexUpdater<String>(new ESTridentTupleMapper()))
;
//DRPC
topology.newDRPCStream("search", localDrpc)
.each(new Fields("args"), new SplitDRPCArgs(), new Fields("query_input"))
.each(new Fields("query_input"), new BingAutoSuggest(0), new Fields("query_preProcessed"))//TODO return List of expanded query
.each(new Fields("query_preProcessed"), new PrepareSearchQuery(), new Fields("query", "indices", "types"))
.groupBy(new Fields("query", "indices", "types"))
.stateQuery(esStaticState, new Fields("query", "indices", "types"), new QuerySearchIndexQuery(), new Fields("results"))
;
return topology.build();
}
示例12: buildTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
public static StormTopology buildTopology(Config conf, LocalDRPC localDrpc) {
TridentTopology topology = new TridentTopology();
//Kafka Spout
BrokerHosts zk = new ZkHosts(conf.get(CrawlerConfig.KAFKA_CONSUMER_HOST_NAME) + ":" +conf.get(CrawlerConfig.KAFKA_CONSUMER_HOST_PORT));
TridentKafkaConfig kafkaConfig = new TridentKafkaConfig(zk, (String) conf.get(CrawlerConfig.KAFKA_TOPIC_NAME));
kafkaConfig.scheme = new SchemeAsMultiScheme(new StringScheme());
//kafkaConfig.ignoreZkOffsets=true;
OpaqueTridentKafkaSpout spout = new OpaqueTridentKafkaSpout(kafkaConfig);
//ElasticSearch Persistent State
Settings esSettings = ImmutableSettings.settingsBuilder()
.put("storm.elasticsearch.cluster.name", conf.get(CrawlerConfig.ELASTICSEARCH_CLUSTER_NAME))
.put("storm.elasticsearch.hosts", conf.get(CrawlerConfig.ELASTICSEARCH_HOST_NAME) + ":" + conf.get(CrawlerConfig.ELASTICSEARCH_HOST_PORT))
.build();
StateFactory esStateFactory = new ESIndexState.Factory<JSONObject>(new ClientFactory.NodeClient(esSettings.getAsMap()), JSONObject.class);
TridentState esStaticState = topology.newStaticState(esStateFactory);
//Topology
topology.newStream("crawlKafkaSpout", spout).parallelismHint(5)
//Splits words on receiving from Kafka
.each(new Fields("str"), new SplitFunction(), new Fields("url", "depth", "task", "user"))
.each(new Fields("str"), new PrintFilter("Kafka"))
//Bloom Filter, Filters already crawled URLs
.each(new Fields("url", "task"), new URLFilter())
//Download and Parse Webpage
.each(new Fields("url"), new GetAdFreeWebPage(), new Fields("content_html", "title", "href"))
//Sending URLs present in the page into the kafka queue.
.each(new Fields("href", "depth", "task", "user"), new KafkaProducerFilter())
//Insert to Elasticsearch
.each(new Fields("url", "content_html", "title", "task", "user"), new PrepareForElasticSearch(), new Fields("index", "type", "id", "source"))
.partitionPersist(esStateFactory, new Fields("index", "type", "id", "source"), new ESIndexUpdater<String>(new ESTridentTupleMapper()), new Fields())
;
//DRPC
topology.newDRPCStream("search", localDrpc)
.each(new Fields("args"), new SplitDRPCArgs(), new Fields("query_input", "task"))
.each(new Fields("query_input"), new BingAutoSuggest(0), new Fields("query_preProcessed"))
.each(new Fields("query_preProcessed", "task"), new PrepareSearchQuery(), new Fields("query", "indices", "types"))
.groupBy(new Fields("query", "indices", "types"))
.stateQuery(esStaticState, new Fields("query", "indices", "types"), new QuerySearchIndexQuery(), new Fields("results"))
;
return topology.build();
}
示例13: getTopology
import storm.trident.TridentTopology; //导入方法依赖的package包/类
@Override
public StormTopology getTopology(Config config) {
Object sObj = config.get(SERVER);
if (null == sObj) {
throw new IllegalArgumentException("must set a drpc server");
}
server = (String) sObj;
config.put(Config.DRPC_SERVERS, Lists.newArrayList(server));
Object pObj = config.get(PORT);
if (null == pObj) {
throw new IllegalArgumentException("must set a drpc port");
}
port = Utils.getInt(pObj);
config.put(Config.DRPC_PORT, port);
LOG.info("drpc server: " + server + "; drpc port: " + port);
final int spoutNum = BenchmarkUtils.getInt(config, SPOUT_NUM, DEFAULT_SPOUT_NUM);
final int pageNum = BenchmarkUtils.getInt(config, PAGE_NUM, DEFAULT_PAGE_BOLT_NUM);
final int viewNum = BenchmarkUtils.getInt(config, VIEW_NUM, DEFAULT_VIEW_BOLT_NUM);
final int userNum = BenchmarkUtils.getInt(config, USER_NUM, DEFAULT_USER_BOLT_NUM);
final int followerNum = BenchmarkUtils.getInt(config, FOLLOWER_NUM, DEFAULT_FOLLOWER_BOLT_NUM);
spout = new TransactionalTridentKafkaSpout(
KafkaUtils.getTridentKafkaConfig(config, new SchemeAsMultiScheme(new StringScheme())));
TridentTopology trident = new TridentTopology();
TridentState urlToUsers =
trident.newStream("drpc", spout).parallelismHint(spoutNum).shuffle()
.each(new Fields(StringScheme.STRING_SCHEME_KEY), new Extract(Arrays.asList(Item.URL, Item.USER)),
new Fields("url", "user")).parallelismHint(pageNum)
.groupBy(new Fields("url"))
.persistentAggregate(new MemoryMapState.Factory(), new Fields("url", "user"), new Distinct(), new Fields("user_set"))
.parallelismHint(viewNum);
/** debug
* 1. this proves that the aggregated result has successfully persisted
urlToUsers.newValuesStream()
.each(new Fields("url", "user_set"), new Print("(url, user_set)"), new Fields("url2", "user_set2"));
*/
PageViewGenerator generator = new PageViewGenerator();
TridentState userToFollowers = trident.newStaticState(new StaticSingleKeyMapState.Factory(generator.genFollowersDB()));
/** debug
* 2. this proves that MemoryMapState could be read correctly
trident.newStream("urlToUsers", new PageViewSpout(false))
.each(new Fields("page_view"), new Extract(Arrays.asList(Item.URL)), new Fields("url"))
.each(new Fields("url"), new Print("url"), new Fields("url2"))
.groupBy(new Fields("url2"))
.stateQuery(urlToUsers, new Fields("url2"), new MapGet(), new Fields("users"))
.each(new Fields("users"), new Print("users"), new Fields("users2"));
*/
/** debug
* 3. this proves that StaticSingleKeyMapState could be read correctly
trident.newStream("userToFollowers", new PageViewSpout(false))
.each(new Fields("page_view"), new Extract(Arrays.asList(Item.USER)), new Fields("user"))
.each(new Fields("user"), new Print("user"), new Fields("user2"))
.stateQuery(userToFollowers, new Fields("user2"), new MapGet(), new Fields("followers"))
.each(new Fields("followers"), new Print("followers"), new Fields("followers2"));
*/
trident.newDRPCStream(FUNCTION, null)
.each(new Fields("args"), new Print("args"), new Fields("url"))
.groupBy(new Fields("url"))
.stateQuery(urlToUsers, new Fields("url"), new MapGet(), new Fields("users"))
.each(new Fields("users"), new Expand(), new Fields("user")).parallelismHint(userNum)
.groupBy(new Fields("user"))
.stateQuery(userToFollowers, new Fields("user"), new MapGet(), new Fields("followers"))
.each(new Fields("followers"), new Expand(), new Fields("follower")).parallelismHint(followerNum)
.groupBy(new Fields("follower"))
.aggregate(new One(), new Fields("one"))
.aggregate(new Fields("one"), new Sum(), new Fields("reach"));
return trident.build();
}
示例14: externalState
import storm.trident.TridentTopology; //导入方法依赖的package包/类
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();
}