• redis学习之常用数据类型


    redis支持5种数据类型:string(字符串),list(列表),set(集合),sorted set(有序集合),hash(哈希)。下面是学习过程中的测试样例:
    1:string(字符串),string是redis最基本的数据类型,其相关命令主要用于管理redis字符串值

    2:list(列表),list是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)

    3:set(集合),Set是string类型的无序集合,集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)

    4:sorted set(有序集合),sorted set和set一样也是string类型元素的集合,且不允许重复的成员,不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序,sorted set的成员是唯一的,但分数(score)却可以重复。

    5:hash(哈希),hash是一个键值(key=>value)对集合,hash是一个string类型的key和value的映射表,hash特别适合用于存储对象。

    测试代码如下:

    import redis.clients.jedis.*;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Set;
    
    public class RedisTest {
        /**
         * Jedis就是封装了redis的一些命令操作的java客户端
         * */
        private static Jedis jedis = null;
        /**
         * Jedis实例不是线程安全的,所以为了避免一些线程安全问题,Jedis提供了线程池JedisPool
         * */
        private static JedisPool jedisPool = null;
    
        private static final String ip = "127.0.0.1";//Redis服务器IP
        private static final Integer port = 6379;//Redis的端口号
        private static final Integer timeout = 1000;//连接redis的等待时间
        private static final Integer maxTotal = 1024;//可连接实例的最大数目,默认值为8
        private static final Integer maxIdle = 200;//控制一个pool最多有多少个状态为idle的jedis实例,默认值为8
        private static final Integer maxWait = 10000;//等待可用连接的最大时间
        private static final boolean testOnBorrow = true;//在borrow一个jedis实例时,是否提前进行validate操作,如果为ture,则得到的jedis实例均是可用的
    
        /**
         * 初始化非切片池
         * */
        private static void initialPool(){
            try {
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxIdle(maxIdle);
                config.setMaxTotal(maxTotal);
                config.setMaxWaitMillis(maxWait);
                config.setTestOnBorrow(testOnBorrow);
                jedisPool = new JedisPool(config,ip,port,timeout);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        
        /**
         * string是redis最基本的数据类型,其相关命令主要用于管理redis字符串值
         * */
        private static void testString() {
            System.out.println("================testString====================");
            // 清空数据
            System.out.println("清空库中数据:"+jedis.flushDB());
            System.out.println("=============增=============");
            jedis.set("name","william");
            jedis.set("gender","male");
            jedis.set("age","18");
            System.out.println("=============查=============");
            System.out.println("添加的键值对:");
            System.out.println("(name,"+jedis.get("name")+")");
            System.out.println("(gender,"+jedis.get("gender")+")");
            System.out.println("(age,"+jedis.get("age")+")");
            
            System.out.println("=============删=============");
            System.out.println("删除gender键值对:"+jedis.del("gender"));
            System.out.println("获取gender键的value:"+jedis.get("gender"));
            
            System.out.println("=============改=============");
            System.out.println("改变name键的value:"+jedis.set("name","Allen"));
            System.out.println("获取name键的value:"+jedis.get("name"));
            //2、直接覆盖原来的数据
            System.out.println("在name的value后追加smith:"+jedis.append("name"," Smith"));
            System.out.println("获取name的value:"+jedis.get("name"));
        }
        /**
         * Redis 列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)
         * */
        private static void testList() {
            System.out.println("================testList====================");
            // 清空数据
            System.out.println("清空库中数据:"+jedis.flushDB());
            System.out.println("=============增=============");
            System.out.println("在头部添加元素:");
            jedis.lpush("names","William");
            jedis.lpush("names","Allen");
            jedis.lpush("names","Smith");
            jedis.lpush("names","Jack");
            System.out.println("在尾部添加元素:");
            jedis.rpush("scores","89");
            jedis.rpush("scores","93");
            jedis.rpush("scores","84");
            jedis.rpush("scores","91");
            
            System.out.println("=============查=============");
            System.out.println("names:"+jedis.lrange("names",0,-1));
            System.out.println("scores:"+jedis.lrange("scores",0,-1));
            
            System.out.println("=============删=============");
            System.out.println("删除names中的Smith:"+jedis.lrem("names",1,"Smith"));
            System.out.println("删除score中的93:"+jedis.lrem("scores",1,"93"));
            System.out.println("删除Smith后names:"+jedis.lrange("names",0,-1));
            System.out.println("删除93后scores:"+jedis.lrange("scores",0,-1));
            // 列表元素出栈
            System.out.println("列表出栈:"+jedis.lpop("names"));
            System.out.println("names出栈后:"+jedis.lrange("names",0,-1));
            
            System.out.println("=============改=============");
            // 修改list中指定下标的值
            jedis.lset("names", 0, "Thompson");
            System.out.println("names修改后:"+jedis.lrange("names",0,-1));
            System.out.println("=============查=============");
            // list长度
            System.out.println("names长度:"+jedis.llen("names"));
            System.out.println("scores长度:"+jedis.llen("scores"));
        }
        /**
         * Redis的Set是string类型的无序集合。
         * 集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)
         * */
        private static void testSet() {
            System.out.println("================testSet====================");
            // 清空数据
            System.out.println("清空库中数据:"+jedis.flushDB());
            
            System.out.println("=============增=============");
            System.out.println("向nation1添加一个元素:"+jedis.sadd("nation1","China"));
            System.out.println("向nation1添加多个元素:"+jedis.sadd("nation1","USA","English","France","Japan"));
            System.out.println("查看nation1中的所有元素:"+jedis.smembers("nation1"));
            System.out.println("再新增一个集合nation2:"+jedis.sadd("nation2","France","Japan","Germany"));
            System.out.println("查看nation2中的所有元素:"+jedis.smembers("nation2"));
            
            System.out.println("=============删=============");
            System.out.println("删除Japan:"+jedis.srem("nation1", "Japan"));
            System.out.println("删除并返回集合中一个随机元素:"+jedis.spop("nation1"));
            System.out.println("删除后的nation1:"+jedis.smembers("nation1"));
            
            System.out.println("=============查=============");
            System.out.println("查看France是否在nation1中:"+jedis.sismember("nation1", "France"));
            System.out.println("查看nation1中元素个数:"+jedis.scard("nation1"));
            
            System.out.println("=============集合运算=============");
            System.out.println("查看nation1中的所有元素:"+jedis.smembers("nation1"));
            System.out.println("查看nation2中的所有元素:"+jedis.smembers("nation2"));
            System.out.println("nation1和nation2交集:"+jedis.sinter("nation1", "nation2"));
            System.out.println("nation1和nation2并集:"+jedis.sunion("nation1", "nation2"));
            System.out.println("nation1和nation2差集:"+jedis.sdiff("nation1", "nation2"));
        }
        /**
         * Redis sorted set 和 set 一样也是string类型元素的集合,且不允许重复的成员。
         * 不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
         * sorted set的成员是唯一的,但分数(score)却可以重复。
         * */
        private static void testSortedSet() {
            System.out.println("================testSortedSet====================");
            // 清空数据
            System.out.println("清空库中数据:"+jedis.flushDB());
            
            System.out.println("=============增=============");
            Map<String,Double> scoreMember = new HashMap<String, Double>();
            scoreMember.put("USA",7.0);
            scoreMember.put("English",6.0);
            scoreMember.put("France",3.0);
            scoreMember.put("Japan",2.0);
            System.out.println("向nation添加一个元素:"+jedis.zadd("nation",9.0,"China"));
            System.out.println("向nation添加多个元素:"+jedis.zadd("nation",scoreMember));
            System.out.println("查询nation中的所有元素:"+jedis.zrange("nation", 0, -1));
            
            System.out.println("=============删=============");
            System.out.println("删除nation中的English:"+jedis.zrem("nation","English"));
            System.out.println("删除English后nation的所有元素:"+jedis.zrange("nation", 0, -1));
            
            System.out.println("=============查=============");
            System.out.println("查询nation中的元素个数:"+jedis.zcard("nation"));
            System.out.println("查询nation中4.0-8.0之间元素个数:"+jedis.zcount("nation",4.0,8.0));
        }
        /**
         * Redis hash 是一个键值(key=>value)对集合。
         * Redis hash 是一个 string 类型的 key 和 value 的映射表,hash 特别适合用于存储对象。
         * */
        private static void testHash() {
            System.out.println("================testHash====================");
            //清空数据
            System.out.println("清空库中数据:"+jedis.flushDB());
            
            System.out.println("=============增=============");
            Map<String,String> stuMap=new HashMap<String, String>();
            stuMap.put("pb13025002","William");
            stuMap.put("pb13025003","Smith");
            stuMap.put("pb13025004","Jack");
            stuMap.put("pb13025005","Linda");
            System.out.println("在student中添加一个键值对:"+jedis.hset("student","pb13025001","Allen"));
            System.out.println("在student中添加多个键值对:" +jedis.hmset("student",stuMap));
            
            System.out.println("=============查=============");
            System.out.println("查询student中所有的key:"+jedis.hkeys("student"));
            System.out.println("查询student中所有的value:"+jedis.hvals("student"));
            System.out.println("查询pb13025001是否在student的key中:"+jedis.hexists("student","pb13025001"));
            System.out.println("查询pb13025001对应的value:"+jedis.hget("student","pb13025001"));
            
            System.out.println("=============删=============");
            System.out.println("删除pb13025001字段:"+jedis.hdel("student","pb13025001"));
            System.out.println("删除pb13025001后所有的key:"+jedis.hkeys("student"));
            System.out.println("删除pb13025001后所有的value:"+jedis.hvals("student"));
            
            System.out.println("=============改=============");
            System.out.println("将pb13025005的value改为Thompson:"+jedis.hset("student","pb13025005","Thompson"));
            System.out.println("查询pb13025005对应的value:"+jedis.hget("student","pb13025005"));
        }
        /**
         * redis支持5种数据类型:string(字符串),list(列表),set(集合),sorted set(有序集合),hash(哈希)
         * */
        public static void test() {
            testString();
            testList();
            testSet();
            testSortedSet();
            testHash();
        }
        
        /* * * 获取客户端连接 * */ 
        public static void init(){ 
            initialPool(); 
            jedis = jedisPool.getResource(); 
        } 
        public static void main(String[] args) { 
            init(); 
            System.out.println("测试jedis运行情况: "+jedis.ping());
            test();
            jedis.close(); 
        } 
    }

    下面是程序运行结果:

    连接成功:PONG
    ================testString====================
    清空库中数据:OK
    =============增=============
    =============查=============
    添加的键值对:
    (name,william)
    (gender,male)
    (age,18)
    =============删=============
    删除gender键值对:1
    获取gender键的value:null
    =============改=============
    改变name键的value:OK
    获取name键的value:Allen
    在name的value后追加smith:11
    获取name的value:Allen Smith
    ================testList====================
    清空库中数据:OK
    =============增=============
    在头部添加元素:
    在尾部添加元素:
    =============查=============
    names:[Jack, Smith, Allen, William]
    scores:[89, 93, 84, 91]
    =============删=============
    删除names中的Smith:1
    删除score中的93:1
    删除Smith后names:[Jack, Allen, William]
    删除93后scores:[89, 84, 91]
    列表出栈:Jack
    names出栈后:[Allen, William]
    =============改=============
    names修改后:[Thompson, William]
    =============查=============
    names长度:2
    scores长度:3
    ================testSet====================
    清空库中数据:OK
    =============增=============
    向nation1添加一个元素:1
    向nation1添加多个元素:4
    查看nation1中的所有元素:[English, USA, France, China, Japan]
    再新增一个集合nation2:3
    查看nation2中的所有元素:[Germany, Japan, France]
    =============删=============
    删除Japan:1
    删除并返回集合中一个随机元素:USA
    删除后的nation1:[English, China, France]
    =============查=============
    查看France是否在nation1中:true
    查看nation1中元素个数:3
    =============集合运算=============
    查看nation1中的所有元素:[English, China, France]
    查看nation2中的所有元素:[Germany, Japan, France]
    nation1和nation2交集:[France]
    nation1和nation2并集:[France, China, English, Germany, Japan]
    nation1和nation2差集:[English, China]
    ================testSortedSet====================
    清空库中数据:OK
    =============增=============
    向nation添加一个元素:1
    向nation添加多个元素:4
    查询nation中的所有元素:[Japan, France, English, USA, China]
    =============删=============
    删除nation中的English:1
    删除English后nation的所有元素:[Japan, France, USA, China]
    =============查=============
    查询nation中的元素个数:4
    查询nation中4.0-8.0之间元素个数:1
    ================testHash====================
    清空库中数据:OK
    =============增=============
    在student中添加一个键值对:1
    在student中添加多个键值对:OK
    =============查=============
    查询student中所有的key:[pb13025004, pb13025003, pb13025005, pb13025002, pb13025001]
    查询student中所有的value:[Allen, Jack, Linda, Smith, William]
    查询pb13025001是否在student的key中:true
    查询pb13025001对应的value:Allen
    =============删=============
    删除pb13025001字段:1
    删除pb13025001后所有的key:[pb13025004, pb13025003, pb13025005, pb13025002]
    删除pb13025001后所有的value:[Jack, Linda, Smith, William]
    =============改=============
    将pb13025005的value改为Thompson:0
    查询pb13025005对应的value:Thompson
  • 相关阅读:
    Chrome使用指南
    Vue2.x-踩坑记
    C# WinForm listView 多行删除 操作
    Winform中DataGridView多行删除
    20211026_阿里云服务器引流限制ssl的问题
    docker commit
    docker build
    docker build与docker commit
    阿里云Docker镜像仓库(Docker Registry)
    Docker Nginx安装(centos7)
  • 原文地址:https://www.cnblogs.com/earthhouge/p/9885521.html
Copyright © 2020-2023  润新知