Search in sources :

Example 41 with Values

use of org.apache.storm.tuple.Values in project storm by apache.

the class TridentWindowingInmemoryStoreTopology method buildTopology.

public static StormTopology buildTopology(WindowsStoreFactory windowStore, WindowConfig windowConfig) 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(windowConfig, windowStore, 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 42 with Values

use of org.apache.storm.tuple.Values in project storm by apache.

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()).project(new Fields("word", "count"));
    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) 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) Count(org.apache.storm.trident.operation.builtin.Count)

Example 43 with Values

use of org.apache.storm.tuple.Values in project storm by apache.

the class JoinBoltExample method generateGenderData.

private static void generateGenderData(FeederSpout genderSpout) {
    for (int i = 0; i < 10; i++) {
        String gender;
        if (i % 2 == 0) {
            gender = "male";
        } else {
            gender = "female";
        }
        genderSpout.feed(new Values(i, gender));
    }
}
Also used : Values(org.apache.storm.tuple.Values)

Example 44 with Values

use of org.apache.storm.tuple.Values in project storm by apache.

the class SingleJoinExample method main.

public static void main(String[] args) throws Exception {
    FeederSpout genderSpout = new FeederSpout(new Fields("id", "gender"));
    FeederSpout ageSpout = new FeederSpout(new Fields("id", "age"));
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("gender", genderSpout);
    builder.setSpout("age", ageSpout);
    builder.setBolt("join", new SingleJoinBolt(new Fields("gender", "age"))).fieldsGrouping("gender", new Fields("id")).fieldsGrouping("age", new Fields("id"));
    Config conf = new Config();
    conf.setDebug(true);
    try (LocalCluster cluster = new LocalCluster();
        LocalTopology topo = cluster.submitTopology("join-example", conf, builder.createTopology())) {
        for (int i = 0; i < 10; i++) {
            String gender;
            if (i % 2 == 0) {
                gender = "male";
            } else {
                gender = "female";
            }
            genderSpout.feed(new Values(i, gender));
        }
        for (int i = 9; i >= 0; i--) {
            ageSpout.feed(new Values(i, i + 20));
        }
        Utils.sleep(2000);
    }
}
Also used : LocalCluster(org.apache.storm.LocalCluster) Fields(org.apache.storm.tuple.Fields) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) Config(org.apache.storm.Config) SingleJoinBolt(org.apache.storm.starter.bolt.SingleJoinBolt) Values(org.apache.storm.tuple.Values) FeederSpout(org.apache.storm.testing.FeederSpout) LocalTopology(org.apache.storm.LocalCluster.LocalTopology)

Example 45 with Values

use of org.apache.storm.tuple.Values in project storm by apache.

the class RandomSentenceSpout method nextTuple.

@Override
public void nextTuple() {
    Utils.sleep(100);
    String[] sentences = new String[] { sentence("the cow jumped over the moon"), sentence("an apple a day keeps the doctor away"), sentence("four score and seven years ago"), sentence("snow white and the seven dwarfs"), sentence("i am at two with nature") };
    final String sentence = sentences[_rand.nextInt(sentences.length)];
    LOG.debug("Emitting tuple: {}", sentence);
    _collector.emit(new Values(sentence));
}
Also used : Values(org.apache.storm.tuple.Values)

Aggregations

Values (org.apache.storm.tuple.Values)206 Test (org.junit.Test)89 ArrayList (java.util.ArrayList)42 Fields (org.apache.storm.tuple.Fields)40 HashMap (java.util.HashMap)39 ChannelHandler (org.apache.storm.sql.runtime.ChannelHandler)26 TridentTopology (org.apache.storm.trident.TridentTopology)21 FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)14 Stream (org.apache.storm.trident.Stream)12 TupleImpl (org.apache.storm.tuple.TupleImpl)12 List (java.util.List)11 TestUtils (org.apache.storm.sql.TestUtils)11 TridentState (org.apache.storm.trident.TridentState)11 Tuple (org.apache.storm.tuple.Tuple)11 JSONObject (org.json.simple.JSONObject)10 Map (java.util.Map)9 Config (org.apache.storm.Config)9 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)9 GeneralTopologyContext (org.apache.storm.task.GeneralTopologyContext)8 TridentTuple (org.apache.storm.trident.tuple.TridentTuple)8