Search in sources :

Example 1 with TridentTopology

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

the class TridentTumblingDurationWindowTest method testTridentTumblingDurationWindow.

@Test
public void testTridentTumblingDurationWindow() {
    WindowsStoreFactory windowsStoreFactory = new InMemoryWindowsStoreFactory();
    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();
    Stream stream = tridentTopology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).window(windowConfig, windowsStoreFactory, new Fields("word"), new CountAsAggregator(), new Fields("count")).peek(new ValidateConsumer());
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentTumblingDurationWindowTest");
    JStormUnitTestRunner.submitTopology(tridentTopology.build(), null, 120, null);
}
Also used : Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) CountAsAggregator(storm.trident.testing.CountAsAggregator) HashMap(java.util.HashMap) Values(backtype.storm.tuple.Values) InMemoryWindowsStoreFactory(storm.trident.windowing.InMemoryWindowsStoreFactory) Stream(storm.trident.Stream) Split(storm.trident.testing.Split) HashMap(java.util.HashMap) Map(java.util.Map) WindowsStoreFactory(storm.trident.windowing.WindowsStoreFactory) InMemoryWindowsStoreFactory(storm.trident.windowing.InMemoryWindowsStoreFactory) Test(org.junit.Test)

Example 2 with TridentTopology

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

the class TridentMinMaxOfVehiclesTest method testTridentMinMaxOfVehicles.

@Test
public void testTridentMinMaxOfVehicles() {
    Fields driverField = new Fields(Driver.FIELD_NAME);
    Fields vehicleField = new Fields(Vehicle.FIELD_NAME);
    Fields fields = new Fields(Vehicle.FIELD_NAME, Driver.FIELD_NAME);
    Random random = new Random(System.currentTimeMillis());
    List<Values> vehicleContent = new ArrayList<Values>();
    List<Values> driverContent = new ArrayList<Values>();
    int maxSpeed = -1, minSpeed = 10000;
    double maxEfficiency = -1, minEfficiency = 10000;
    for (int i = 0; i < SPOUT_BATCH_SIZE; i++) {
        int speed = random.nextInt(10000);
        maxSpeed = Math.max(speed, maxSpeed);
        minSpeed = Math.min(speed, minSpeed);
        double efficiency = random.nextDouble() * 10000;
        maxEfficiency = Math.max(efficiency, maxEfficiency);
        minEfficiency = Math.min(efficiency, minEfficiency);
        vehicleContent.add(new Values(new Vehicle("vehicle-" + (i + 1), speed, efficiency)));
        driverContent.add(new Values(new Driver("driver-" + (i + 1), i + 1)));
    }
    ShuffleValuesBatchSpout spout = new ShuffleValuesBatchSpout(fields, vehicleContent, driverContent);
    TridentTopology tridentTopology = new TridentTopology();
    Stream vehiclesStream = tridentTopology.newStream("spout", spout).each(fields, new Debug("#### vehicles"));
    Stream slowVehiclesStream = vehiclesStream.min(new SpeedComparator()).each(vehicleField, new Debug("#### slowest vehicle")).peek(new SpeedValidator(minSpeed));
    Stream slowDriversStream = slowVehiclesStream.project(driverField).each(driverField, new Debug("#### slowest driver"));
    vehiclesStream.max(new SpeedComparator()).each(vehicleField, new Debug("#### fastest vehicle")).peek(new SpeedValidator(maxSpeed)).project(driverField).each(driverField, new Debug("#### fastest driver"));
    vehiclesStream.minBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### least efficient vehicle")).peek(new EfficiencyValidator(minEfficiency));
    vehiclesStream.maxBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### most efficient vehicle")).peek(new EfficiencyValidator(maxEfficiency));
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentMinMaxOfVehiclesTest");
    // use the assert in the body of consumer.accept() to validate
    JStormUnitTestRunner.submitTopology(tridentTopology.build(), null, 120, null);
}
Also used : Values(backtype.storm.tuple.Values) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) Stream(storm.trident.Stream) Debug(storm.trident.operation.builtin.Debug) Test(org.junit.Test)

Example 3 with TridentTopology

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

the class TridentTumblingCountWindowTest method testTridentTumblingCountWindow.

@Test
public void testTridentTumblingCountWindow() {
    WindowsStoreFactory windowsStoreFactory = new InMemoryWindowsStoreFactory();
    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();
    Stream stream = tridentTopology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).window(windowConfig, windowsStoreFactory, new Fields("word"), new CountAsAggregator(), new Fields("count")).peek(new ValidateConsumer());
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentTumblingCountWindowTest");
    JStormUnitTestRunner.submitTopology(tridentTopology.build(), null, 120, null);
}
Also used : Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) CountAsAggregator(storm.trident.testing.CountAsAggregator) HashMap(java.util.HashMap) Values(backtype.storm.tuple.Values) InMemoryWindowsStoreFactory(storm.trident.windowing.InMemoryWindowsStoreFactory) Stream(storm.trident.Stream) Split(storm.trident.testing.Split) HashMap(java.util.HashMap) Map(java.util.Map) WindowsStoreFactory(storm.trident.windowing.WindowsStoreFactory) InMemoryWindowsStoreFactory(storm.trident.windowing.InMemoryWindowsStoreFactory) Test(org.junit.Test)

Example 4 with TridentTopology

use of storm.trident.TridentTopology 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 5 with TridentTopology

use of storm.trident.TridentTopology 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)

Aggregations

Fields (backtype.storm.tuple.Fields)21 TridentTopology (storm.trident.TridentTopology)21 Values (backtype.storm.tuple.Values)13 Stream (storm.trident.Stream)10 Test (org.junit.Test)9 FixedBatchSpout (storm.trident.testing.FixedBatchSpout)9 HashMap (java.util.HashMap)7 Map (java.util.Map)7 TridentState (storm.trident.TridentState)7 Count (storm.trident.operation.builtin.Count)7 MapGet (storm.trident.operation.builtin.MapGet)6 Sum (storm.trident.operation.builtin.Sum)6 Debug (storm.trident.operation.builtin.Debug)5 CountAsAggregator (storm.trident.testing.CountAsAggregator)5 Split (storm.trident.testing.Split)5 FilterNull (storm.trident.operation.builtin.FilterNull)4 InMemoryWindowsStoreFactory (storm.trident.windowing.InMemoryWindowsStoreFactory)4 WindowsStoreFactory (storm.trident.windowing.WindowsStoreFactory)4 LocalDRPC (backtype.storm.LocalDRPC)3 JStormUnitTestDRPCValidator (com.jstorm.example.unittests.utils.JStormUnitTestDRPCValidator)3