Search in sources :

Example 6 with MapGet

use of org.apache.storm.trident.operation.builtin.MapGet in project storm by apache.

the class WordCountTridentMap method buildTopology.

public static StormTopology buildTopology(String url, String collectionName) {
    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);
    MongoMapper mapper = new SimpleMongoMapper().withFields("word", "count");
    MongoMapState.Options options = new MongoMapState.Options();
    options.url = url;
    options.collectionName = collectionName;
    options.mapper = mapper;
    QueryFilterCreator filterCreator = new SimpleQueryFilterCreator().withField("word");
    options.queryCreator = filterCreator;
    StateFactory factory = MongoMapState.transactional(options);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout);
    TridentState state = stream.groupBy(new Fields("word")).persistentAggregate(factory, new Fields("count"), new Sum(), new Fields("sum"));
    stream.stateQuery(state, new Fields("word"), new MapGet(), new Fields("sum")).each(new Fields("word", "sum"), new PrintFunction(), new Fields());
    return topology.build();
}
Also used : MongoMapState(org.apache.storm.mongodb.trident.state.MongoMapState) TridentState(org.apache.storm.trident.TridentState) Values(org.apache.storm.tuple.Values) Sum(org.apache.storm.trident.operation.builtin.Sum) MapGet(org.apache.storm.trident.operation.builtin.MapGet) SimpleQueryFilterCreator(org.apache.storm.mongodb.common.SimpleQueryFilterCreator) QueryFilterCreator(org.apache.storm.mongodb.common.QueryFilterCreator) SimpleQueryFilterCreator(org.apache.storm.mongodb.common.SimpleQueryFilterCreator) FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) SimpleMongoMapper(org.apache.storm.mongodb.common.mapper.SimpleMongoMapper) MongoMapper(org.apache.storm.mongodb.common.mapper.MongoMapper) StateFactory(org.apache.storm.trident.state.StateFactory) TridentTopology(org.apache.storm.trident.TridentTopology) Stream(org.apache.storm.trident.Stream) SimpleMongoMapper(org.apache.storm.mongodb.common.mapper.SimpleMongoMapper)

Example 7 with MapGet

use of org.apache.storm.trident.operation.builtin.MapGet in project storm by apache.

the class TridentMapExample method buildTopology.

public static StormTopology buildTopology(LocalDRPC drpc) {
    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", drpc).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();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) FilterNull(org.apache.storm.trident.operation.builtin.FilterNull) Fields(org.apache.storm.tuple.Fields) Consumer(org.apache.storm.trident.operation.Consumer) TridentTopology(org.apache.storm.trident.TridentTopology) TridentState(org.apache.storm.trident.TridentState) Values(org.apache.storm.tuple.Values) MapGet(org.apache.storm.trident.operation.builtin.MapGet) Sum(org.apache.storm.trident.operation.builtin.Sum) Count(org.apache.storm.trident.operation.builtin.Count) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 8 with MapGet

use of org.apache.storm.trident.operation.builtin.MapGet in project storm by apache.

the class TridentReach method buildTopology.

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();
}
Also used : Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) TridentState(org.apache.storm.trident.TridentState) MapGet(org.apache.storm.trident.operation.builtin.MapGet) Sum(org.apache.storm.trident.operation.builtin.Sum)

Example 9 with MapGet

use of org.apache.storm.trident.operation.builtin.MapGet 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();
}
Also used : LocalCluster(org.apache.storm.LocalCluster) FilterNull(org.apache.storm.trident.operation.builtin.FilterNull) TridentState(org.apache.storm.trident.TridentState) HashMap(java.util.HashMap) Values(org.apache.storm.tuple.Values) MapGet(org.apache.storm.trident.operation.builtin.MapGet) Sum(org.apache.storm.trident.operation.builtin.Sum) Count(org.apache.storm.trident.operation.builtin.Count) FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) LocalDRPC(org.apache.storm.LocalDRPC) Split(org.apache.storm.trident.testing.Split)

Example 10 with MapGet

use of org.apache.storm.trident.operation.builtin.MapGet 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();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) FilterNull(org.apache.storm.trident.operation.builtin.FilterNull) Fields(org.apache.storm.tuple.Fields) Consumer(org.apache.storm.trident.operation.Consumer) TridentTopology(org.apache.storm.trident.TridentTopology) TridentState(org.apache.storm.trident.TridentState) Values(org.apache.storm.tuple.Values) MapGet(org.apache.storm.trident.operation.builtin.MapGet) Sum(org.apache.storm.trident.operation.builtin.Sum) Count(org.apache.storm.trident.operation.builtin.Count) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Aggregations

TridentState (org.apache.storm.trident.TridentState)10 TridentTopology (org.apache.storm.trident.TridentTopology)10 MapGet (org.apache.storm.trident.operation.builtin.MapGet)10 Fields (org.apache.storm.tuple.Fields)10 Sum (org.apache.storm.trident.operation.builtin.Sum)8 FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)8 Values (org.apache.storm.tuple.Values)8 Count (org.apache.storm.trident.operation.builtin.Count)5 FilterNull (org.apache.storm.trident.operation.builtin.FilterNull)5 Stream (org.apache.storm.trident.Stream)3 StateFactory (org.apache.storm.trident.state.StateFactory)3 RedisDataTypeDescription (org.apache.storm.redis.common.mapper.RedisDataTypeDescription)2 Consumer (org.apache.storm.trident.operation.Consumer)2 TridentTuple (org.apache.storm.trident.tuple.TridentTuple)2 InetSocketAddress (java.net.InetSocketAddress)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LocalCluster (org.apache.storm.LocalCluster)1 LocalDRPC (org.apache.storm.LocalDRPC)1 QueryFilterCreator (org.apache.storm.mongodb.common.QueryFilterCreator)1