Redis學習筆記(3)-Hash

來源:互聯網
上載者:User

標籤:

package cn.com;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Set;import redis.clients.jedis.Jedis;public class Redis_Hash {public static Jedis redis = new Jedis("localhost", 6379);// 串連redispublic static void main(String [] args){hkeys();}/** * hset 將雜湊表 key 中的域 field 的值設為 value 。 * hget 返回雜湊表 key 中給定域 field 的值。 * hgetAll 返回雜湊表 key 中,所有的域和值。 *  * */public static void hset_hget_hgetAll(){redis.flushDB();//清除資料redis.hset("user", "id", "1");redis.hset("user", "name", "liliang");redis.hset("user", "age", "18");System.out.println("==========hget============");System.out.println(redis.hget("user", "id"));System.out.println(redis.hget("user", "name"));System.out.println(redis.hget("user", "age"));System.out.println("=========hgetAll===========");Map<String,String> map=redis.hgetAll("user");System.out.println("id:"+map.get("id"));System.out.println("name:"+map.get("name"));System.out.println("age:"+map.get("age"));}/** * HMSET key field value [field value ...] * 同時將多個 field-value (域-值)對設定到雜湊表 key 中。 * 此命令會覆蓋雜湊表中已存在的域。 * 如果 key 不存在,一個空雜湊表被建立並執行 HMSET 操作。 * */public static void hmset_hmget(){redis.flushDB();//清除資料Map<String,String> map=new HashMap<String,String>();map.put("id","1");map.put("name","liliang");map.put("age","18");redis.hmset("user", map);System.out.println("======HMGET==========");List<String> list=redis.hmget("user","id","name","age");for(String s:list){System.out.println("s:"+s);}}/** * HLEN key * 返回雜湊表 key 中域的數量。 * */public static void hlen(){redis.flushDB();//清除資料Map<String,String> map=new HashMap<String,String>();map.put("id","1");map.put("name","liliang");map.put("age","18");redis.hmset("user", map);Long length=redis.hlen("user");System.out.println("length:"+length);}/** * HKEYS key *返回雜湊表 key 中的所有域。 * */public static void hkeys(){redis.flushDB();//清除資料Map<String,String> map=new HashMap<String,String>();map.put("id","1");map.put("name","liliang");map.put("age","18");redis.hmset("user", map);Set<String> set=redis.hkeys("user");for(String s:set){System.out.println("s:"+s);}}}

  

Redis學習筆記(3)-Hash

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.