java中怎么redis的數(shù)據(jù)類型進(jìn)行操作,針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站制作、成都網(wǎng)站制作、巨野網(wǎng)絡(luò)推廣、小程序開發(fā)、巨野網(wǎng)絡(luò)營銷、巨野企業(yè)策劃、巨野品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供巨野建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
一、項目必備Jar包
redis.clients jedis 2.9.0 commons-pool commons-pool 1.6 junit junit 4.12
二、實例與結(jié)構(gòu)
1)遠(yuǎn)程連接redis,清空數(shù)據(jù)庫 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è)置多個鍵值對 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 表示列表的最后一個元素, -2 表示列表的倒數(shù)第二個元素,以此類推。 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做添加,不能項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集合第二個數(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字符串,類似保存對象) jedis.hset("student","name","zhangsan"); System.out.println("student中name的值為:"+jedis.hget("student","name")); //適合對象:設(shè)值hmset 取值hmget Mapmap = new HashMap (); 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的個數(shù)hlen 返回值hvals 返回鍵hkeys 鍵值對hgetAll jedis.hset("student","age","13"); jedis.hset("student","qq","2246920330"); jedis.hset("student","address","beijing"); System.out.println("student中鍵的個數(shù)為:"+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個數(shù) scard 是否存在某個值 sismember System.out.println("set集合的個數(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 隨機刪除并返回 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("有序集合的成員數(shù):"+jedis.zcard("math")); System.out.println("有序集合的成員:"+jedis.zrevrangeByScore("math",100,0)); //返回setSystem.out.println("有序集合的成員:"+jedis.zrangeWithScores("math",0,100)); }
關(guān)于java中怎么redis的數(shù)據(jù)類型進(jìn)行操作問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。