Search in sources :

Example 1 with Sum

use of org.apache.storm.trident.operation.builtin.Sum in project storm by apache.

the class WordCountTridentRedisClusterMap method buildTopology.

public static StormTopology buildTopology(String redisHostPort) {
    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);
    Set<InetSocketAddress> nodes = new HashSet<InetSocketAddress>();
    for (String hostPort : redisHostPort.split(",")) {
        String[] host_port = hostPort.split(":");
        nodes.add(new InetSocketAddress(host_port[0], Integer.valueOf(host_port[1])));
    }
    JedisClusterConfig clusterConfig = new JedisClusterConfig.Builder().setNodes(nodes).build();
    RedisDataTypeDescription dataTypeDescription = new RedisDataTypeDescription(RedisDataTypeDescription.RedisDataType.HASH, "test");
    StateFactory factory = RedisClusterMapState.transactional(clusterConfig, dataTypeDescription);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout);
    TridentState state = stream.groupBy(new Fields("word")).persistentAggregate(factory, new Fields("count"), new Sum(), new Fields("sum"));
    stream.stateQuery(state, new Fields("word"), new MapGet(), new Fields("sum")).each(new Fields("word", "sum"), new PrintFunction(), new Fields());
    return topology.build();
}
Also used : RedisDataTypeDescription(org.apache.storm.redis.common.mapper.RedisDataTypeDescription) TridentState(org.apache.storm.trident.TridentState) InetSocketAddress(java.net.InetSocketAddress) JedisClusterConfig(org.apache.storm.redis.common.config.JedisClusterConfig) Values(org.apache.storm.tuple.Values) Sum(org.apache.storm.trident.operation.builtin.Sum) MapGet(org.apache.storm.trident.operation.builtin.MapGet) FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) StateFactory(org.apache.storm.trident.state.StateFactory) TridentTopology(org.apache.storm.trident.TridentTopology) Stream(org.apache.storm.trident.Stream) HashSet(java.util.HashSet)

Example 2 with Sum

use of org.apache.storm.trident.operation.builtin.Sum in project storm by apache.

the class WordCountTridentRedisMap method buildTopology.

public static StormTopology buildTopology(String redisHost, Integer redisPort) {
    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);
    JedisPoolConfig poolConfig = new JedisPoolConfig.Builder().setHost(redisHost).setPort(redisPort).build();
    RedisDataTypeDescription dataTypeDescription = new RedisDataTypeDescription(RedisDataTypeDescription.RedisDataType.HASH, "test");
    StateFactory factory = RedisMapState.transactional(poolConfig, dataTypeDescription);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout);
    TridentState state = stream.groupBy(new Fields("word")).persistentAggregate(factory, new Fields("count"), new Sum(), new Fields("sum"));
    stream.stateQuery(state, new Fields("word"), new MapGet(), new Fields("sum")).each(new Fields("word", "sum"), new PrintFunction(), new Fields());
    return topology.build();
}
Also used : RedisDataTypeDescription(org.apache.storm.redis.common.mapper.RedisDataTypeDescription) TridentState(org.apache.storm.trident.TridentState) Values(org.apache.storm.tuple.Values) Sum(org.apache.storm.trident.operation.builtin.Sum) MapGet(org.apache.storm.trident.operation.builtin.MapGet) JedisPoolConfig(org.apache.storm.redis.common.config.JedisPoolConfig) FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) Fields(org.apache.storm.tuple.Fields) StateFactory(org.apache.storm.trident.state.StateFactory) TridentTopology(org.apache.storm.trident.TridentTopology) Stream(org.apache.storm.trident.Stream)

Example 3 with Sum

use of org.apache.storm.trident.operation.builtin.Sum in project storm by apache.

the class WordCountTridentMap 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");
    QueryFilterCreator filterCreator = new SimpleQueryFilterCreator().withField("word");
    MongoMapState.Options options = new MongoMapState.Options();
    options.url = url;
    options.collectionName = collectionName;
    options.mapper = mapper;
    options.queryCreator = filterCreator;
    StateFactory factory = MongoMapState.transactional(options);
    TridentTopology topology = new TridentTopology();
    Stream stream = topology.newStream("spout1", spout);
    TridentState state = stream.groupBy(new Fields("word")).persistentAggregate(factory, new Fields("count"), new Sum(), new Fields("sum"));
    stream.stateQuery(state, new Fields("word"), new MapGet(), new Fields("sum")).each(new Fields("word", "sum"), new PrintFunction(), new Fields());
    return topology.build();
}
Also used : TridentState(org.apache.storm.trident.TridentState) Values(org.apache.storm.tuple.Values) Sum(org.apache.storm.trident.operation.builtin.Sum) MapGet(org.apache.storm.trident.operation.builtin.MapGet) SimpleQueryFilterCreator(org.apache.storm.mongodb.common.SimpleQueryFilterCreator) QueryFilterCreator(org.apache.storm.mongodb.common.QueryFilterCreator) SimpleQueryFilterCreator(org.apache.storm.mongodb.common.SimpleQueryFilterCreator) 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) TridentTopology(org.apache.storm.trident.TridentTopology) Stream(org.apache.storm.trident.Stream) SimpleMongoMapper(org.apache.storm.mongodb.common.mapper.SimpleMongoMapper)

Example 4 with Sum

use of org.apache.storm.trident.operation.builtin.Sum in project storm by apache.

the class OpaqueTridentEventCount method buildTopology.

@Override
protected StormTopology buildTopology(EventHubSpout eventHubSpout) {
    TridentTopology topology = new TridentTopology();
    OpaqueTridentEventHubSpout spout = new OpaqueTridentEventHubSpout(spoutConfig);
    TridentState state = topology.newStream("stream-" + spoutConfig.getTopologyName(), spout).parallelismHint(spoutConfig.getPartitionCount()).aggregate(new Count(), new Fields("partial-count")).persistentAggregate(new MemoryMapState.Factory(), new Fields("partial-count"), new Sum(), new Fields("count"));
    state.newValuesStream().each(new Fields("count"), new LoggingFilter("got count: ", 10000));
    return topology.build();
}
Also used : Fields(org.apache.storm.tuple.Fields) TridentTopology(org.apache.storm.trident.TridentTopology) OpaqueTridentEventHubSpout(org.apache.storm.eventhubs.trident.OpaqueTridentEventHubSpout) TridentState(org.apache.storm.trident.TridentState) MemoryMapState(org.apache.storm.trident.testing.MemoryMapState) LoggingFilter(org.apache.storm.eventhubs.samples.TransactionalTridentEventCount.LoggingFilter) Sum(org.apache.storm.trident.operation.builtin.Sum) Count(org.apache.storm.trident.operation.builtin.Count)

Example 5 with Sum

use of org.apache.storm.trident.operation.builtin.Sum in project storm by apache.

the class TestTridentTopology method buildTopology.

private StormTopology buildTopology() {
    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(true);
    TridentTopology topology = new TridentTopology();
    topology.newStream("spout", spout).each(new Fields("sentence"), new Split(), new Fields("word")).partitionBy(new Fields("word")).name("abc").each(new Fields("word"), new StringLength(), new Fields("length")).partitionBy(new Fields("length")).name("def").aggregate(new Fields("length"), new Count(), new Fields("count")).partitionBy(new Fields("count")).name("ghi").aggregate(new Fields("count"), new Sum(), new Fields("sum"));
    return topology.build();
}
Also used : FixedBatchSpout(org.apache.storm.trident.testing.FixedBatchSpout) StringLength(org.apache.storm.trident.testing.StringLength) Fields(org.apache.storm.tuple.Fields) Values(org.apache.storm.tuple.Values) Sum(org.apache.storm.trident.operation.builtin.Sum) Count(org.apache.storm.trident.operation.builtin.Count) Split(org.apache.storm.trident.testing.Split)

Aggregations

Sum (org.apache.storm.trident.operation.builtin.Sum)8 Fields (org.apache.storm.tuple.Fields)8 TridentState (org.apache.storm.trident.TridentState)7 TridentTopology (org.apache.storm.trident.TridentTopology)7 MapGet (org.apache.storm.trident.operation.builtin.MapGet)5 FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)5 Values (org.apache.storm.tuple.Values)5 Count (org.apache.storm.trident.operation.builtin.Count)4 Stream (org.apache.storm.trident.Stream)3 StateFactory (org.apache.storm.trident.state.StateFactory)3 RedisDataTypeDescription (org.apache.storm.redis.common.mapper.RedisDataTypeDescription)2 MemoryMapState (org.apache.storm.trident.testing.MemoryMapState)2 InetSocketAddress (java.net.InetSocketAddress)1 HashSet (java.util.HashSet)1 LoggingFilter (org.apache.storm.eventhubs.samples.TransactionalTridentEventCount.LoggingFilter)1 OpaqueTridentEventHubSpout (org.apache.storm.eventhubs.trident.OpaqueTridentEventHubSpout)1 TransactionalTridentEventHubSpout (org.apache.storm.eventhubs.trident.TransactionalTridentEventHubSpout)1 QueryFilterCreator (org.apache.storm.mongodb.common.QueryFilterCreator)1 SimpleQueryFilterCreator (org.apache.storm.mongodb.common.SimpleQueryFilterCreator)1 MongoMapper (org.apache.storm.mongodb.common.mapper.MongoMapper)1