This is an automated email from the ASF dual-hosted git repository. donalevans pushed a commit to branch feature/GEODE-9814-redis-example in repository https://gitbox.apache.org/repos/asf/geode-examples.git
commit 868519f824731520b74ad40899d4b866bb669c3f Author: Donal Evans <[email protected]> AuthorDate: Fri Dec 3 15:22:12 2021 -0800 GEODE-9814: Add geode-for-redis example Authored-by: Donal Evans <[email protected]> --- .gitignore | 4 +- README.md | 4 +- build.gradle | 2 + geodeForRedis/README.md | 86 ++++++++++++++++++++++ geodeForRedis/build.gradle | 20 +++++ geodeForRedis/scripts/start.gfsh | 19 +++++ geodeForRedis/scripts/stop.gfsh | 17 +++++ .../geode_examples/geodeForRedis/Example.java | 70 ++++++++++++++++++ gradle/rat.gradle | 2 + settings.gradle | 1 + 10 files changed, 221 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index f34a209..b6156c8 100644 --- a/.gitignore +++ b/.gitignore @@ -18,8 +18,8 @@ locator locator1 l1 server -server1 -server2 +*server1 +*server2 *.iml *.ipr diff --git a/README.md b/README.md index bc97af5..bca68a8 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ an integration with other projects. * SpringBoot Application * HTTP Session replication -* Redis +* [Redis](geodeForRedis/README.md) * Memcached * Spark Connector @@ -118,7 +118,7 @@ an integration with other projects. Follow this approach to add a new example: -* Create a subdirectory with a descriptive name like `cache-writer` +* Create a subdirectory with a descriptive name like `cacheWriter` * Create a `README.md` file in the example subproject to walk the user through the tutorial * Create a Java class with a main method in the `org.apache.geode_examples.$name.Example` class * Create a cluster initialization script in `scripts/start.gfsh` diff --git a/build.gradle b/build.gradle index 77acecf..4feaa5d 100644 --- a/build.gradle +++ b/build.gradle @@ -102,6 +102,8 @@ subprojects { delete 'server-ln-2' delete 'server-ny-1' delete 'server-ny-2' + delete 'redisServer1' + delete 'redisServer2' } } clean.finalizedBy cleanServer diff --git a/geodeForRedis/README.md b/geodeForRedis/README.md new file mode 100644 index 0000000..e2fdc4b --- /dev/null +++ b/geodeForRedis/README.md @@ -0,0 +1,86 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +--> + +# Geode for Redis Example using Jedis Client + +This example demonstrates simple operations on a Geode for Redis cluster using the Jedis Client. + +In this example, two servers are started with geode-for-redis enabled, some data is added to the cluster. + +This example assumes that Java and Geode are installed. + +## Set up the cluster +1. Set directory ```geode-examples/geodeForRedis``` to be the +current working directory. +Each step in this example specifies paths relative to that directory. + +2. Build the example + + $ ../gradlew build + +3. Run a script that starts a locator and two servers with geode-for-redis enabled. + + $ ../gradlew start + +## Run the example program +Run the example to populate the cluster with some initial leaderboard data, increment +the scores associated with each member, then remove the lowest scoring member, printing the +contents of the leaderboard at each step. + + $ ../gradlew run + +## Optionally use the redis-cli to issue additional commands + +1. If you do not have `redis-cli` installed, follow the "Installing Redis" instructions +at: https://redis.io/topics/quickstart + +2. Start the `redis-cli` in cluster mode, specifying the port used to start the geode-for-redis +server: + + $ redis-cli -c -p 6379 + +3. Experiment with other commands: + + $ set stringKey aString + OK + + $ get stringKey + "aString" + + $ append stringKey WithAppendedData + (integer) 23 + + $ get stringKey + "aStringWithAppendedData" + + $ del stringKey + (integer) 1 + + $ get stringKey + (nil) + +Other supported commands can be listed using the `COMMAND` command. + +## Shut down the cluster and (optionally) clean up the directory +1. Shut down the cluster: + + $ ../gradlew stop + +2. If desired, clean up the generated directories containing +logs: + + $ ../gradlew cleanServer diff --git a/geodeForRedis/build.gradle b/geodeForRedis/build.gradle new file mode 100644 index 0000000..567f632 --- /dev/null +++ b/geodeForRedis/build.gradle @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +dependencies { + compile "redis.clients:jedis" +} diff --git a/geodeForRedis/scripts/start.gfsh b/geodeForRedis/scripts/start.gfsh new file mode 100644 index 0000000..53b976b --- /dev/null +++ b/geodeForRedis/scripts/start.gfsh @@ -0,0 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +start locator --name=locator --bind-address=localhost + +start server --name=redisServer1 --locators=localhost[10334] --server-port=0 --J=-Dgemfire.geode-for-redis-enabled=true --J=-Dgemfire.geode-for-redis-port=6379 --J=-Dgemfire.geode-for-redis-bind-address=127.0.0.1 +start server --name=redisServer2 --locators=localhost[10334] --server-port=0 --J=-Dgemfire.geode-for-redis-enabled=true --J=-Dgemfire.geode-for-redis-port=6380 --J=-Dgemfire.geode-for-redis-bind-address=127.0.0.1 diff --git a/geodeForRedis/scripts/stop.gfsh b/geodeForRedis/scripts/stop.gfsh new file mode 100644 index 0000000..9755688 --- /dev/null +++ b/geodeForRedis/scripts/stop.gfsh @@ -0,0 +1,17 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +connect --locator=127.0.0.1[10334] +shutdown --include-locators=true diff --git a/geodeForRedis/src/main/java/org/apache/geode_examples/geodeForRedis/Example.java b/geodeForRedis/src/main/java/org/apache/geode_examples/geodeForRedis/Example.java new file mode 100644 index 0000000..1648271 --- /dev/null +++ b/geodeForRedis/src/main/java/org/apache/geode_examples/geodeForRedis/Example.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode_examples.geodeForRedis; + +import java.text.DecimalFormat; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.JedisCluster; + +public class Example { + public static final String SORTED_SET_KEY = "leaderboard"; + + public static void main(String[] args) { + JedisCluster jedis = new JedisCluster(new HostAndPort("127.0.0.1", 6379)); + + populateSortedSet(jedis); + + System.out.println("Initial leaderboard with key '" + SORTED_SET_KEY + "': " + + jedis.zrangeWithScores(SORTED_SET_KEY, 0, Integer.MAX_VALUE)); + System.out.println("Updating scores..."); + + modifyScores(jedis); + + System.out.println("Updated leaderboard with key '" + SORTED_SET_KEY + "': " + + jedis.zrevrangeWithScores(SORTED_SET_KEY, 0, Integer.MAX_VALUE)); + + System.out.println("Removing lowest scoring member..."); + + jedis.zpopmin(SORTED_SET_KEY); + + System.out.println("Updated leaderboard with key '" + SORTED_SET_KEY + "': " + + jedis.zrevrangeWithScores(SORTED_SET_KEY, 0, Integer.MAX_VALUE)); + + } + + private static void populateSortedSet(JedisCluster jedis) { + Map<String, Double> memberScoreMap = new HashMap<>(); + memberScoreMap.put("John", 0.0); + memberScoreMap.put("Maria", 0.0); + memberScoreMap.put("Jose", 0.0); + memberScoreMap.put("Wei", 0.0); + memberScoreMap.put("Ahmed", 0.0); + + jedis.zadd(SORTED_SET_KEY, memberScoreMap); + } + + private static void modifyScores(JedisCluster jedis) { + Random random = new Random(); + jedis.zincrby(SORTED_SET_KEY, random.nextInt(10_000) / 100.0, "John"); + jedis.zincrby(SORTED_SET_KEY, random.nextInt(10_000) / 100.0, "Maria"); + jedis.zincrby(SORTED_SET_KEY, random.nextInt(10_000) / 100.0, "Jose"); + jedis.zincrby(SORTED_SET_KEY, random.nextInt(10_000) / 100.0, "Wei"); + jedis.zincrby(SORTED_SET_KEY, random.nextInt(10_000) / 100.0, "Ahmed"); + } +} diff --git a/gradle/rat.gradle b/gradle/rat.gradle index 2aedfe0..0485ff9 100644 --- a/gradle/rat.gradle +++ b/gradle/rat.gradle @@ -75,6 +75,8 @@ rat { '**/locator-ny/**', '**/server-ny-1/**', '**/server-ny-2/**', + '**/redisServer1/**', + '**/redisServer2/**', '**/META-INF/**' ] diff --git a/settings.gradle b/settings.gradle index a2165fa..fc595ce 100644 --- a/settings.gradle +++ b/settings.gradle @@ -43,6 +43,7 @@ include 'colocation' include 'micrometerMetrics' include 'compression' include 'rest' +include 'geodeForRedis' // Logic for defining a custom Geode clone for integration with this project // Define `-PgeodeCompositeDirectory` to your geode root, default `../geode`
