Search in sources :

Example 21 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class WordCountTrident method buildTopology.

public static StormTopology buildTopology(String url, String collectionName) {
    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);
    MongoMapper mapper = new SimpleMongoMapper().withFields("word", "count");
    MongoState.Options options = new MongoState.Options().withUrl(url).withCollectionName(collectionName).withMapper(mapper);
    StateFactory factory = new MongoStateFactory(options);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout);
    stream.partitionPersist(factory, fields, new MongoStateUpdater(), new Fields());
    TridentState state = topology.newStaticState(factory);
    stream = stream.stateQuery(state, new Fields("word"), new MongoStateQuery(), new Fields("columnName", "columnValue"));
    stream.each(new Fields("word", "columnValue"), new PrintFunction(), new Fields());
    return topology.build();
}
Also used : MongoState(org.apache.storm.mongodb.trident.state.MongoState) TridentState(org.apache.storm.trident.TridentState) Values(org.apache.storm.tuple.Values) FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) SimpleMongoMapper(org.apache.storm.mongodb.common.mapper.SimpleMongoMapper) MongoMapper(org.apache.storm.mongodb.common.mapper.MongoMapper) StateFactory(org.apache.storm.trident.state.StateFactory) MongoStateFactory(org.apache.storm.mongodb.trident.state.MongoStateFactory) TridentTopology(org.apache.storm.trident.TridentTopology) MongoStateFactory(org.apache.storm.mongodb.trident.state.MongoStateFactory) Stream(org.apache.storm.trident.Stream) SimpleMongoMapper(org.apache.storm.mongodb.common.mapper.SimpleMongoMapper) MongoStateUpdater(org.apache.storm.mongodb.trident.state.MongoStateUpdater) MongoStateQuery(org.apache.storm.mongodb.trident.state.MongoStateQuery)

Example 22 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class SampleOpenTsdbTridentTopology method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        throw new IllegalArgumentException("There should be at least one argument. Run as `SampleOpenTsdbTridentTopology <tsdb-url>`");
    }
    String tsdbUrl = args[0];
    final OpenTsdbClient.Builder openTsdbClientBuilder = OpenTsdbClient.newBuilder(tsdbUrl);
    final OpenTsdbStateFactory openTsdbStateFactory = new OpenTsdbStateFactory(openTsdbClientBuilder, Collections.singletonList(TupleOpenTsdbDatapointMapper.DEFAULT_MAPPER));
    TridentTopology tridentTopology = new TridentTopology();
    final Stream stream = tridentTopology.newStream("metric-tsdb-stream", new MetricGenBatchSpout(10));
    stream.peek(new Consumer() {

        @Override
        public void accept(TridentTuple input) {
            LOG.info("########### Received tuple: [{}]", input);
        }
    }).partitionPersist(openTsdbStateFactory, MetricGenSpout.DEFAULT_METRIC_FIELDS, new OpenTsdbStateUpdater());
    Config conf = new Config();
    conf.setDebug(true);
    String topoName = "word-count";
    if (args.length > 1) {
        topoName = args[1];
    }
    conf.setNumWorkers(3);
    StormSubmitter.submitTopologyWithProgressBar(topoName, conf, tridentTopology.build());
}
Also used : OpenTsdbClient(org.apache.storm.opentsdb.client.OpenTsdbClient) Consumer(org.apache.storm.trident.operation.Consumer) TridentTopology(org.apache.storm.trident.TridentTopology) Config(org.apache.storm.Config) OpenTsdbStateUpdater(org.apache.storm.opentsdb.trident.OpenTsdbStateUpdater) OpenTsdbStateFactory(org.apache.storm.opentsdb.trident.OpenTsdbStateFactory) Stream(org.apache.storm.trident.Stream) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 23 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

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, 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", drpc).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)

Example 24 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

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

Example 25 with TridentTopology

use of org.apache.storm.trident.TridentTopology in project storm by apache.

the class TridentKafkaTopology method buildTopology.

private static StormTopology buildTopology(String brokerConnectionString) {
    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);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout);
    Properties props = new Properties();
    props.put("bootstrap.servers", brokerConnectionString);
    props.put("acks", "1");
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
    TridentKafkaStateFactory stateFactory = new TridentKafkaStateFactory().withProducerProperties(props).withKafkaTopicSelector(new DefaultTopicSelector("test")).withTridentTupleToKafkaMapper(new FieldNameBasedTupleToKafkaMapper("word", "count"));
    stream.partitionPersist(stateFactory, fields, new TridentKafkaUpdater(), new Fields());
    return topology.build();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) Values(org.apache.storm.tuple.Values) Stream(org.apache.storm.trident.Stream) DefaultTopicSelector(org.apache.storm.kafka.trident.selector.DefaultTopicSelector) Properties(java.util.Properties) FieldNameBasedTupleToKafkaMapper(org.apache.storm.kafka.trident.mapper.FieldNameBasedTupleToKafkaMapper)

Aggregations

TridentTopology (org.apache.storm.trident.TridentTopology)44 Fields (org.apache.storm.tuple.Fields)38 Stream (org.apache.storm.trident.Stream)25 Values (org.apache.storm.tuple.Values)25 TridentState (org.apache.storm.trident.TridentState)22 FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)19 StateFactory (org.apache.storm.trident.state.StateFactory)12 MapGet (org.apache.storm.trident.operation.builtin.MapGet)10 Sum (org.apache.storm.trident.operation.builtin.Sum)10 HashMap (java.util.HashMap)9 Count (org.apache.storm.trident.operation.builtin.Count)8 AbstractTridentProcessor (org.apache.storm.sql.AbstractTridentProcessor)7 QueryPlanner (org.apache.storm.sql.planner.trident.QueryPlanner)7 ISqlTridentDataSource (org.apache.storm.sql.runtime.ISqlTridentDataSource)7 TestUtils (org.apache.storm.sql.TestUtils)6 MockState.getCollectedValues (org.apache.storm.sql.TestUtils.MockState.getCollectedValues)6 Consumer (org.apache.storm.trident.operation.Consumer)6 TridentTuple (org.apache.storm.trident.tuple.TridentTuple)6 Test (org.junit.Test)6 FilterNull (org.apache.storm.trident.operation.builtin.FilterNull)5