Search in sources :

Example 1 with RedisDataTypeDescription

use of org.apache.storm.redis.common.mapper.RedisDataTypeDescription 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 RedisDataTypeDescription

use of org.apache.storm.redis.common.mapper.RedisDataTypeDescription 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 RedisDataTypeDescription

use of org.apache.storm.redis.common.mapper.RedisDataTypeDescription in project storm by apache.

the class RedisClusterMapState method retrieveValuesFromRedis.

/**
     * {@inheritDoc}
     */
@Override
protected List<String> retrieveValuesFromRedis(List<String> keys) {
    String[] stringKeys = keys.toArray(new String[keys.size()]);
    RedisDataTypeDescription description = this.options.dataTypeDescription;
    switch(description.getDataType()) {
        case STRING:
            List<String> values = Lists.newArrayList();
            for (String stringKey : keys) {
                String value = jedisCluster.get(stringKey);
                values.add(value);
            }
            return values;
        case HASH:
            return jedisCluster.hmget(description.getAdditionalKey(), stringKeys);
        default:
            throw new IllegalArgumentException("Cannot process such data type: " + description.getDataType());
    }
}
Also used : RedisDataTypeDescription(org.apache.storm.redis.common.mapper.RedisDataTypeDescription)

Example 4 with RedisDataTypeDescription

use of org.apache.storm.redis.common.mapper.RedisDataTypeDescription in project storm by apache.

the class RedisMapState method updateStatesToRedis.

/**
     * {@inheritDoc}
     */
@Override
protected void updateStatesToRedis(Map<String, String> keyValues) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        RedisDataTypeDescription description = this.options.dataTypeDescription;
        switch(description.getDataType()) {
            case STRING:
                String[] keyValue = buildKeyValuesList(keyValues);
                jedis.mset(keyValue);
                if (this.options.expireIntervalSec > 0) {
                    Pipeline pipe = jedis.pipelined();
                    for (int i = 0; i < keyValue.length; i += 2) {
                        pipe.expire(keyValue[i], this.options.expireIntervalSec);
                    }
                    pipe.sync();
                }
                break;
            case HASH:
                jedis.hmset(description.getAdditionalKey(), keyValues);
                if (this.options.expireIntervalSec > 0) {
                    jedis.expire(description.getAdditionalKey(), this.options.expireIntervalSec);
                }
                break;
            default:
                throw new IllegalArgumentException("Cannot process such data type: " + description.getDataType());
        }
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisDataTypeDescription(org.apache.storm.redis.common.mapper.RedisDataTypeDescription) Pipeline(redis.clients.jedis.Pipeline)

Example 5 with RedisDataTypeDescription

use of org.apache.storm.redis.common.mapper.RedisDataTypeDescription in project storm by apache.

the class RedisMapState method retrieveValuesFromRedis.

/**
     * {@inheritDoc}
     */
@Override
protected List<String> retrieveValuesFromRedis(List<String> keys) {
    String[] stringKeys = keys.toArray(new String[keys.size()]);
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        RedisDataTypeDescription description = this.options.dataTypeDescription;
        switch(description.getDataType()) {
            case STRING:
                return jedis.mget(stringKeys);
            case HASH:
                return jedis.hmget(description.getAdditionalKey(), stringKeys);
            default:
                throw new IllegalArgumentException("Cannot process such data type: " + description.getDataType());
        }
    } finally {
        if (jedis != null) {
            jedis.close();
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisDataTypeDescription(org.apache.storm.redis.common.mapper.RedisDataTypeDescription)

Aggregations

RedisDataTypeDescription (org.apache.storm.redis.common.mapper.RedisDataTypeDescription)5 Stream (org.apache.storm.trident.Stream)2 TridentState (org.apache.storm.trident.TridentState)2 TridentTopology (org.apache.storm.trident.TridentTopology)2 MapGet (org.apache.storm.trident.operation.builtin.MapGet)2 Sum (org.apache.storm.trident.operation.builtin.Sum)2 StateFactory (org.apache.storm.trident.state.StateFactory)2 FixedBatchSpout (org.apache.storm.trident.testing.FixedBatchSpout)2 Fields (org.apache.storm.tuple.Fields)2 Values (org.apache.storm.tuple.Values)2 Jedis (redis.clients.jedis.Jedis)2 InetSocketAddress (java.net.InetSocketAddress)1 HashSet (java.util.HashSet)1 JedisClusterConfig (org.apache.storm.redis.common.config.JedisClusterConfig)1 JedisPoolConfig (org.apache.storm.redis.common.config.JedisPoolConfig)1 Pipeline (redis.clients.jedis.Pipeline)1