jedis介绍

  jedis是redis的java版本的客户端实现。下面演示jedis的相关操作:
  
  首先在eclipse新建动态web工程:
  
  image_1b6tlp75ltr41kcd1vurkheah89.png-41.6kB
  
  将jedis所需的jar包导入到工程中:
  
  image_1b6toosmh8iuv9h107gach19e6m.png-8.7kB
  
  编写Java代码测试连通性:

import redis.clients.jedis.Jedis;

public class TestPing {

    public static void main(String[] args) {
      //192.168.202.131是我虚拟机的IP地址,6379是redis服务的端口号,请自行配置
        Jedis jedis = new Jedis("192.168.202.131", 6379);
        System.out.println(jedis.ping());

    }

}

  运行程序,会报错:
  Exception in thread “main” redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
  
  我们需要更改redis的配置文件,将bind 127.0.0.1注释掉,使任何IP都可以访问redis服务:
  
  image_1b6tp9atqk1c13l21at9pi964o13.png-108.2kB
  
  重启redis服务,再运行程序,发现继续报错:
  
  Exception in thread “main” redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command ‘CONFIG SET protected-mode no’ from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to ‘no’, and then restarting the server. 3) If you started the server manually just for testing, restart it with the ‘–protected-mode no’ option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
  
  大意是redis在保护模式下,拒绝在没有绑定IP地址或者没有授权密码的情况下进行访问。我们需要继续更改redis配置文件,将protected-mode yes改为protected-mode no:
  
  image_1b6ttt2ro11oenofng81ccn1clv1g.png-97.5kB
  
  重启redis服务,重新运行程序,会输出“PONG”,证明连接成功:
  
  image_1b6ttupvq8hdeu0v2k1enl1ev81t.png-14.8kB
  
  下面根据数据类型来测试jedis的API:

    //String类型
  @Test
    public void testString() {
        Jedis jedis = new Jedis("192.168.202.131", 6379);

        //清空数据库
        jedis.flushDB();

        String k1 = "k1";
        String v1 = "v1";
        jedis.set("k1", v1);
        System.out.println(jedis.get(k1));

        //是否存在
        System.out.println(jedis.exists(k1));

        //类型
        System.out.println(jedis.type(k1));

        //拼接
        jedis.append(k1, "v1v1v1");
        System.out.println(jedis.get(k1));

        //设置多个数据
        jedis.mset("k2","v2","k3","3");
        System.out.println(jedis.keys("*"));

        //删除
        jedis.del("k2");
        System.out.println(jedis.keys("*"));

        //自增
        jedis.incr("k3");
        System.out.println(jedis.get("k3"));
    }

  输出:
  
  image_1b6u762sg11b31bn9i9t17621gff9.png-19.7kB
  

    //List类型
    @Test
    public void testList(){
        Jedis jedis = new Jedis("192.168.202.131", 6379);

        //清空数据库
        jedis.flushDB();

        //从列表头部插入
        jedis.lpush("l1", "a","b","c");
        //从列表尾部插入
        jedis.rpush("l1", "d","e");
        //返回列表中所有值
        System.out.println(jedis.lrange("l1", 0, -1));
        //返回列表长度
        System.out.println(jedis.llen("l1"));
        //从列表头部移除一个元素并返回
        System.out.println(jedis.lpop("l1"));
    }

  输出:
  
  image_1b6u87tfdc1g1lvhptjcv1l9lm.png-13.7kB
  

       //Hash类型
        @Test
        public void testHash(){
            Jedis jedis = new Jedis("192.168.202.131", 6379);

            //清空数据库
            jedis.flushDB();

            Map<String,String> map = new HashMap<>();
            map.put("name", "Alan");
            map.put("age", "22");
            jedis.hmset("user",map);
            jedis.hset("user", "gender", "male");

            //获取单个数据
            System.out.println(jedis.hget("user", "age"));

            //获取多个数据
            System.out.println(jedis.hmget("user", "name","gender"));

            //获取所有数据
            System.out.println(jedis.hgetAll("user"));

            //获取所有的键
            System.out.println(jedis.hkeys("user"));

            //获取所有的值
            System.out.println(jedis.hvals("user"));
        }

  输出:
  
  image_1b6u89aa51egu1dakmtqtuebhd13.png-22.5kB
  

    // Set类型
    @Test
    public void testSet() {
        Jedis jedis = new Jedis("192.168.202.131", 6379);

        // 清空数据库
        jedis.flushDB();

        //添加一个或多个数据
        jedis.sadd("s1", "a","b","c");
        jedis.sadd("s1", "d");

        //返回集合中所有元素
        System.out.println(jedis.smembers("s1"));

        //成员个数
        System.out.println(jedis.scard("s1"));

        //求交集,并集,差集
        jedis.sadd("s2", "c","d","e","f");

        System.out.println(jedis.sinter("s1","s2"));

        System.out.println(jedis.sunion("s1","s2"));

        System.out.println(jedis.sdiff("s1","s2"));

        //判断是否是成员
        System.out.println(jedis.sismember("s1", "c"));

        //移除一个或者多个元素
        jedis.srem("s1", "b","c");
        System.out.println(jedis.smembers("s1"));
    }

  输出:

  image_1b6u8sf9u1hk2dc21g3o1m5l5p71g.png-12.8kB
  

    // Zset类型
        @Test
        public void testZset() {
            Jedis jedis = new Jedis("192.168.202.131", 6379);

            // 清空数据库
            jedis.flushDB();

            //向有序集合中添加多个成员
            Map<Double,String> map = new HashMap<>();
            map.put(60.0, "Alan");
            map.put(70.0, "Jack");
            map.put(75.0, "Tom");

            jedis.zadd("rank", map);

            //返回所有成员
            System.out.println(jedis.zrange("rank", 0, -1));

            //带有分数地返回所有成员,字符串由ASCII码表示
            System.out.println(jedis.zrangeWithScores("rank", 0, -1));

            //增加成员分数
            jedis.zincrby("rank", 0.5, "Alan");
            System.out.println(jedis.zscore("rank", "Alan"));

            //返回分数在60到70之间的成员
            System.out.println(jedis.zrangeByScore("rank", 60, 70));


        }

  输出:
  
  image_1b6u9nmnpk56b6dp26hj611en1t.png-29.1kB
  

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值