文章目錄
- 前言
- 一、創建項目
- 二、導入依賴
- 三、鍵操作
- 四、字符串操作
- 五、列表操作
- 六、集合操作
- 七、哈希表操作
- 八、有序集合操作
- 九、完整代碼
- 1. 完整代碼
- 2. 項目下載
前言
本文主要介紹如何使用 Java 操作 Redis 數據庫,涵蓋項目創建、依賴導入及 Redis 各數據類型(鍵、字符串、列表、集合、哈希表、有序集合)的常用操作示例。通過 IDEA + Maven 搭建開發環境,并基于 Jedis 客戶端實現代碼演示,適合 Java 開發者快速掌握 Redis 的編程接口與基本操作邏輯。
一、創建項目
在開始使用 Java 操作 Redis 之前,需先搭建開發環境。本章節以 IDEA 為開發工具,演示如何創建一個基于 Maven 的 Java 項目。通過 IDEA 的項目向導,依次選擇 Java 項目類型、配置 Maven 管理依賴、指定 JDK 版本,快速生成標準的項目結構,為后續引入 Redis 依賴和編寫代碼做好準備。
打開IDEA,點擊文件-->新建-->項目
。
如下圖所示,選擇Java-->輸入項目名-->選擇位置-->選擇Maven-->選擇JDK-->點擊創建
。
二、導入依賴
Maven 是 Java 項目中常用的依賴管理工具,通過在 pom.xml 文件中添加 Redis 客戶端(Jedis)的依賴配置,Maven 會自動下載并管理相關 Jar 包。本章節詳細說明如何在 Maven 項目中引入 Jedis 依賴(版本為 5.0.1),并通過 IDEA 的依賴刷新功能完成配置,確保項目能夠調用 Redis 的 Java API。
在pom.xml
文件中添加如下依賴配置。
<dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>5.0.1</version></dependency></dependencies>
添加依賴后如下圖所示,點擊右上角的刷新
圖標下載配置Redis依賴包。
三、鍵操作
鍵(Key)是 Redis 中最基礎的操作對象,本章節通過 Jedis 客戶端演示鍵的常用操作,包括創建鍵值對(set/mset)、獲取鍵值(get/mget)、查詢所有鍵或匹配模式的鍵(keys)、判斷鍵是否存在(exists)、重命名鍵(rename)、設置鍵過期時間(expire/ttl)、刪除鍵(del)等。代碼示例覆蓋了鍵操作的核心功能,幫助理解 Redis 鍵的生命周期管理。
public static void keyOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給school鍵設置值為dy。String set = jedis.set("school", "dy");System.out.println(set);System.out.println("===============");// 示例:給k1鍵設置值為v1,k2鍵設置值為v2,k3鍵設置值為v3。jedis.mset("k1", "v1", "k2", "v2", "k3", "v3");System.out.println(jedis.mget("k1", "k2", "k3"));System.out.println("===============");// 示例1:查看所有鍵。Set<String> keys = jedis.keys("*");for (String key : keys) {System.out.println(key);}System.out.println("===============");// 示例2:查看所有以k開頭的鍵。Set<String> keys1 = jedis.keys("k*");for (String key : keys1) {System.out.println(key);}System.out.println("===============");// 示例:查看scool鍵的值。String school = jedis.get("school");System.out.println(school);System.out.println("===============");// 示例:查看k1、k2、k3鍵的值。List<String> mget = jedis.mget("k1", "k2", "k3");for (String s : mget) {System.out.println(s);}System.out.println("===============");// 示例:查看k1鍵的值的序列化版本。byte[] k1s = jedis.dump("k1");System.out.println(Arrays.toString(k1s));System.out.println("===============");// 示例1:查看school鍵是否存在。Boolean exists = jedis.exists("school");System.out.println(exists);System.out.println("===============");// 示例2:查看k1、k2、k3、k4、k5鍵是否存在。long exists1 = jedis.exists("k1", "k2", "k3", "k4", "k5");System.out.println(exists1);// 示例:查看school鍵的值的類型。String type = jedis.type("school");System.out.println(type);System.out.println("===============");// 示例:把school鍵重命名為new_school鍵。String rename = jedis.rename("school", "new_school");System.out.println(rename);System.out.println("===============");// 示例:把k3鍵的生存時間設置為600秒。long expire = jedis.expire("k3", 600);System.out.println(expire);System.out.println("===============");// 示例:查看k3鍵的剩余存活時間。long ttl = jedis.ttl("k3");System.out.println(ttl);System.out.println("===============");// 示例:刪除k3鍵的存活時間。long persist = jedis.persist("k3");System.out.println(persist);System.out.println("===============");// 示例1:刪除school鍵。long del = jedis.del("school");System.out.println(del);System.out.println("===============");// 示例2:刪除k1、k2、k3鍵。long del1 = jedis.del("k1", "k2", "k3");System.out.println(del1);System.out.println("===============");// 關閉連接jedis.close();}
四、字符串操作
字符串(String)是 Redis 最常用的數據類型,適用于存儲簡單的鍵值對。本章節通過示例代碼演示字符串的常見操作,包括設置值(set/mset)、獲取值(get/mget)、獲取字符串長度(strlen)、截取子字符串(getrange)、替換指定索引內容(setrange)、追加內容(append)等。通過 getSet 方法還展示了原子性的 “獲取舊值并設置新值” 操作。
public static void stringOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給字符串鍵str1設置值為value1。String set = jedis.set("str1", "value1");System.out.println(set);System.out.println("===============");// 示例:給字符串鍵str2、str3、str4分別設置值為value2、value3、value4。String mset = jedis.mset("str2", "value2", "str3", "value3", "str4", "value4");System.out.println(mset);System.out.println("===============");// 示例:獲取字符串鍵str1的值。String str1 = jedis.get("str1");System.out.println(str1);System.out.println("===============");// 示例:獲取字符串鍵str2、str3、str4的值。List<String> mget = jedis.mget("str2", "str3", "str4");for (String s : mget) {System.out.println(s);}System.out.println("===============");// 示例:獲取字符串鍵str1的舊值,并設置新值為new_value1。String getSet = jedis.getSet("str1", "new_value1");System.out.println(getSet);// 查看設置后的新值。String str11 = jedis.get("str1");System.out.println(str11);System.out.println("===============");// 示例:獲取字符串鍵str1的值的長度。long strlen = jedis.strlen("str1");System.out.println(strlen);System.out.println("===============");// 示例:獲取字符串鍵str1的索引0-5的值的內容。String getrange = jedis.getrange("str1", 0, 5);System.out.println(getrange);System.out.println("===============");// 示例:把字符串鍵str1從索引2開始的后面三個字母的內容替換為aaa。long setrange = jedis.setrange("str1", 2, "aaa");System.out.println(setrange);// 查看設置后的新值。String str12 = jedis.get("str1");System.out.println(str12);System.out.println("===============");// 示例:向字符串鍵str1的結尾追加內容bbb。long append = jedis.append("str1", "bbb");System.out.println(append);// 查看設置后的新值。String str13 = jedis.get("str1");System.out.println(str13);// 關閉連接jedis.close();}
五、列表操作
列表(List)是 Redis 中有序、可重復的鏈表結構,支持從頭部或尾部插入 / 刪除元素。本章節通過代碼示例演示列表的核心操作,包括右推(rpush)和左推(lpush)元素、獲取列表范圍元素(lrange)、獲取指定索引元素(lindex)、彈出尾部(rpop)和頭部(lpop)元素、移除指定元素(lrem)、獲取列表長度(llen)等。列表操作適用于隊列、棧等場景。
public static void listOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:在列表右端向列表color中添加多個值。// rpush color blue green purple red whitelong rpush = jedis.rpush("color", "blue", "green", "purple", "red", "white");System.out.println(rpush);System.out.println("===============");// 示例:在列表左端向列表color中添加多個值。// lpush color blue2 green2 purple2 red2 white2long lpush = jedis.lpush("color", "blue2", "green2", "purple2", "red2", "white2");System.out.println(lpush);System.out.println("===============");// 示例1:獲取列表color中索引0-5的元素。List<String> color = jedis.lrange("color", 0, 5);for (String s : color) {System.out.println(s);}System.out.println("===============");// 示例2:獲取列表color中的所有元素。List<String> color1 = jedis.lrange("color", 0, -1);for (String s : color1) {System.out.println(s);}System.out.println("===============");// 示例:獲取列表color中索引為2的元素。String color2 = jedis.lindex("color", 2);System.out.println(color2);System.out.println("===============");// 示例:移除并獲取列表color中的最后一個(最右端)元素。String rpop = jedis.rpop("color");System.out.println(rpop);System.out.println("===============");// 示例:移除并獲取列表color中的第一個(最左端)元素。String lpop = jedis.lpop("color");System.out.println(lpop);System.out.println("===============");// 示例:獲取列表color的長度。long llen = jedis.llen("color");System.out.println(llen);System.out.println("===============");// 示例1:從列表開頭開始搜索移除列表color中1個red元素。long lrem = jedis.lrem("color", 1, "red");System.out.println(lrem);System.out.println("===============");// 示例2:從列表結尾開始搜索移除列表color中3個blue元素。long lrem1 = jedis.lrem("color", -3, "blue");System.out.println(lrem1);System.out.println("===============");// 示例3:移除列表color中所有blue2元素。long lrem2 = jedis.lrem("color", 0, "blue2");System.out.println(lrem2);System.out.println("===============");// 關閉連接jedis.close();}
六、集合操作
集合(Set)是 Redis 中無序、唯一的元素集合,支持高效的成員判斷和集合運算。本章節通過示例代碼演示集合的基本操作,包括添加成員(sadd)、獲取成員數量(scard)、查詢所有成員(smembers)、判斷成員是否存在(sismember)、移除成員(srem)、移動成員到其他集合(smove)等。集合操作適用于去重、交集 / 并集 / 差集計算等場景。
public static void setOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給集合鍵set1添加多個成員。// sadd set1 mem1 mem2 mem3long sadd = jedis.sadd("set1", "mem1", "mem2", "mem3");System.out.println(sadd);System.out.println("===============");// 示例:查看集合鍵set1中成員的數量。long scard = jedis.scard("set1");System.out.println(scard);System.out.println("===============");// 示例:查看集合鍵set1中的所有成員。Set<String> set1 = jedis.smembers("set1");for (String s : set1) {System.out.println(s);}System.out.println("===============");// 示例:檢查成員mem1是否在集合set1中。boolean sismember = jedis.sismember("set1", "mem1");System.out.println(sismember);System.out.println("===============");// 示例:移除集合set1中成員mem3。long srem = jedis.srem("set1", "mem3");System.out.println(srem);System.out.println("===============");// 示例:把集合set1中成員mem2移動到集合set2中。long smove = jedis.smove("set1", "set2", "mem2");System.out.println(smove);System.out.println("===============");// 關閉連接jedis.close();}
七、哈希表操作
哈希表(Hash)用于存儲字段和值的映射關系,適合存儲對象數據。本章節通過代碼示例演示哈希表的常用操作,包括單個字段設置(hset)和批量設置(hmset)、獲取單個字段值(hget)和多個字段值(hmget)、獲取所有字段和值(hgetAll)、刪除字段(hdel)等。哈希表操作能有效減少鍵的數量,提升數據結構化存儲的效率。
public static void hashOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給哈希表student添加一個學生信息。// hset student:1 name zhangsan// hset student:1 age 19// hset student:1 address yunnanlong hset = jedis.hset("student:1", "name", "zhangsan");System.out.println(hset);long hset1 = jedis.hset("student:1", "age", "19");System.out.println(hset1);long hset2 = jedis.hset("student:1", "address", "yunnan");System.out.println(hset2);System.out.println("===============");// 示例:給哈希表student添加一個學生信息。// hmset student:2 name lisi age 20 address guizhouMap<String, String> map = new HashMap<>();map.put("name", "lisi");map.put("age", "20");map.put("address", "guizhou");String hmset = jedis.hmset("student:2", map);System.out.println(hmset);System.out.println("===============");// 示例:獲取hash鍵student:2中name字段的值。String hget = jedis.hget("student:2", "name");System.out.println(hget);System.out.println("===============");// 示例:獲取hash鍵student:2中name、age和address字段的值。List<String> hget1 = jedis.hmget("student:2", "name", "age", "address");for (String s : hget1) {System.out.println(s);}System.out.println("===============");// 示例:獲取hash鍵student:2中的所有字段和值。Map<String, String> student2 = jedis.hgetAll("student:2");for (Map.Entry<String, String> entry : student2.entrySet()) {System.out.println(entry.getKey() + ": " + entry.getValue());}System.out.println("===============");// 示例:獲取hash鍵student:2中的所有字段。Set<String> hkeys = jedis.hkeys("student:2");for (String s : hkeys) {System.out.println(s);}System.out.println("===============");// 示例:獲取hash鍵student:2中的所有值。List<String> hvals = jedis.hvals("student:2");for (String s : hvals) {System.out.println(s);}System.out.println("===============");// 示例:刪除hash鍵student:2中的age和address字段。long hdel = jedis.hdel("student:2", "age", "address");System.out.println(hdel);System.out.println("===============");// 關閉連接jedis.close();}
八、有序集合操作
有序集合(Sorted Set)每個成員關聯一個分數(Score),通過分數實現自動排序。本章節通過示例代碼演示有序集合的核心操作,包括添加成員及分數(zadd)、獲取成員數量(zcard)、統計指定分數范圍的成員數(zcount)、按索引范圍獲取成員(zrange)、獲取成員分數(zscore)、移除成員(zrem)等。有序集合適用于排行榜、范圍查詢等場景。
public static void orderSetOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給有序集合salary添加多個分數和成員。// zadd salary 5000 zhnagsan 4500 lisi 6000 wangwuMap<String, Double> map = new HashMap<>();map.put("zhangsan", 5000.0);map.put("lisi", 4500.0);map.put("wangwu", 6000.0);long zadd = jedis.zadd("salary", map);System.out.println(zadd);System.out.println("===============");// 示例:獲取有序集合salary中的成員數量。long zcard = jedis.zcard("salary");System.out.println(zcard);System.out.println("===============");// 示例:計算有序集合中分數在5000-6500之間的成員數量。long zcount = jedis.zcount("salary", 5000, 6500);System.out.println(zcount);System.out.println("===============");// 示例:根據索引范圍獲取有序集合salary中的成員。// zrange salary 0 1List<String> salary = jedis.zrange("salary", 0, 1);for (String s : salary) {System.out.println(s);}System.out.println("===============");// 示例:獲取有序集合salary中成員wangwu的分數。Double zscore = jedis.zscore("salary", "wangwu");System.out.println(zscore);System.out.println("===============");// 示例:移除有序集合salary中成員wangwu。long zrem = jedis.zrem("salary", "wangwu");System.out.println(zrem);System.out.println("===============");// 關閉連接jedis.close();}
九、完整代碼
1. 完整代碼
本章節提供上述所有操作示例的整合代碼,方便開發者直接參考或運行。代碼包含項目初始化、依賴配置及各數據類型操作的完整邏輯,可用于驗證 Redis 功能或作為實際項目的基礎模板。通過完整代碼示例,可快速復現文中演示的所有操作,加深對 Java 操作 Redis 的整體理解。
package org.example;import redis.clients.jedis.Jedis;import java.util.*;public class Main {public static void main(String[] args) {flushDB(1);stringOperation();keyOperation();listOperation();setOperation();hashOperation();orderSetOperation();}public static void flushDB(int dbIndex) {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(dbIndex);// 清空數據庫jedis.flushDB();// 關閉連接jedis.close();}public static void keyOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給school鍵設置值為dy。String set = jedis.set("school", "dy");System.out.println(set);System.out.println("===============");// 示例:給k1鍵設置值為v1,k2鍵設置值為v2,k3鍵設置值為v3。jedis.mset("k1", "v1", "k2", "v2", "k3", "v3");System.out.println(jedis.mget("k1", "k2", "k3"));System.out.println("===============");// 示例1:查看所有鍵。Set<String> keys = jedis.keys("*");for (String key : keys) {System.out.println(key);}System.out.println("===============");// 示例2:查看所有以k開頭的鍵。Set<String> keys1 = jedis.keys("k*");for (String key : keys1) {System.out.println(key);}System.out.println("===============");// 示例:查看scool鍵的值。String school = jedis.get("school");System.out.println(school);System.out.println("===============");// 示例:查看k1、k2、k3鍵的值。List<String> mget = jedis.mget("k1", "k2", "k3");for (String s : mget) {System.out.println(s);}System.out.println("===============");// 示例:查看k1鍵的值的序列化版本。byte[] k1s = jedis.dump("k1");System.out.println(Arrays.toString(k1s));System.out.println("===============");// 示例1:查看school鍵是否存在。Boolean exists = jedis.exists("school");System.out.println(exists);System.out.println("===============");// 示例2:查看k1、k2、k3、k4、k5鍵是否存在。long exists1 = jedis.exists("k1", "k2", "k3", "k4", "k5");System.out.println(exists1);// 示例:查看school鍵的值的類型。String type = jedis.type("school");System.out.println(type);System.out.println("===============");// 示例:把school鍵重命名為new_school鍵。String rename = jedis.rename("school", "new_school");System.out.println(rename);System.out.println("===============");// 示例:把k3鍵的生存時間設置為600秒。long expire = jedis.expire("k3", 600);System.out.println(expire);System.out.println("===============");// 示例:查看k3鍵的剩余存活時間。long ttl = jedis.ttl("k3");System.out.println(ttl);System.out.println("===============");// 示例:刪除k3鍵的存活時間。long persist = jedis.persist("k3");System.out.println(persist);System.out.println("===============");// 示例1:刪除school鍵。long del = jedis.del("school");System.out.println(del);System.out.println("===============");// 示例2:刪除k1、k2、k3鍵。long del1 = jedis.del("k1", "k2", "k3");System.out.println(del1);System.out.println("===============");// 關閉連接jedis.close();}public static void stringOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給字符串鍵str1設置值為value1。String set = jedis.set("str1", "value1");System.out.println(set);System.out.println("===============");// 示例:給字符串鍵str2、str3、str4分別設置值為value2、value3、value4。String mset = jedis.mset("str2", "value2", "str3", "value3", "str4", "value4");System.out.println(mset);System.out.println("===============");// 示例:獲取字符串鍵str1的值。String str1 = jedis.get("str1");System.out.println(str1);System.out.println("===============");// 示例:獲取字符串鍵str2、str3、str4的值。List<String> mget = jedis.mget("str2", "str3", "str4");for (String s : mget) {System.out.println(s);}System.out.println("===============");// 示例:獲取字符串鍵str1的舊值,并設置新值為new_value1。String getSet = jedis.getSet("str1", "new_value1");System.out.println(getSet);// 查看設置后的新值。String str11 = jedis.get("str1");System.out.println(str11);System.out.println("===============");// 示例:獲取字符串鍵str1的值的長度。long strlen = jedis.strlen("str1");System.out.println(strlen);System.out.println("===============");// 示例:獲取字符串鍵str1的索引0-5的值的內容。String getrange = jedis.getrange("str1", 0, 5);System.out.println(getrange);System.out.println("===============");// 示例:把字符串鍵str1從索引2開始的后面三個字母的內容替換為aaa。long setrange = jedis.setrange("str1", 2, "aaa");System.out.println(setrange);// 查看設置后的新值。String str12 = jedis.get("str1");System.out.println(str12);System.out.println("===============");// 示例:向字符串鍵str1的結尾追加內容bbb。long append = jedis.append("str1", "bbb");System.out.println(append);// 查看設置后的新值。String str13 = jedis.get("str1");System.out.println(str13);// 關閉連接jedis.close();}public static void listOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:在列表右端向列表color中添加多個值。// rpush color blue green purple red whitelong rpush = jedis.rpush("color", "blue", "green", "purple", "red", "white");System.out.println(rpush);System.out.println("===============");// 示例:在列表左端向列表color中添加多個值。// lpush color blue2 green2 purple2 red2 white2long lpush = jedis.lpush("color", "blue2", "green2", "purple2", "red2", "white2");System.out.println(lpush);System.out.println("===============");// 示例1:獲取列表color中索引0-5的元素。List<String> color = jedis.lrange("color", 0, 5);for (String s : color) {System.out.println(s);}System.out.println("===============");// 示例2:獲取列表color中的所有元素。List<String> color1 = jedis.lrange("color", 0, -1);for (String s : color1) {System.out.println(s);}System.out.println("===============");// 示例:獲取列表color中索引為2的元素。String color2 = jedis.lindex("color", 2);System.out.println(color2);System.out.println("===============");// 示例:移除并獲取列表color中的最后一個(最右端)元素。String rpop = jedis.rpop("color");System.out.println(rpop);System.out.println("===============");// 示例:移除并獲取列表color中的第一個(最左端)元素。String lpop = jedis.lpop("color");System.out.println(lpop);System.out.println("===============");// 示例:獲取列表color的長度。long llen = jedis.llen("color");System.out.println(llen);System.out.println("===============");// 示例1:從列表開頭開始搜索移除列表color中1個red元素。long lrem = jedis.lrem("color", 1, "red");System.out.println(lrem);System.out.println("===============");// 示例2:從列表結尾開始搜索移除列表color中3個blue元素。long lrem1 = jedis.lrem("color", -3, "blue");System.out.println(lrem1);System.out.println("===============");// 示例3:移除列表color中所有blue2元素。long lrem2 = jedis.lrem("color", 0, "blue2");System.out.println(lrem2);System.out.println("===============");// 關閉連接jedis.close();}public static void setOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給集合鍵set1添加多個成員。// sadd set1 mem1 mem2 mem3long sadd = jedis.sadd("set1", "mem1", "mem2", "mem3");System.out.println(sadd);System.out.println("===============");// 示例:查看集合鍵set1中成員的數量。long scard = jedis.scard("set1");System.out.println(scard);System.out.println("===============");// 示例:查看集合鍵set1中的所有成員。Set<String> set1 = jedis.smembers("set1");for (String s : set1) {System.out.println(s);}System.out.println("===============");// 示例:檢查成員mem1是否在集合set1中。boolean sismember = jedis.sismember("set1", "mem1");System.out.println(sismember);System.out.println("===============");// 示例:移除集合set1中成員mem3。long srem = jedis.srem("set1", "mem3");System.out.println(srem);System.out.println("===============");// 示例:把集合set1中成員mem2移動到集合set2中。long smove = jedis.smove("set1", "set2", "mem2");System.out.println(smove);System.out.println("===============");// 關閉連接jedis.close();}public static void hashOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給哈希表student添加一個學生信息。// hset student:1 name zhangsan// hset student:1 age 19// hset student:1 address yunnanlong hset = jedis.hset("student:1", "name", "zhangsan");System.out.println(hset);long hset1 = jedis.hset("student:1", "age", "19");System.out.println(hset1);long hset2 = jedis.hset("student:1", "address", "yunnan");System.out.println(hset2);System.out.println("===============");// 示例:給哈希表student添加一個學生信息。// hmset student:2 name lisi age 20 address guizhouMap<String, String> map = new HashMap<>();map.put("name", "lisi");map.put("age", "20");map.put("address", "guizhou");String hmset = jedis.hmset("student:2", map);System.out.println(hmset);System.out.println("===============");// 示例:獲取hash鍵student:2中name字段的值。String hget = jedis.hget("student:2", "name");System.out.println(hget);System.out.println("===============");// 示例:獲取hash鍵student:2中name、age和address字段的值。List<String> hget1 = jedis.hmget("student:2", "name", "age", "address");for (String s : hget1) {System.out.println(s);}System.out.println("===============");// 示例:獲取hash鍵student:2中的所有字段和值。Map<String, String> student2 = jedis.hgetAll("student:2");for (Map.Entry<String, String> entry : student2.entrySet()) {System.out.println(entry.getKey() + ": " + entry.getValue());}System.out.println("===============");// 示例:獲取hash鍵student:2中的所有字段。Set<String> hkeys = jedis.hkeys("student:2");for (String s : hkeys) {System.out.println(s);}System.out.println("===============");// 示例:獲取hash鍵student:2中的所有值。List<String> hvals = jedis.hvals("student:2");for (String s : hvals) {System.out.println(s);}System.out.println("===============");// 示例:刪除hash鍵student:2中的age和address字段。long hdel = jedis.hdel("student:2", "age", "address");System.out.println(hdel);System.out.println("===============");// 關閉連接jedis.close();}public static void orderSetOperation() {// 連接本地的 Redis 服務Jedis jedis = new Jedis("127.0.0.1", 6379);jedis.select(1);// 示例:給有序集合salary添加多個分數和成員。// zadd salary 5000 zhnagsan 4500 lisi 6000 wangwuMap<String, Double> map = new HashMap<>();map.put("zhangsan", 5000.0);map.put("lisi", 4500.0);map.put("wangwu", 6000.0);long zadd = jedis.zadd("salary", map);System.out.println(zadd);System.out.println("===============");// 示例:獲取有序集合salary中的成員數量。long zcard = jedis.zcard("salary");System.out.println(zcard);System.out.println("===============");// 示例:計算有序集合中分數在5000-6500之間的成員數量。long zcount = jedis.zcount("salary", 5000, 6500);System.out.println(zcount);System.out.println("===============");// 示例:根據索引范圍獲取有序集合salary中的成員。// zrange salary 0 1List<String> salary = jedis.zrange("salary", 0, 1);for (String s : salary) {System.out.println(s);}System.out.println("===============");// 示例:獲取有序集合salary中成員wangwu的分數。Double zscore = jedis.zscore("salary", "wangwu");System.out.println(zscore);System.out.println("===============");// 示例:移除有序集合salary中成員wangwu。long zrem = jedis.zrem("salary", "wangwu");System.out.println(zrem);System.out.println("===============");// 關閉連接jedis.close();}
}
2. 項目下載
下載地址:https://download.csdn.net/download/zcs2312852665/90849803?spm=1001.2014.3001.5501