您好,登錄后才能下訂單哦!
java中怎么redis的數據類型進行操作,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一、項目必備Jar包
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
二、實例與結構
1)遠程連接redis,清空數據庫 new Jedis(redis服務連接IP,端口)
private static Jedis jedis; @Before public void before(){ jedis= JedisConnectUtil.getJedis(JedisTest.URL,63**); jedis.flushDB(); }
2)String類型
@Test public void testString(){ //設置鍵 獲取鍵 get set jedis.set("steve","stevetao"); System.out.println("設置后值:"+jedis.get("steve")); //追加鍵 append jedis.append("steve"," Is Good Man"); System.out.println("追加后值:"+jedis.get("steve")); //刪除操作 del jedis.del("steve"); System.out.println("刪除后值:"+jedis.get("steve")); //不存在就保存, setnx msetnx jedis.setnx("steve","stevetao"); System.out.println("設置后值:"+jedis.get("steve")); System.out.println("再次設置后值:"+jedis.setnx("steve","stevetao")); //截取字符串 substr System.out.println("截取后值:"+jedis.substr("steve",0,4)); //設置多個鍵值對 mset mget jedis.mset(new String[]{"zhangsan","123","lisi","1234"}); System.out.println("多次設置后值:"+jedis.mget("zhangsan","lisi")); //遞增遞減 incr decr incrby decrby jedis.incr("zhangsan"); jedis.decr("lisi"); System.out.println("遞增遞減后值:"+jedis.mget("zhangsan","lisi")); jedis.incrBy("zhangsan",6); jedis.decrBy("lisi",3); System.out.println("遞增遞減后值:"+jedis.mget("zhangsan","lisi")); }
3)List類型
@Test public void testList(){ //尾添加 rpush 頭添加 lpush jedis.lpush("books","java","C++","Ruby","Scala","python"); jedis.rpush("language","java","C++","Ruby","Scala","python"); // -1 表示列表的最后一個元素, -2 表示列表的倒數第二個元素,以此類推。 System.out.println("頭添加后books值:"+jedis.lrange("books",0,-1)); System.out.println("尾添加后language值:"+jedis.lrange("language",0,-1)); //尾部刪除 rpop 頭部刪除 lpop System.out.println("刪除的值為:"+jedis.lpop("books")); System.out.println("刪除的值為:"+jedis.rpop("language")); System.out.println("頭部刪除后books值:"+jedis.lrange("books",0,-1)); System.out.println("尾部刪除后language值:"+jedis.lrange("language",0,-1)); //尾部刪除并頭添加 rpoplpush jedis.rpoplpush("language","books"); System.out.println("尾部刪除并頭添加后books值:"+jedis.lrange("books",0,-1)); System.out.println("尾部刪除并頭添加后language值:"+jedis.lrange("language",0,-1)); //區別: 只能給存在的list做添加,不能項lpush那樣能新增list jedis.lpushx("books","php"); jedis.lpushx("book","php"); System.out.println("頭添加后books值:"+jedis.lrange("books",0,-1)); System.out.println("頭添加后book值:"+jedis.lrange("book",0,-1)); //獲取集合長度 llen 指定索引的值 lindex 保留截取的值 ltrim System.out.println("books集合長度:"+jedis.llen("books")); System.out.println("books集合第二個數值:"+jedis.lindex("books",1)); jedis.ltrim("books",0,2); System.out.println("截取后books值:"+jedis.lrange("books",0,-1)); }
4)Hash類型
@Test public void testHash(){ //適合字段:設值hset 取值hget(如果value是json字符串,類似保存對象) jedis.hset("student","name","zhangsan"); System.out.println("student中name的值為:"+jedis.hget("student","name")); //適合對象:設值hmset 取值hmget Map<String,String> map = new HashMap<String,String>(); map.put("name", "lisi"); map.put("age", "36"); jedis.hmset("teacher",map); System.out.println("teacher中name、age的值為:"+jedis.hmget("teacher","name","age")); //teacher是否存在鍵age hexists if(jedis.hexists("teacher","age")){ //給指定值增加4 hincrBy jedis.hincrBy("teacher","age",4); System.out.println("teacher中name、age的值為:"+jedis.hmget("teacher","name","age")); } //返回key的個數hlen 返回值hvals 返回鍵hkeys 鍵值對hgetAll jedis.hset("student","age","13"); jedis.hset("student","qq","2246920330"); jedis.hset("student","address","beijing"); System.out.println("student中鍵的個數為:"+jedis.hlen("student")); System.out.println("student中所有的鍵為:"+jedis.hkeys("student")); System.out.println("student中所有的值為:"+jedis.hvals("student")); System.out.println("student中所有的鍵值對為:"+jedis.hgetAll("student")); //刪除 hdel jedis.hdel("student",new String[]{"address","qq","age"}); System.out.println("刪除后,student中所有的鍵值對為:"+jedis.hgetAll("student")); }
5)Set類型
@Test public void testSet(){ //set中添加值 sadd 取值 smembers jedis.sadd("student","Jan","John","Steve","jack","lili","peter","Anna"); jedis.sadd("girls","Jan","lili","Alice","Jeanne","Anna"); System.out.println("排名不分先后:"+jedis.smembers("student")); //set個數 scard 是否存在某個值 sismember System.out.println("set集合的個數:"+jedis.scard("student")); System.out.println("student是否存在steve:"+jedis.sismember("student","Steve")); System.out.println("student是否存在stevetao:"+jedis.sismember("student","Stevetao")); //System.out.println(jedis.sscan("student","0").getResult()); //刪除指定的值 srem 隨機刪除并返回 spop System.out.println("刪除指定的值Steve:"+jedis.srem("student","Steve")); System.out.println("刪除的值為:"+jedis.spop("student")); System.out.println("再次排名不分先后:"+jedis.smembers("student")); //集合操作 System.out.println("兩個set的交集:"+jedis.sinter("student","girls")); System.out.println("兩個set的并集:"+jedis.sunion("student","girls")); System.out.println("student對girls的差集:"+jedis.sdiff("student","girls")); System.out.println("girls對student的差集:"+jedis.sdiff("girls","student")); //集合操作并保存 jedis.sinterstore("jiaoji","student","girls"); jedis.sunionstore("bingji","student","girls"); jedis.sdiffstore("chaji","student","girls"); System.out.println("交集:"+jedis.smembers("jiaoji")); System.out.println("并集:"+jedis.smembers("bingji")); System.out.println("student對girls的差集:"+jedis.smembers("chaji")); }
6)ZSet類型
@Test public void testZset(){ jedis.zadd("math",75,"Jim"); jedis.zadd("math",86,"Lina"); jedis.zadd("math",52,"Dive"); jedis.zadd("math",91,"Bobber"); System.out.println("有序集合的成員數:"+jedis.zcard("math")); System.out.println("有序集合的成員:"+jedis.zrevrangeByScore("math",100,0)); //返回set<Tuple> System.out.println("有序集合的成員:"+jedis.zrangeWithScores("math",0,100)); }
關于java中怎么redis的數據類型進行操作問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。