• jedis连接redis,测试操作redis常用的数据类型 String List Set Hash Zset


    编码测试

    1) 连接数据库

    2) 操作命令

    3) 断开连接

    常用的api

    String List Set Hash Zset

    package com.zyq.key;

    import redis.clients.jedis.Jedis;

    import java.util.Set;

    /**
     * Created by yyy on 2020/9/15.
     */
    public class TestKey {
        public static void main(String[] args) {
            //连接redis
            Jedis jedis = new Jedis("127.0.0.1", 6379);

            System.out.println("清空当前数据库:"+jedis.flushDB());
            System.out.println("判断某个键是否存在:"+jedis.exists("username"));
            System.out.println("新增username--yyy的键值对:"+jedis.set("username","yyy"));
            System.out.println("新增password--111的键值对:"+jedis.set("password","111"));
            System.out.println("新增hobby--running的键值对:"+jedis.set("hobby","running"));
            System.out.println("返回当前数据库中key的数目:"+jedis.dbSize());
            System.out.println("现在系统中所有的键如下:");
            Set<String> keys = jedis.keys("*");
            System.out.println(keys);

            System.out.println("删除键password:"+jedis.del("password"));
            System.out.println("删除键password是否存在:"+jedis.exists("password"));
            System.out.println("查看键username的数据类型:"+jedis.type("username"));
            System.out.println("随机返回key空间的一个:"+jedis.randomKey());

            System.out.println("重命名key"+jedis.rename("username","name"));
            System.out.println("取出修改后的name的值:"+jedis.get("name"));
            System.out.println("按索引查询:"+jedis.select(0));
            System.out.println("清空当前数据库:"+jedis.flushDB());
            System.out.println("返回当前数据库中key的数目:"+jedis.dbSize());
            System.out.println("删除所有数据库中的key"+jedis.flushAll());


        }
    }

     

    运行结果:

    清空当前数据库:OK

    判断某个键是否存在:false

    新增username--yyy的键值对:OK

    新增password--111的键值对:OK

    新增hobby--running的键值对:OK

    返回当前数据库中key的数目:3

    现在系统中所有的键如下:

    [password, hobby, username]

    删除键password:1

    删除键password是否存在:false

    查看键username的数据类型:string

    随机返回key空间的一个:username

    重命名keyOK

    取出修改后的name的值:yyy

    按索引查询:OK

    清空当前数据库:OK

    返回当前数据库中key的数目:0

    删除所有数据库中的keyOK

    String类型练习

    package com.zyq.practise;

    import redis.clients.jedis.Jedis;

    import java.util.concurrent.TimeUnit;

    /**
     * Created by yyy on 2020/9/16.
     */
    public class TestString {
        public static void main(String[] args) {
            Jedis jedis = new Jedis("127.0.0.1", 6379);
            jedis.flushDB();
            System.out.println("===========================");
            System.out.println("添加key1--value1键值对:" + jedis.set("key1", "value1"));
            System.out.println("添加key2--value2键值对:" + jedis.set("key2", "value2"));
            System.out.println("添加key3--value3键值对:" + jedis.set("key3", "value3"));
            System.out.println("删除键key2" + jedis.del("key2"));
            System.out.println("获取键key2:" + jedis.get("key2"));
            System.out.println("获取键key1:" + jedis.get("key1"));
            System.out.println("修改key1的值:" + jedis.set("key1", "value1Changed"));
            System.out.println("获取键key1:" + jedis.get("key1"));
            System.out.println("key3后面加入值:" + jedis.append("key3", "end"));
            System.out.println("获取key3的值:" + jedis.get("key3"));
            System.out.println("增加多个键值对:" + jedis.mset("key01", "value01", "key02", "value02", "key03", "value03"));
            System.out.println("批量获取多个键值对:" + jedis.mget("key01","key02", "key03"));
            System.out.println("批量获取多个键值对:" + jedis.mget("key01","key02", "key03","key04"));
            System.out.println("删除多个键值对:"+jedis.del("key01","key02"));
            System.out.println("获取多个键值对:"+jedis.mget("key01","key02","key03"));

            System.out.println("=============清空当前数据库===============");
            System.out.println("新增键值对防止覆盖原先值");
            System.out.println("添加key1--value1键值对:" + jedis.set("key1", "value1"));
            System.out.println("添加key2--value2键值对:" + jedis.set("key2", "value2"));
            System.out.println("添加key2--value2new键值对:" + jedis.set("key2", "value2new"));
            System.out.println("获取key1的值:"+jedis.get("key1"));
            System.out.println("获取key2的值:"+jedis.get("key2"));

            System.out.println("===========新增键值对并设置有效时间==============");
            System.out.println("添加key3--value3键值对,并设置过期时间是2秒:"+jedis.setex("key3",2,"value3"));
            System.out.println("获取key3的值:"+jedis.get("key3"));
            try {
                TimeUnit.SECONDS.sleep(3);
            }catch (InterruptedException ex){
                ex.printStackTrace();
            }
            System.out.println("获取key3的值:"+jedis.get("key3"));

            System.out.println("======获取原值,更新为新值========");
            System.out.println("获取原值,并设置新值:"+jedis.getSet("key2","key2getset"));
            System.out.println("获取key2的值:"+jedis.get("key2"));
            System.out.println("获取key2的值的截取字符串:"+jedis.getrange("key2",2,4));


        }

    }

    运行结果:

    ===========================

    添加key1--value1键值对:OK

    添加key2--value2键值对:OK

    添加key3--value3键值对:OK

    删除键key21

    获取键key2:null

    获取键key1:value1

    修改key1的值:OK

    获取键key1:value1Changed

    key3后面加入值:9

    获取key3的值:value3end

    增加多个键值对:OK

    批量获取多个键值对:[value01, value02, value03]

    批量获取多个键值对:[value01, value02, value03, null]

    删除多个键值对:2

    获取多个键值对:[null, null, value03]

    =============清空当前数据库===============

    新增键值对防止覆盖原先值

    添加key1--value1键值对:OK

    添加key2--value2键值对:OK

    添加key2--value2new键值对:OK

    获取key1的值:value1

    获取key2的值:value2new

    ===========新增键值对并设置有效时间==============

    添加key3--value3键值对,并设置过期时间是2秒:OK

    获取key3的值:value3

    获取key3的值:null

    ======获取原值,更新为新值========

    获取原值,并设置新值:value2new

    获取key2的值:key2getset

    获取key2的值的截取字符串:y2g

    List集合:

    package com.zyq.practise;

    import redis.clients.jedis.Jedis;

    /**
     * Created by ZhaoYongqiang on 2020/9/16.
     */
    public class TestList {
        public static void main(String[] args) {
            Jedis jedis = new Jedis("127.0.0.1", 6379);
            jedis.flushDB();
            System.out.println("添加一个list");
            jedis.lpush("collections","ArrayList","Vector","Stack","HashMap","WeakHashMap","LinkedHashMap");
            jedis.lpush("collections","HashSet");
            jedis.lpush("collections","TreeSet");
            jedis.lpush("collections","TreeMap");
            System.out.println("collections集合的内容:"+jedis.lrange("collections",0,-1));
            System.out.println("collections区间0-3区间之外的元素:"+jedis.lrange("collections",0,3));

            System.out.println("============================");
            //删除列表中指定的值,第二个参数为删除的个数(有重复时),后add进去的值先被删除,类似于出栈
            System.out.println("删除指定元素个数:"+jedis.lrem("collections",2,"HashMap"));
            System.out.println("collections集合中内容:"+jedis.lrange("collections",0,-1));
            System.out.println("删除下标0-3区间之外的元素:"+jedis.ltrim("collections",0,3));
            System.out.println("collections集合中内容:"+jedis.lrange("collections",0,-1));
            System.out.println("collections列表出栈(左端):"+jedis.lpop("collection"));
            System.out.println("collections集合中内容:"+jedis.lrange("collections",0,-1));
            System.out.println("collections添加元素,从列表右端:"+jedis.rpush("collections","EnumMap"));
            System.out.println("collections集合中内容:"+jedis.lrange("collections",0,-1));
            System.out.println("collections列表出栈(右端):"+jedis.rpop("collection"));
            System.out.println("collections集合中内容:"+jedis.lrange("collections",0,-1));
            System.out.println("修改collections指定下标元素的内容:"+jedis.lset("collections",1,"LinkedArrayList"));
            System.out.println("collections集合中内容:"+jedis.lrange("collections",0,-1));
            System.out.println("================================");
            System.out.println("collection的长度:"+jedis.llen("collections"));
            System.out.println("获取collections下标为2的元素:"+jedis.lindex("collections",2));
            System.out.println("================================");
            System.out.println("添加一个集合元素:"+jedis.lpush("sortedList","3","6","2","0","7","4"));
            System.out.println("sortedLIst排序前:"+jedis.lrange("sortedList",0,-1));
            System.out.println(jedis.sort("sortedList"));
            System.out.println("sortedList排序后:"+jedis.lrange("sortedList",0,-1));


        }
    }

     运行结果:

    添加一个list

    collections集合的内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, HashMap, Stack, Vector, ArrayList]

    collections区间0-3区间之外的元素:[TreeMap, TreeSet, HashSet, LinkedHashMap]

    ============================

    删除指定元素个数:1

    collections集合中内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, WeakHashMap, Stack, Vector, ArrayList]

    删除下标0-3区间之外的元素:OK

    collections集合中内容:[TreeMap, TreeSet, HashSet, LinkedHashMap]

    collections列表出栈(左端):null

    collections集合中内容:[TreeMap, TreeSet, HashSet, LinkedHashMap]

    collections添加元素,从列表右端:5

    collections集合中内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, EnumMap]

    collections列表出栈(右端):null

    collections集合中内容:[TreeMap, TreeSet, HashSet, LinkedHashMap, EnumMap]

    修改collections指定下标元素的内容:OK

    collections集合中内容:[TreeMap, LinkedArrayList, HashSet, LinkedHashMap, EnumMap]

    ================================

    collection的长度:5

    获取collections下标为2的元素:HashSet

    ================================

    添加一个集合元素:6

    sortedLIst排序前:[4, 7, 0, 2, 6, 3]

    [0, 2, 3, 4, 6, 7]

    sortedList排序后:[4, 7, 0, 2, 6, 3]

    Set集合:

    package com.zyq.practise;

    import redis.clients.jedis.Jedis;

    /**
     * Created by yyy on 2020/9/16.
     */
    public class TestSet {
        public static void main(String[] args) {
            Jedis jedis = new Jedis("127.0.0.1", 6379);
            jedis.flushDB();
            System.out.println("========向集合中添加元素(不重复)=================");
            System.out.println(jedis.sadd("eleSet","e1","e2","e4","e3","e0","e8","e7","e5"));
            System.out.println(jedis.sadd("eleSet","e6"));
            System.out.println(jedis.sadd("eleSet","e6"));
            System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
            System.out.println("删除一个元素e0:"+jedis.srem("eleSet","e0"));
            System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
            System.out.println("删除两个元素e7e6:"+jedis.srem("eleSet","e6","e7"));
            System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
            System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet"));
            System.out.println("随机的移除集合中的一个元素:"+jedis.spop("eleSet"));
            System.out.println("eleSet的所有元素为:"+jedis.smembers("eleSet"));
            System.out.println("eleSet中包含元素的个数:"+jedis.scard("eleSet"));
            System.out.println("eleSet中是否存在e1元素:"+jedis.sismember("eleSet","e1"));
            System.out.println("eleSet中是否存在e3元素:"+jedis.sismember("eleSet","e3"));
            System.out.println("================================");
            System.out.println(jedis.sadd("eleSet1","e1","e2","e4","e3","e0","e8","e7","e5"));
            System.out.println(jedis.sadd("eleSet2","e1","e2","e4","e3","e0","e8"));
            System.out.println("eleSet1中删除e1并存入到eleSet3中:"+jedis.smove("eleSet1","eleSet3","e1"));
            System.out.println("eleSet1中删除e2并存入到eleSet3:"+jedis.smove("eleSet1","eleSet3","e2"));
            System.out.println("eleSet1的所有元素为:"+jedis.smembers("eleSet1"));
            System.out.println("eleSet3的所有元素为:"+jedis.smembers("eleSet3"));
            System.out.println("===============集合运算==============");
            System.out.println("eleSet1中的元素:"+jedis.smembers("eleSet1"));
            System.out.println("eleSet2中元素:"+jedis.smembers("eleSet2"));
            System.out.println("eleSet1eleSet2的交集:"+jedis.sinter("eleSet1","eleSet2"));
            System.out.println("eleSet1eleSet2的并集:"+jedis.sunion("eleSet1","eleSet2"));
            System.out.println("eleSet1eleSet2的交集:"+jedis.sdiff("eleSet1","eleSet2"));
            System.out.println(jedis.sinterstore("eleSet4","eleSet1","eleSet2"));//将集合1和集合2的交集保存到集合4
            System.out.println("eleSet4中的元素:"+jedis.smembers("eleSet4"));



        }
    }

    ========向集合中添加元素(不重复)=================

    8

    1

    0

    eleSet的所有元素为:[e0, e1, e4, e8, e2, e3, e5, e6, e7]

    删除一个元素e0:1

    eleSet的所有元素为:[e1, e4, e8, e2, e3, e5, e6, e7]

    删除两个元素e7e6:2

    eleSet的所有元素为:[e2, e3, e5, e1, e8, e4]

    随机的移除集合中的一个元素:e3

    随机的移除集合中的一个元素:e1

    eleSet的所有元素为:[e5, e8, e4, e2]

    eleSet中包含元素的个数:4

    eleSet中是否存在e1元素:false

    eleSet中是否存在e3元素:false

    ================================

    8

    6

    eleSet1中删除e1并存入到eleSet3中:1

    eleSet1中删除e2并存入到eleSet3:1

    eleSet1的所有元素为:[e5, e7, e0, e4, e8, e3]

    eleSet3的所有元素为:[e1, e2]

    ===============集合运算==============

    eleSet1中的元素:[e5, e7, e0, e4, e8, e3]

    eleSet2中元素:[e4, e3, e2, e0, e1, e8]

    eleSet1eleSet2的交集:[e4, e3, e0, e8]

    eleSet1eleSet2的并集:[e5, e7, e0, e1, e4, e8, e2, e3]

    eleSet1eleSet2的交集:[e7, e5]

    4

    eleSet4中的元素:[e0, e8, e4, e3]

    Hash数据类型的练习:

    package com.zyq.practise;

    import redis.clients.jedis.Jedis;

    import java.util.HashMap;

    /**
     * Created by yyyyy on 2020/9/16.
     */
    public class TestHash {
        public static void main(String[] args) {
            Jedis jedis = new Jedis("127.0.0.1", 6379);
            jedis.flushDB();
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("key1","value1");
            map.put("key2","value2");
            map.put("key3","value3");
            map.put("key4","value4");
            //添加名称为hashhash元素
            jedis.hmset("hash",map);
            //像名称为hashhash中添加keykey5valuevalue5的元素
            jedis.hset("hash","key5","value5");
            System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
            System.out.println("散列hash的所有键为:"+jedis.hkeys("hash"));
            System.out.println("散列hash的所有值为:"+jedis.hvals("hash"));
            System.out.println("key6保存的值加上一个整数,如果key6不存在则添加key6"+jedis.hincrBy("hash","key6",6));
            System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
            System.out.println("删除一个或者多个键值对:"+jedis.hdel("hash","key2"));
            System.out.println("散列hash的所有键值对为:"+jedis.hgetAll("hash"));
            System.out.println("散列hash中键值对的个数:"+jedis.hlen("hash"));
            System.out.println("判断hash中是否存在key2:"+jedis.hexists("hash","key2"));
            System.out.println("判断hash中是否存在key3:"+jedis.hexists("hash","key3"));
            System.out.println("获取hash中的值:"+jedis.hmget("hash","key3"));
            System.out.println("获取hash中的值:"+jedis.hmget("hash","key3","key4"));

        }
    }

    运行结果:

    散列hash的所有键值对为:{key1=value1, key2=value2, key5=value5, key3=value3, key4=value4}

    散列hash的所有键为:[key1, key2, key5, key3, key4]

    散列hash的所有值为:[value3, value2, value4, value1, value5]

    key6保存的值加上一个整数,如果key6不存在则添加key66

    散列hash的所有键值对为:{key1=value1, key2=value2, key5=value5, key6=6, key3=value3, key4=value4}

    删除一个或者多个键值对:1

    散列hash的所有键值对为:{key1=value1, key5=value5, key6=6, key3=value3, key4=value4}

    散列hash中键值对的个数:5

    判断hash中是否存在key2:false

    判断hash中是否存在key3:true

    获取hash中的值:[value3]

    获取hash中的值:[value3, value4]

    测试连接:

    package com.zyq.practise;

    import redis.clients.jedis.Jedis;

    /**
     * Created by yyyy on 2025/9/16.
     */
    public class TestPassWord {
        public static void main(String[] args) {
            Jedis jedis = new Jedis("127.0.0.1", 6379);
            //如果redis有设置密码,则加上,没有这行代码去掉
    //        jedis.auth("79797979");
            jedis.connect();
            jedis.disconnect();
            System.out.println("=========="+jedis.flushDB());

        }
    }

    运行结果:

    ==========OK

  • 相关阅读:
    vue慕课网音乐项目手记:9-封装一个公用的scroll组件
    vue慕课网音乐项目手记:50-搜索列表的点击删除、删除全部的交互事件
    vue慕课网音乐项目手记:48-搜索历史数据的处理
    vue慕课网音乐项目手记:6-手写滚动轮播图(中)
    vue慕课网音乐项目手记:5-手写滚动轮播图(上)
    vue慕课网音乐项目手记:30-音乐环形进度条的实现
    vue慕课网音乐项目手记:45-搜索页面跳转歌手页面
    基于Vue2.0的音乐播放器(2)——歌手模块
    linux学习笔记-(1)-安装
    linux学习笔记-前篇
  • 原文地址:https://www.cnblogs.com/dongyaotou/p/13763703.html
Copyright © 2020-2023  润新知