[storm-redis] Include explanation of basic bolts
Project: http://git-wip-us.apache.org/repos/asf/storm/repo Commit: http://git-wip-us.apache.org/repos/asf/storm/commit/4b9b9767 Tree: http://git-wip-us.apache.org/repos/asf/storm/tree/4b9b9767 Diff: http://git-wip-us.apache.org/repos/asf/storm/diff/4b9b9767 Branch: refs/heads/nimbus-ha-branch Commit: 4b9b9767c2bcc9b10a541d950a81827d89b4607e Parents: 286c52b Author: Jungtaek Lim <[email protected]> Authored: Wed Mar 25 18:31:03 2015 +0900 Committer: Jungtaek Lim <[email protected]> Committed: Wed Mar 25 18:31:03 2015 +0900 ---------------------------------------------------------------------- external/storm-redis/README.md | 108 +++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/storm/blob/4b9b9767/external/storm-redis/README.md ---------------------------------------------------------------------- diff --git a/external/storm-redis/README.md b/external/storm-redis/README.md index b01854f..32480e6 100644 --- a/external/storm-redis/README.md +++ b/external/storm-redis/README.md @@ -2,6 +2,8 @@ Storm/Trident integration for [Redis](http://redis.io/) +Storm-redis uses Jedis for Redis client. + ## Usage ### How do I use it? @@ -12,12 +14,114 @@ use it as a maven dependency: <dependency> <groupId>org.apache.storm</groupId> <artifactId>storm-redis</artifactId> - <version>0.10.0</version> + <version>{storm.version}</version> <type>jar</type> </dependency> ``` -### AbstractRedisBolt usage: +### For normal Bolt + +Storm-redis provides basic Bolt implementations, ```RedisLookupBolt``` and ```RedisStoreBolt```. + +As name represents its usage, ```RedisLookupBolt``` retrieves value from Redis using key, and ```RedisStoreBolt``` stores key / value to Redis. One tuple will be matched to one key / value pair, and you can define match pattern to ```TupleMapper```. + +You can also choose data type from ```RedisDataTypeDescription``` to use. Please refer ```RedisDataTypeDescription.RedisDataType``` to see what data types are supported. In some data types (hash and sorted set), it requires additional key and converted key from tuple becomes element. + +These interfaces are combined with ```RedisLookupMapper``` and ```RedisStoreMapper``` which fit ```RedisLookupBolt``` and ```RedisStoreBolt``` respectively. + +#### RedisLookupBolt example + +```java + +class WordCountRedisLookupMapper implements RedisLookupMapper { + private RedisDataTypeDescription description; + private final String hashKey = "wordCount"; + + public WordCountRedisLookupMapper() { + description = new RedisDataTypeDescription( + RedisDataTypeDescription.RedisDataType.HASH, hashKey); + } + + @Override + public List<Values> toTuple(ITuple input, Object value) { + String member = getKeyFromTuple(input); + List<Values> values = Lists.newArrayList(); + values.add(new Values(member, value)); + return values; + } + + @Override + public void declareOutputFields(OutputFieldsDeclarer declarer) { + declarer.declare(new Fields("wordName", "count")); + } + + @Override + public RedisDataTypeDescription getDataTypeDescription() { + return description; + } + + @Override + public String getKeyFromTuple(ITuple tuple) { + return tuple.getStringByField("word"); + } + + @Override + public String getValueFromTuple(ITuple tuple) { + return null; + } +} + +``` + +```java + +JedisPoolConfig poolConfig = new JedisPoolConfig.Builder() + .setHost(host).setPort(port).build(); +RedisLookupMapper lookupMapper = new WordCountRedisLookupMapper(); +RedisLookupBolt lookupBolt = new RedisLookupBolt(poolConfig, lookupMapper); +``` + +#### RedisStoreBolt example + +```java + +class WordCountStoreMapper implements RedisStoreMapper { + private RedisDataTypeDescription description; + private final String hashKey = "wordCount"; + + public WordCountStoreMapper() { + description = new RedisDataTypeDescription( + RedisDataTypeDescription.RedisDataType.HASH, hashKey); + } + + @Override + public RedisDataTypeDescription getDataTypeDescription() { + return description; + } + + @Override + public String getKeyFromTuple(ITuple tuple) { + return tuple.getStringByField("word"); + } + + @Override + public String getValueFromTuple(ITuple tuple) { + return tuple.getStringByField("count"); + } +} +``` + +```java + +JedisPoolConfig poolConfig = new JedisPoolConfig.Builder() + .setHost(host).setPort(port).build(); +RedisStoreMapper storeMapper = new WordCountStoreMapper(); +RedisStoreBolt storeBolt = new RedisStoreBolt(poolConfig, storeMapper); +``` + +### For non-simple Bolt + +If your scenario doesn't fit ```RedisStoreBolt``` and ```RedisLookupBolt```, storm-redis also provides ```AbstractRedisBolt``` to let you extend and apply your business logic. ```java
