Search in sources :

Example 1 with TridentState

use of storm.trident.TridentState in project jstorm by alibaba.

the class TridentWordCountTest method testTridentWordCount.

// to make sure the validator is right
@Test
public void testTridentWordCount() {
    LocalDRPC localDRPC = new LocalDRPC();
    FixedLimitBatchSpout spout = new FixedLimitBatchSpout(SPOUT_LIMIT, new Fields("sentence"), SPOUT_BATCH_SIZE, 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"));
    TridentTopology tridentTopology = new TridentTopology();
    TridentState wordCount = tridentTopology.newStream("spout", spout).parallelismHint(1).each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    tridentTopology.newDRPCStream("words", localDRPC).each(new Fields("args"), new Split(), new Fields("keyword")).groupBy(new Fields("keyword")).stateQuery(wordCount, new Fields("keyword"), new MapGet(), new Fields("result")).each(new Fields("result"), new FilterNull()).aggregate(new Fields("result"), new Sum(), new Fields("sum"));
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentWordCountTest");
    JStormUnitTestValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {

        Logger LOG = LoggerFactory.getLogger(JStormUnitTestValidator.class);

        @Override
        public boolean validate(Map config) {
            String queryResult = executeLocalDRPC("words", "the");
            // the result is like [[8080]], so remove the [[]]
            queryResult = queryResult.substring(2, queryResult.length() - 2);
            // how many times of emit can finish a loop
            int oneLoopNeedEmits = (int) Math.ceil(SPOUT_CONTENT_TYPES / (float) SPOUT_BATCH_SIZE);
            // of all the spout content
            // the loop time of the LimitFixBatchSpout content
            int loopTime = SPOUT_LIMIT / oneLoopNeedEmits;
            int receiveCountOfThe = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfThe + " \"the\" when expected " + (loopTime * 5));
            // 5 "the" are in one loop
            boolean isCountOfTheRight = (receiveCountOfThe == loopTime * 5);
            // query the word count of these 3 words total
            queryResult = executeLocalDRPC("words", "be store kujou");
            queryResult = queryResult.substring(2, queryResult.length() - 2);
            int receiveCountOfBeAndStore = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfBeAndStore + " \"be\" and \"store\" and \"kujou\" " + "when expected " + (loopTime * 3));
            // 2 "be" 1 "store" 0 "kujou" are in one loop
            boolean isCountOfBeAndStoreRight = (receiveCountOfBeAndStore == loopTime * 3);
            return isCountOfTheRight && isCountOfBeAndStoreRight;
        }
    };
    try {
        boolean result = JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 90, validator);
        assertTrue("Topology should pass the validator", result);
    } finally {
        localDRPC.shutdown();
    }
}
Also used : FilterNull(storm.trident.operation.builtin.FilterNull) JStormUnitTestValidator(com.jstorm.example.unittests.utils.JStormUnitTestValidator) TridentState(storm.trident.TridentState) HashMap(java.util.HashMap) Values(backtype.storm.tuple.Values) LoggerFactory(org.slf4j.LoggerFactory) MapGet(storm.trident.operation.builtin.MapGet) Sum(storm.trident.operation.builtin.Sum) JStormUnitTestDRPCValidator(com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator) Count(storm.trident.operation.builtin.Count) Logger(org.slf4j.Logger) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) LocalDRPC(backtype.storm.LocalDRPC) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with TridentState

use of storm.trident.TridentState in project jstorm by alibaba.

the class TridentReachTest method testTridentReach.

@Test
public void testTridentReach() {
    TridentTopology tridentTopology = new TridentTopology();
    TridentState urlToTweeters = tridentTopology.newStaticState(new TridentReach.StaticSingleKeyMapState.Factory(TWEETERS));
    TridentState tweetersToFollowers = tridentTopology.newStaticState(new TridentReach.StaticSingleKeyMapState.Factory(FOLLOWERS));
    LocalDRPC localDRPC = new LocalDRPC();
    tridentTopology.newDRPCStream("reach", localDRPC).stateQuery(urlToTweeters, new Fields("args"), new MapGet(), new Fields("tweeters")).each(new Fields("tweeters"), new TridentReach.ExpandList(), new Fields("tweeter")).shuffle().stateQuery(tweetersToFollowers, new Fields("tweeter"), new MapGet(), new Fields("followers")).each(new Fields("followers"), new TridentReach.ExpandList(), new Fields("follower")).groupBy(new Fields("follower")).aggregate(new TridentReach.One(), new Fields("one")).aggregate(new Fields("one"), new Sum(), new Fields("reach"));
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentReachTest");
    JStormUnitTestDRPCValidator validator = new JStormUnitTestDRPCValidator(localDRPC) {

        @Override
        public boolean validate(Map config) {
            String query = executeLocalDRPC("reach", "aaa");
            assertEquals("[[0]]", query);
            query = executeLocalDRPC("reach", "foo.com/blog/1");
            assertEquals("[[16]]", query);
            query = executeLocalDRPC("reach", "engineering.twitter.com/blog/5");
            assertEquals("[[14]]", query);
            return true;
        }
    };
    try {
        JStormUnitTestRunner.submitTopology(tridentTopology.build(), config, 120, validator);
    } finally {
        localDRPC.shutdown();
    }
}
Also used : TridentReach(org.apache.storm.starter.trident.TridentReach) TridentState(storm.trident.TridentState) HashMap(java.util.HashMap) MapGet(storm.trident.operation.builtin.MapGet) Sum(storm.trident.operation.builtin.Sum) JStormUnitTestDRPCValidator(com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) LocalDRPC(backtype.storm.LocalDRPC) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with TridentState

use of storm.trident.TridentState in project jstorm by alibaba.

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(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) TridentState(storm.trident.TridentState) MapGet(storm.trident.operation.builtin.MapGet) Sum(storm.trident.operation.builtin.Sum)

Example 4 with TridentState

use of storm.trident.TridentState in project jstorm by alibaba.

the class TridentWordCount method buildTopology.

public static StormTopology buildTopology(LocalDRPC drpc) {
    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 wordCounts = topology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    topology.newDRPCStream("words", drpc).each(new Fields("args"), new Split(), new Fields("word")).groupBy(new Fields("word")).stateQuery(wordCounts, new Fields("word"), new MapGet(), new Fields("count")).each(new Fields("count"), new FilterNull()).aggregate(new Fields("count"), new Sum(), new Fields("sum"));
    return topology.build();
}
Also used : FixedBatchSpout(storm.trident.testing.FixedBatchSpout) FilterNull(storm.trident.operation.builtin.FilterNull) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) TridentState(storm.trident.TridentState) Values(backtype.storm.tuple.Values) MapGet(storm.trident.operation.builtin.MapGet) Sum(storm.trident.operation.builtin.Sum) Count(storm.trident.operation.builtin.Count)

Example 5 with TridentState

use of storm.trident.TridentState in project jstorm by alibaba.

the class TridentFastWordCount method buildTopology.

public static StormTopology buildTopology(LocalDRPC drpc) {
    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"), new Values("marry had a little lamb whos fleese was white as snow"), new Values("and every where that marry went the lamb was sure to go"), new Values("one two three four five six seven eight nine ten"), new Values("this is a test of the emergency broadcast system this is only a test"), new Values("peter piper picked a peck of pickeled peppers"), new Values("JStorm is a distributed and fault-tolerant realtime computation system."), new Values("Inspired by Apache Storm, JStorm has been completely rewritten in Java and provides many more enhanced features."), new Values("JStorm has been widely used in many enterprise environments and proved robust and stable."), new Values("JStorm provides a distributed programming framework very similar to Hadoop MapReduce."), new Values("The developer only needs to compose his/her own pipe-lined computation logic by implementing the JStorm API"), new Values(" which is fully compatible with Apache Storm API"), new Values("and submit the composed Topology to a working JStorm instance."), new Values("Similar to Hadoop MapReduce, JStorm computes on a DAG (directed acyclic graph)."), new Values("Different from Hadoop MapReduce, a JStorm topology runs 24 * 7"), new Values("the very nature of its continuity abd 100% in-memory architecture "), new Values("has been proved a particularly suitable solution for streaming data and real-time computation."), new Values("JStorm guarantees fault-tolerance."), new Values("Whenever a worker process crashes, "), new Values("the scheduler embedded in the JStorm instance immediately spawns a new worker process to take the place of the failed one."), new Values(" The Acking framework provided by JStorm guarantees that every single piece of data will be processed at least once."));
    spout.setCycle(true);
    int spout_Parallelism_hint = JStormUtils.parseInt(conf.get(TOPOLOGY_SPOUT_PARALLELISM_HINT), 1);
    int split_Parallelism_hint = JStormUtils.parseInt(conf.get(TOPOLOGY_SPLIT_PARALLELISM_HINT), 2);
    int count_Parallelism_hint = JStormUtils.parseInt(conf.get(TOPOLOGY_COUNT_PARALLELISM_HINT), 2);
    TridentTopology topology = new TridentTopology();
    TridentState wordCounts = topology.newStream("spout1", spout).parallelismHint(spout_Parallelism_hint).each(new Fields("sentence"), new Split(), new Fields("word")).parallelismHint(split_Parallelism_hint).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(count_Parallelism_hint);
    return topology.build();
}
Also used : FixedBatchSpout(storm.trident.testing.FixedBatchSpout) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) TridentState(storm.trident.TridentState) Values(backtype.storm.tuple.Values) Count(storm.trident.operation.builtin.Count)

Aggregations

Fields (backtype.storm.tuple.Fields)7 TridentState (storm.trident.TridentState)7 TridentTopology (storm.trident.TridentTopology)7 MapGet (storm.trident.operation.builtin.MapGet)6 Sum (storm.trident.operation.builtin.Sum)6 Values (backtype.storm.tuple.Values)5 Count (storm.trident.operation.builtin.Count)5 FilterNull (storm.trident.operation.builtin.FilterNull)4 LocalDRPC (backtype.storm.LocalDRPC)3 JStormUnitTestDRPCValidator (com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator)3 Test (org.junit.Test)3 FixedBatchSpout (storm.trident.testing.FixedBatchSpout)3 JStormUnitTestValidator (com.jstorm.example.unittests.utils.JStormUnitTestValidator)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 TridentReach (org.apache.storm.starter.trident.TridentReach)1 Consumer (storm.trident.operation.Consumer)1 TridentTuple (storm.trident.tuple.TridentTuple)1