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();
}
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();
}
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());
}
}
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();
}
}
}
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();
}
}
}
Aggregations