Rediscluster Java and Python client API usage examples

Source: Internet
Author: User
Tags redis cluster

The environment here still uses the final cluster configuration environment in [Redis Cluster installation and configuration], as follows:

127.0.0.1:7000> cluster nodes8868592d98d84b7cf5752cc0b97af4ac807d1a12 127.0.0.1:7007  slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410882113063 8  connectedf5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1:7002 slave  bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410882111659 8  connected82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1:7001 master - 0  1410882111158 2 connected 6461-1092261dfb1055760d5dcf6519e35435d60dc5b207940  127.0.0.1:7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410882112662 5  connected6d1ebedad33bb31ffbaa99bad095eef4a5920857 127.0.0.1:7006 master - 0  1410882111158 0 connectedbfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1:7005 master  - 0 1410882111659 8 connected  11923-1638335e0f6fdadbf81a00a1d6d1843698613e653867b 127.0.0.1:7003 slave 123ed65d59ff22370f2f09546f410d31207789f6 0 1410882110657 7  Connected123ed65d59ff22370f2f09546f410d31207789f6 127.0.0.1:7000 myself,master - 0 0  7 connected 0-6460 10923-11922127.0.0.1:7000>

The following use Jedis and Redis-py-cluster to access the cluster, the operation is mainly: Set/get, pub/sub, and get the cluster information.

1,jedis

From now on, the best support for Redis and Rediscluster should be Jedis, the Open Source project (also the Java library recommended by the Redis website), for reference: Https://github.com/xetorthio/jedis

import redis.clients.jedis.*;import java.util.hashset;import java.util.iterator;import  java.util.map;import java.util.set;import java.util.concurrent.executorservice;import  java.util.concurrent.executors;/** * jedis  Test  cluster * *  @author   Steven */public class app {    public static void main (  String[] args )     {        set <HostAndPort> clusterNodes = new HashSet<HostAndPort> ();         //  Here you only need to list one node in the cluster         //  jediscluster  will go by himself  discovery  other cluster nodes          Clusternodes.add (New hostandport ("127.0.0.1",  7000));         jediscluster cluster = new&nbsP Jediscluster (clusternodes);        // set/get         cluster.set ("foo",  "Jedis test");         string value = cluster.get ("foo");         System.out.println ("foo = "  + value);         //  get cluster nodes        system.out.println ("-------  cluster nodes --------");        map<string,  Jedispool> nodes = cluster.getclusternodes ();         iterator<map.entry<string, jedispool>> iternodes = nodes.entryset (). Iterator ();        while  (Iternodes.hasnext ())  {       &nbSp;     map.entry<string, jedispool> entry = iternodes.next ();            jedis jedis =  Entry.getvalue (). GetResource ();             System.out.println ("============");             System.out.println (Entry.getkey ()  +  "\ n"  + jedis.info ());         }        // pub/sub         system.out.println ("------- pub/sub --------");         //  here randomly two  jedis  connections for  publish  and  subscribe  are taken         // Redis  does not support both  publisher  and   on the same connection Subscriber     &nbSp;  final jedispool jedispool = nodes.entryset (). iterator (). Next (). GetValue ();         //  use a separate thread  publish         executorservice newfixedthreadpool = executors.newfixedthreadpool (Ten);         newfixedthreadpool.submit (new runnable ()  {              @Override              public void run ()  {                 try {                     thread.sleep (; )                } catch  ( Interruptedexception e) &nbsP {                     e.printstacktrace ();                     return ;                 }                 jedispool.getresource (). Publish ("Channel-test",  "hello, redis  Cluster! ");             }         });        // subscribe -  here will always be blocked to receive   publish  message         jedispool.getresource (). Subscribe (new  Jedispubsub ()  {             @Override              public void onmessage (String channel,  string message)  {                 system.out.println ("onmessage - "  + channel +  ":"  +  message);            }              @Override              public void onpmessage (String pattern, string channel,  string message)  {                 system.out.println ("onpmessage - "  + pattern +  "|"  + channel +  ":"  + message);             }             @Override              public void onsubscribe (string channel, int  Subscribedchannels)  {                 system.out.println ("onsubscribe - "  + channel +  ":"  +  Subscribedchannels);            }              @Override              public void onunsubscribe (string channel, int  Subscribedchannels)  {                 system.out.println ("onunsubscribe - "  + channel +  ":"  +  Subscribedchannels);             }              @Override             public void  onpunsubscribe (string pattern, int subscribedchannels)  {                 system.out.println ("OnPUnsubscribe  -  " + pattern + ": " + subscribedchannels);             }              @Override             public void  onpsubscribe (string pattern, int subscribedchannels)  {                 system.out.println ("onPSubscribe -   " + pattern +  ":"  + subscribedchannels);             }        },  "Channel-test");     }}

The following record for key Foo is written to the 7005 node:

127.0.0.1:7000> get foo-> redirected to Slots [12182] located at 127.0.0.1:7005 "Jedis test" 127.0.0.1:7005> keys fo O1) "Foo" 127.0.0.1:7005>

2,redis-py-cluster

Python's Redis Cluster library uses Redis-py-cluster (Https://github.com/Grokzen/redis-py-cluster)

The following code is just a common example of how to use Redis-py-cluster

If you need to see a complete test of redis-py-cluster, you can refer directly to: https://github.com/Grokzen/redis-py-cluster/tree/master/tests

Import redisfrom rediscluster import rediscluster def main (): Startup_nodes = [{"Host": "127.0.0.1", "Port": "7000"}] r c = Rediscluster (Startup_nodes=startup_nodes, decode_responses=true) print '--------get/set---------' if not rc.set (    "foo", "Hello, Redis-py-cluster"): print ' Set foo to cluster failed!!! ' return-1 print ' foo =%s '% (Rc.get ("foo"),) if __name__ = = ' __main__ ': Main ()

This article is from the "Quiet lunatic" blog, please make sure to keep this source http://quietmadman.blog.51cto.com/3269500/1554544

Rediscluster Java and Python client API usage examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.