Search in sources :

Example 16 with FixedBatchSpout

use of org.apache.storm.trident.testing.FixedBatchSpout in project storm by apache.

the class WordCountTrident method buildTopology.

public static StormTopology buildTopology(String nameserverAddr, String topic) {
    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);
    TupleToMessageMapper mapper = new FieldNameBasedTupleToMessageMapper("word", "count");
    TopicSelector selector = new DefaultTopicSelector(topic);
    Properties properties = new Properties();
    properties.setProperty(RocketMqConfig.NAME_SERVER_ADDR, nameserverAddr);
    RocketMqState.Options options = new RocketMqState.Options().withMapper(mapper).withSelector(selector).withProperties(properties);
    StateFactory factory = new RocketMqStateFactory(options);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout);
    stream.partitionPersist(factory, fields, new RocketMqStateUpdater(), new Fields());
    return topology.build();
}
Also used : FieldNameBasedTupleToMessageMapper(org.apache.storm.rocketmq.common.mapper.FieldNameBasedTupleToMessageMapper) TopicSelector(org.apache.storm.rocketmq.common.selector.TopicSelector) DefaultTopicSelector(org.apache.storm.rocketmq.common.selector.DefaultTopicSelector) Values(org.apache.storm.tuple.Values) Properties(java.util.Properties) FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) StateFactory(org.apache.storm.trident.state.StateFactory) RocketMqStateFactory(org.apache.storm.rocketmq.trident.state.RocketMqStateFactory) TupleToMessageMapper(org.apache.storm.rocketmq.common.mapper.TupleToMessageMapper) FieldNameBasedTupleToMessageMapper(org.apache.storm.rocketmq.common.mapper.FieldNameBasedTupleToMessageMapper) TridentTopology(org.apache.storm.trident.TridentTopology) DefaultTopicSelector(org.apache.storm.rocketmq.common.selector.DefaultTopicSelector) Stream(org.apache.storm.trident.Stream) RocketMqStateUpdater(org.apache.storm.rocketmq.trident.state.RocketMqStateUpdater) RocketMqState(org.apache.storm.rocketmq.trident.state.RocketMqState) RocketMqStateFactory(org.apache.storm.rocketmq.trident.state.RocketMqStateFactory)

Example 17 with FixedBatchSpout

use of org.apache.storm.trident.testing.FixedBatchSpout 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 18 with FixedBatchSpout

use of org.apache.storm.trident.testing.FixedBatchSpout in project storm by apache.

the class TridentHBaseWindowingStoreTopology method buildTopology.

public static StormTopology buildTopology(WindowsStoreFactory windowsStore) 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"), new Values("to be or not to be the person"));
    spout.setCycle(true);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout).parallelismHint(16).each(new Fields("sentence"), new Split(), new Fields("word")).window(TumblingCountWindow.of(1000), windowsStore, new Fields("word"), new CountAsAggregator(), new Fields("count")).peek(new Consumer() {

        @Override
        public void accept(TridentTuple input) {
            LOG.info("Received tuple: [{}]", input);
        }
    });
    return topology.build();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) Consumer(org.apache.storm.trident.operation.Consumer) TridentTopology(org.apache.storm.trident.TridentTopology) CountAsAggregator(org.apache.storm.trident.testing.CountAsAggregator) Values(org.apache.storm.tuple.Values) Stream(org.apache.storm.trident.Stream) Split(org.apache.storm.trident.testing.Split) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 19 with FixedBatchSpout

use of org.apache.storm.trident.testing.FixedBatchSpout in project storm by apache.

the class TridentMinMaxOfDevicesTopology method buildVehiclesTopology.

/**
 * Creates a topology which demonstrates min/max operations on tuples of stream which contain vehicle and driver fields
 * with values {@link TridentMinMaxOfDevicesTopology.Vehicle} and {@link TridentMinMaxOfDevicesTopology.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.max(new EfficiencyComparator()).each(vehicleField, new Debug("#### efficient vehicle"));
    return topology.build();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) Stream(org.apache.storm.trident.Stream) Debug(org.apache.storm.trident.operation.builtin.Debug)

Example 20 with FixedBatchSpout

use of org.apache.storm.trident.testing.FixedBatchSpout 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

FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)20 Fields (org.apache.storm.tuple.Fields)20 TridentTopology (org.apache.storm.trident.TridentTopology)19 Values (org.apache.storm.tuple.Values)18 Stream (org.apache.storm.trident.Stream)13 TridentState (org.apache.storm.trident.TridentState)12 MapGet (org.apache.storm.trident.operation.builtin.MapGet)8 Count (org.apache.storm.trident.operation.builtin.Count)7 Sum (org.apache.storm.trident.operation.builtin.Sum)7 StateFactory (org.apache.storm.trident.state.StateFactory)6 FilterNull (org.apache.storm.trident.operation.builtin.FilterNull)5 Consumer (org.apache.storm.trident.operation.Consumer)4 Split (org.apache.storm.trident.testing.Split)4 TridentTuple (org.apache.storm.trident.tuple.TridentTuple)4 InetSocketAddress (java.net.InetSocketAddress)2 HashSet (java.util.HashSet)2 Properties (java.util.Properties)2 MongoMapper (org.apache.storm.mongodb.common.mapper.MongoMapper)2 SimpleMongoMapper (org.apache.storm.mongodb.common.mapper.SimpleMongoMapper)2 JedisClusterConfig (org.apache.storm.redis.common.config.JedisClusterConfig)2