摘要:一八種數(shù)據(jù)類型二三四集群一概述是官方推薦的連接開發(fā)工具。使可以操作中間件。
Jedis 是 Redis 官方推薦的 Java 連接開發(fā)工具。使 Java 可以操作 Redis 中間件。
public class TestPing { public static void main(String[] args) { // 創(chuàng)建連接 Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.printf("測(cè)試連接: %s", jedis.ping()).println(); // 關(guān)閉連接 jedis.close(); }}
public class TestPassword { public static void main(String[] args) { // 創(chuàng)建連接 Jedis jedis = new Jedis("127.0.0.1", 6379); // 未設(shè)置密碼,跳過(guò)// System.out.printf("驗(yàn)證密碼: %s", jedis.auth("123456")).println(); // 連接 jedis.connect(); // 斷開連接 jedis.disconnect(); // 清除所有key jedis.flushAll(); // 關(guān)閉連接 jedis.close(); }}
public class TestKey { public static void main(String[] args) { // 創(chuàng)建連接 Jedis jedis = new Jedis("127.0.0.1", 6379); System.out.println("清空數(shù)據(jù): " + jedis.flushDB()); System.out.println("判斷username鍵是否存在: " + jedis.exists("username")); System.out.println("新增<"username","qs">的鍵值對(duì): " + jedis.set("username", "qs")); System.out.println("新增<"password","123">的鍵值對(duì): " + jedis.set("password", "123")); System.out.print("系統(tǒng)中所有的鍵如下: "); Set<String> keys = jedis.keys("*"); System.out.println(keys); System.out.println("刪除鍵password: " + jedis.del("password")); System.out.println("判斷鍵password是否存在: " + jedis.exists("password")); System.out.println("查看鍵username所存儲(chǔ)的值的類型: " + jedis.type("username")); System.out.println("隨機(jī)返回key空間中一個(gè)key: " + jedis.randomKey()); System.out.println("重命名key: " + jedis.rename("username", "name")); System.out.println("取出修改后的name: " + jedis.get("name")); System.out.println("按索引查詢: " + jedis.select(0)); System.out.println("刪除當(dāng)前選擇數(shù)據(jù)庫(kù)中的所有key: " + jedis.flushDB()); System.out.println("返回當(dāng)前數(shù)據(jù)庫(kù)中key的數(shù)目: " + jedis.dbSize()); System.out.println("刪除所有數(shù)據(jù)庫(kù)中的所有key: " + jedis.flushAll()); // 關(guān)閉連接 jedis.close(); }}
public class TestString { public static void main(String[] args) { // 創(chuàng)建連接 Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("===========增加數(shù)據(jù)==========="); System.out.println(jedis.set("key1", "value1")); System.out.println(jedis.set("key2", "value2")); System.out.println(jedis.set("key3", "value3")); System.out.println("修改key1: " + jedis.set("key1", "value1Changed")); System.out.println("獲取key1的值: " + jedis.get("key1")); System.out.println("刪除鍵key2: " + jedis.del("key2")); System.out.println("獲取鍵key2: " + jedis.get("key2")); System.out.println("在key3后面加入值: " + jedis.append("key3", "End")); System.out.println("獲取key3的值: " + jedis.get("key3")); System.out.println("增加多個(gè)鍵值對(duì):" + jedis.mset("key01", "value01", "key02", "value02", "key03", "value03")); System.out.println("獲取多個(gè)鍵值對(duì):" + jedis.mget("key01", "key02", "key03")); System.out.println("獲取多個(gè)鍵值對(duì):" + jedis.mget("key01", "key02", "key03", "key04")); System.out.println("刪除多個(gè)鍵值對(duì):" + jedis.del("key01", "key02")); System.out.println("獲取多個(gè)鍵值對(duì):" + jedis.mget("key01", "key02", "key03")); jedis.flushDB(); System.out.println("===========新增鍵值對(duì)防止覆蓋原先值=============="); System.out.println(jedis.setnx("key1", "value1")); System.out.println(jedis.setnx("key2", "value2")); System.out.println(jedis.setnx("key2", "value2-new")); System.out.println(jedis.get("key1")); System.out.println(jedis.get("key2")); System.out.println("===========新增鍵值對(duì)并設(shè)置有效時(shí)間============="); System.out.println(jedis.setex("key3", 2, "value3")); System.out.println(jedis.get("key3")); try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(jedis.get("key3")); System.out.println("===========獲取原值,更新為新值=========="); System.out.println(jedis.getSet("key2", "key2GetSet")); System.out.println(jedis.get("key2")); System.out.println("獲得key2的值的字串: " + jedis.getrange("key2", 2, 4)); // 關(guān)閉連接 jedis.close(); }}
public class TestList { public static void main(String[] args) { // 創(chuàng)建連接 Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("===========添加一個(gè)list==========="); jedis.lpush("collections", "ArrayList", "Vector", "Stack", "HashMap", "WeakHashMap", "LinkedHashMap"); jedis.lpush("collections", "HashSet"); jedis.lpush("collections", "TreeSet"); jedis.lpush("collections", "TreeMap"); // -1代表倒數(shù)第一個(gè)元素,-2代表倒數(shù)第二個(gè)元素,end為-1表示查詢?nèi)?/span> System.out.println("collections的內(nèi)容: " + jedis.lrange("collections", 0, -1)); System.out.println("collections區(qū)間0-3的元素: " + jedis.lrange("collections", 0, 3)); System.out.println("==============================="); // 刪除列表指定的值,第二個(gè)參數(shù)為刪除的個(gè)數(shù)(有重復(fù)時(shí)),后add進(jìn)去的值先被刪,類似于出棧 System.out.println("刪除指定元素個(gè)數(shù): " + jedis.lrem("collections", 2, "HashMap")); System.out.println("collections的內(nèi)容: " + jedis.lrange("collections", 0, -1)); System.out.println("刪除下標(biāo)0-3區(qū)間之外的元素: " + jedis.ltrim("collections", 0, 3)); System.out.println("collections的內(nèi)容: " + jedis.lrange("collections", 0, -1)); System.out.println("collections列表出棧(左端): " + jedis.lpop("collections")); System.out.println("collections的內(nèi)容: " + jedis.lrange("collections", 0, -1)); System.out.println("collections添加元素,從列表右端,與lpush相對(duì)應(yīng): " + jedis.rpush("collections", "EnumMap")); System.out.println("collections的內(nèi)容: " + jedis.lrange("collections", 0, -1)); System.out.println("collections列表出棧(右端): " + jedis.rpop("collections")); System.out.println("collections的內(nèi)容: " + jedis.lrange("collections", 0, -1)); System.out.println("修改collections指定下標(biāo)1的內(nèi)容: " + jedis.lset("collections", 1, "LinkedArrayList")); System.out.println("collections的內(nèi)容: " + jedis.lrange("collections", 0, -1)); System.out.println("==============================="); System.out.println("collections的長(zhǎng)度: " + jedis.llen("collections")); System.out.println("獲取collections下標(biāo)為2的元素: " + jedis.lindex("collections", 2)); System.out.println("==============================="); jedis.lpush("sortedList", "3", "6", "2", "0", "7", "4"); System.out.println("sortedList排序前: " + jedis.lrange("sortedList", 0, -1)); System.out.println(jedis.sort("sortedList")); System.out.println("sortedList排序后: " + jedis.lrange("sortedList", 0, -1)); // 關(guān)閉連接 jedis.close(); }}
public class TestSet { public static void main(String[] args) { // 創(chuàng)建連接 Jedis jedis = new Jedis("127.0.0.1", 6379); jedis.flushDB(); System.out.println("============向集合中添加元素(不重復(fù))============"); System.out.println(jedis.sadd("eleSet", "e1", "e2", "e4", "e3", "e0", "e8", "e7", "e5")); System.out.println(jedis.sadd("eleSet", "e6")); System.out.println(jedis.sadd("eleSet", "e6")); System.out.println("eleSet的所有元素為: " + jedis.smembers("eleSet")); System.out.println(&
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/124027.html
摘要:,中導(dǎo)入包在中新建一個(gè),在項(xiàng)目下創(chuàng)建文件夾,將包復(fù)制到中,并將包添加到編譯環(huán)境中右鍵,目錄結(jié)構(gòu)大致如下,單例連接在外界訪問(wèn)服務(wù)時(shí)要開放防火墻的端口,不然會(huì)訪問(wèn)不到下輸入當(dāng)然也可以關(guān)閉防火墻。 1,Eclipse中導(dǎo)入jar包 在Eclipse中新建一個(gè)Java Project,在項(xiàng)目下創(chuàng)建lib文件夾,將jar包復(fù)制到lib中,并將jar包添加到編譯環(huán)境中(右鍵lib-->Build ...
摘要:操作之連接以及簡(jiǎn)單操作下載對(duì)應(yīng)的驅(qū)動(dòng)包下載創(chuàng)建一個(gè)連接類連接主機(jī)地址端口號(hào)登錄密碼連接服務(wù)器權(quán)限認(rèn)證連接完成會(huì)返回緩存鏈接錯(cuò)誤查詢所有中的查詢所有的為通配符清除所有的中的是清除所有的的命令如果清理完成,會(huì)返回完整的代碼聲明對(duì)象測(cè)試地址端口密 Java操作Redis之連接以及簡(jiǎn)單操作 1.下載對(duì)應(yīng)的驅(qū)動(dòng)包 下載 jedis.jar :https://mvnrepository.com/a...
閱讀 3123·2021-11-22 09:34
閱讀 628·2021-11-22 09:34
閱讀 2473·2021-10-08 10:18
閱讀 3411·2021-09-22 15:57
閱讀 2624·2021-09-22 15:25
閱讀 2451·2019-08-30 15:54
閱讀 2185·2019-08-30 15:44
閱讀 1824·2019-08-29 11:18