Search in sources :

Example 16 with TridentTopology

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

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).filter(theFilter).peek(new Consumer() {

        @Override
        public void accept(TridentTuple input) {
            System.out.println(input.getString(0));
        }
    }).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    topology.newDRPCStream("words", drpc).flatMap(split).groupBy(new Fields("args")).stateQuery(wordCounts, new Fields("args"), new MapGet(), new Fields("count")).filter(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) Consumer(storm.trident.operation.Consumer) 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) TridentTuple(storm.trident.tuple.TridentTuple)

Example 17 with TridentTopology

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

the class TridentMinMaxOfVehiclesTopology method buildVehiclesTopology.

/**
 * Creates a topology which demonstrates min/max operations on tuples of
 * stream which contain vehicle and driver fields with values
 * {@link TridentMinMaxOfVehiclesTopology.Vehicle} and
 * {@link TridentMinMaxOfVehiclesTopology.Driver} respectively.
 */
public static StormTopology buildVehiclesTopology() {
    Fields driverField = new Fields(Driver.FIELD_NAME);
    Fields vehicleField = new Fields(Vehicle.FIELD_NAME);
    Fields allFields = new Fields(Vehicle.FIELD_NAME, Driver.FIELD_NAME);
    FixedBatchSpout spout = new FixedBatchSpout(allFields, 10, Vehicle.generateVehicles(20));
    spout.setCycle(true);
    TridentTopology topology = new TridentTopology();
    Stream vehiclesStream = topology.newStream("spout1", spout).each(allFields, new Debug("##### vehicles"));
    Stream slowVehiclesStream = vehiclesStream.min(new SpeedComparator()).each(vehicleField, new Debug("#### slowest vehicle"));
    Stream slowDriversStream = slowVehiclesStream.project(driverField).each(driverField, new Debug("##### slowest driver"));
    vehiclesStream.max(new SpeedComparator()).each(vehicleField, new Debug("#### fastest vehicle")).project(driverField).each(driverField, new Debug("##### fastest driver"));
    vehiclesStream.minBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### least efficient vehicle"));
    vehiclesStream.maxBy(Vehicle.FIELD_NAME, new EfficiencyComparator()).each(vehicleField, new Debug("#### most efficient vehicle"));
    return topology.build();
}
Also used : FixedBatchSpout(storm.trident.testing.FixedBatchSpout) Fields(backtype.storm.tuple.Fields) TridentTopology(storm.trident.TridentTopology) Stream(storm.trident.Stream) Debug(storm.trident.operation.builtin.Debug)

Example 18 with TridentTopology

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

the class TridentSlidingDurationWindowTest method testTridentSlidingDurationWindow.

@Test
public void testTridentSlidingDurationWindow() {
    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, "TridentSlidingDurationWindowTest");
    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 19 with TridentTopology

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

the class TridentMapTest method testTridentMap.

// to make sure the validator is right
@Test
public void testTridentMap() {
    LocalDRPC localDRPC = new LocalDRPC();
    FixedLimitBatchSpout spout = new FixedLimitBatchSpout(SPOUT_LIMIT, new Fields("word"), 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).flatMap(new SplitFlatMapFunction()).map(new UpperMapFunction()).filter(new Fields("word"), new WordFilter("THE")).peek(new LogConsumer()).groupBy(new Fields("word")).persistentAggregate(new MemoryMapState.Factory(), new Count(), new Fields("count")).parallelismHint(16);
    tridentTopology.newDRPCStream("words", localDRPC).flatMap(new SplitFlatMapFunction()).groupBy(new Fields("args")).stateQuery(wordCount, new Fields("args"), new MapGet(), new Fields("result")).filter(new Fields("result"), new FilterNull()).aggregate(new Fields("result"), new Sum(), new Fields("sum"));
    Map config = new HashMap();
    config.put(Config.TOPOLOGY_NAME, "TridentMapTest");
    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 receiveCountOfUpperCase = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfUpperCase + " \"THE\" when expected " + (loopTime * 5));
            // 5 "the" are in one loop
            boolean isCountOfUpperCaseRight = (receiveCountOfUpperCase == loopTime * 5);
            // query the word count of these 3 words total
            queryResult = executeLocalDRPC("words", "the tHe thE");
            queryResult = queryResult.substring(2, queryResult.length() - 2);
            int receiveCountOfLowerCase = Integer.valueOf(queryResult);
            LOG.info("Final receive total " + receiveCountOfLowerCase + " \"the\" and \"tHe\" and \"thE\" " + "when expected " + 0);
            // no the tHe thE should pass the UpperFilter
            boolean isCountOfLowerCaseRight = (receiveCountOfLowerCase == 0);
            return isCountOfUpperCaseRight && isCountOfLowerCaseRight;
        }
    };
    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) 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) Test(org.junit.Test)

Example 20 with TridentTopology

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

the class TridentSlidingCountWindowTest method testTridentSlidingCountWindow.

@Test
public void testTridentSlidingCountWindow() {
    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, "TridentSlidingCountWindowTest");
    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)

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