JedisCluster操做redis集羣demo

package com.chenk;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.Tuple;

public class JedisClusterDemo {
    JedisCluster jedis = null;

    @Before
    public void conn() {
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        nodes.add(new HostAndPort("192.168.178.161", 6379));
        nodes.add(new HostAndPort("192.168.178.161", 6380));
        nodes.add(new HostAndPort("192.168.178.161", 6381));
        nodes.add(new HostAndPort("192.168.178.161", 6382));
        nodes.add(new HostAndPort("192.168.178.161", 6383));
        nodes.add(new HostAndPort("192.168.178.161", 6384));
        jedis = new JedisCluster(nodes);
    }

    /**
     * 操做String
     */
    @Test
    public void operStr() {

        // 添加一條數據
        jedis.set("username", "jonychen");
        // 獲取一條數據
        String username = jedis.get("username");
        System.out.println("用戶名:" + username);

        // 刪除
        jedis.del("username");
    }

    /**
     * 操做hash
     */
    @Test
    public void operHash() {
        // 添加一條
        jedis.hset("goodsInfo", "goodsName", "403-超級手機");
        // 獲取一條
        String goodsName = jedis.hget("goodsInfo", "goodsName");
        System.out.println("商品名稱" + goodsName);

        Map<String, String> hash = new HashMap<String, String>();
        hash.put("orderSn", "20171226122301");
        hash.put("orderStatus", "提交預訂單");

        // 添加多條
        jedis.hmset("orderInfo", hash);
        System.out.println("---------------");
        // 獲取多條
        List<String> strList = jedis.hmget("orderInfo", "orderSn", "orderStatus");
        for (String string : strList) {
            System.out.println(string);
        }
        System.out.println("---------------");
        // 獲取所有

        Map<String, String> orderInfoMap = jedis.hgetAll("orderInfo");
        for (Entry<String, String> entry : orderInfoMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }

        // 刪除
        jedis.hdel("orderInfo", "orderStatus");
    }

    /**
     * 操做list
     */
    @Test
    public void operList() {
        // 添加
        for (int i = 0; i < 10; i++) {
            jedis.lpush("animals", "dog" + i, "cat" + i, "fish" + i);
        }

        // 獲取
        String reString = jedis.rpop("animals");
        System.out.println(reString);
        // 分頁查詢 start:起始條數 end :結束條數
        List<String> strList = jedis.lrange("animals", 0, 9);
        for (String string : strList) {
            System.out.println(string);
        }
        System.out.println("----------------");
        // 獲取總條數
        Long total = jedis.llen("animals");
        System.out.println("總條數" + total);
        // 刪除
        jedis.lrem("animals", 1, "dog0");
    }

    /**
     * 操做set
     */
    @Test
    public void operSet() {
        // 添加
        jedis.sadd("members", "xiaoming", "xiaohua", "xiaohui", "xiaochen");
        // 獲取
        Set<String> members = jedis.smembers("members");
        for (String string : members) {
            System.out.println(string);
        }
        // 刪除
        jedis.srem("members", "xiaohui");
    }

    /**
     * 操做sorted set-自動排序
     */
    @Test
    public void operSortedSet() {
        Map<String, Double> scoreMembers = new HashMap<String, Double>();
        scoreMembers.put("小明", 89D);
        scoreMembers.put("xiaopeng", 93D);
        scoreMembers.put("小胡", 88D);
        // 添加
        jedis.zadd("score", scoreMembers);
        // 獲取start :起始條數 end:結束條數 按分數升序查詢
        Set<String> strSet = jedis.zrange("score", 0, 1);
        for (String string : strSet) {
            System.out.println(string);
        }
        System.out.println("-------------");
        // 降序查詢,並獲取成員的分數
        Set<Tuple> tupleSet = jedis.zrevrangeWithScores("score", 0, 1);
        for (Tuple tuple : tupleSet) {
            // 成員
            String element = tuple.getElement();
            Double score = tuple.getScore();
            System.out.println(element + ":" + score);
        }
        System.out.println("-----------------");
        // 獲取總條數
        Long total = jedis.zcard("score");
        System.out.println("總條數:" + total);
        // 刪除
        jedis.zrem("score", "xiaopeng");
    }
}

 與單節點redis不一樣點

1     JedisCluster不支持ping操做

2     JedisCluster不能同時操做多個key(mset、mget不能正常調用)

3     JedisCluster不支持flushAll、flushDb操做

相關文章
相關標籤/搜索