Java Code Examples for storm.trident.TridentTopology#newStaticState()
The following examples show how to use
storm.trident.TridentTopology#newStaticState() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexMapStateTest.java From storm-trident-elasticsearch with Apache License 2.0 | 5 votes |
@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(); }
Example 2
Source File: TridentReach.java From flink-perf with Apache License 2.0 | 5 votes |
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(); }
Example 3
Source File: RealTimeTextSearch.java From trident-tutorial with Apache License 2.0 | 5 votes |
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(); }
Example 4
Source File: TridentReach.java From jstorm with Apache License 2.0 | 5 votes |
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(); }
Example 5
Source File: DRPC.java From storm-benchmark with Apache License 2.0 | 4 votes |
@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(); }
Example 6
Source File: WordCountTrident.java From storm-hbase with Apache License 2.0 | 4 votes |
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(); }
Example 7
Source File: Part05_AdvancedStateAndDRPC.java From trident-tutorial with Apache License 2.0 | 4 votes |
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(); }