您好,登錄后才能下訂單哦!
java中怎么redis的數(shù)據(jù)類型進(jìn)行操作,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
一、項(xiàng)目必備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>
二、實(shí)例與結(jié)構(gòu)
1)遠(yuǎn)程連接redis,清空數(shù)據(jù)庫(kù) new Jedis(redis服務(wù)連接IP,端口)
private static Jedis jedis; @Before public void before(){ jedis= JedisConnectUtil.getJedis(JedisTest.URL,63**); jedis.flushDB(); }
2)String類型
@Test public void testString(){ //設(shè)置鍵 獲取鍵 get set jedis.set("steve","stevetao"); System.out.println("設(shè)置后值:"+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("設(shè)置后值:"+jedis.get("steve")); System.out.println("再次設(shè)置后值:"+jedis.setnx("steve","stevetao")); //截取字符串 substr System.out.println("截取后值:"+jedis.substr("steve",0,4)); //設(shè)置多個(gè)鍵值對(duì) mset mget jedis.mset(new String[]{"zhangsan","123","lisi","1234"}); System.out.println("多次設(shè)置后值:"+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 表示列表的最后一個(gè)元素, -2 表示列表的倒數(shù)第二個(gè)元素,以此類推。 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)); //區(qū)別: 只能給存在的list做添加,不能項(xiàng)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)); //獲取集合長(zhǎng)度 llen 指定索引的值 lindex 保留截取的值 ltrim System.out.println("books集合長(zhǎng)度:"+jedis.llen("books")); System.out.println("books集合第二個(gè)數(shù)值:"+jedis.lindex("books",1)); jedis.ltrim("books",0,2); System.out.println("截取后books值:"+jedis.lrange("books",0,-1)); }
4)Hash類型
@Test public void testHash(){ //適合字段:設(shè)值hset 取值hget(如果value是json字符串,類似保存對(duì)象) jedis.hset("student","name","zhangsan"); System.out.println("student中name的值為:"+jedis.hget("student","name")); //適合對(duì)象:設(shè)值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的個(gè)數(shù)hlen 返回值hvals 返回鍵hkeys 鍵值對(duì)hgetAll jedis.hset("student","age","13"); jedis.hset("student","qq","2246920330"); jedis.hset("student","address","beijing"); System.out.println("student中鍵的個(gè)數(shù)為:"+jedis.hlen("student")); System.out.println("student中所有的鍵為:"+jedis.hkeys("student")); System.out.println("student中所有的值為:"+jedis.hvals("student")); System.out.println("student中所有的鍵值對(duì)為:"+jedis.hgetAll("student")); //刪除 hdel jedis.hdel("student",new String[]{"address","qq","age"}); System.out.println("刪除后,student中所有的鍵值對(duì)為:"+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個(gè)數(shù) scard 是否存在某個(gè)值 sismember System.out.println("set集合的個(gè)數(shù):"+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 隨機(jī)刪除并返回 spop System.out.println("刪除指定的值Steve:"+jedis.srem("student","Steve")); System.out.println("刪除的值為:"+jedis.spop("student")); System.out.println("再次排名不分先后:"+jedis.smembers("student")); //集合操作 System.out.println("兩個(gè)set的交集:"+jedis.sinter("student","girls")); System.out.println("兩個(gè)set的并集:"+jedis.sunion("student","girls")); System.out.println("student對(duì)girls的差集:"+jedis.sdiff("student","girls")); System.out.println("girls對(duì)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對(duì)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("有序集合的成員數(shù):"+jedis.zcard("math")); System.out.println("有序集合的成員:"+jedis.zrevrangeByScore("math",100,0)); //返回set<Tuple> System.out.println("有序集合的成員:"+jedis.zrangeWithScores("math",0,100)); }
關(guān)于java中怎么redis的數(shù)據(jù)類型進(jìn)行操作問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。