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);//Connection Redispub Lic static void Main (String [] args) {Hkeys ();} /** * Hset Sets the value of the field field in the hash table key to values. * Hget Returns the value of the given field field in the hash table key. * Hgetall returns the hash table key, all fields and values. * * */public static void Hset_hget_hgetall () {redis.flushdb ();//Clear Data 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 ...] * Multiple Field-value (domain-value) pairs are also set to the hash table key. * This command overwrites fields that already exist in the hash table. * If key does not exist, an empty hash table is created and the Hmset operation is performed. * */public static void Hmset_hmget () {redis.flushdb ();//Clear Data map<string,string> map=new hashmap<string,string > ("id", "1"), Map.put ("name", "Liliang"), Map.put ("Age", "Map.put"), Redis.hmset ("user", map); System.out.println ("======hmget=========="); List<string> list=redis.hmget ("user", "id", "name", "Age"), and for (String s:list) {System.out.println ("s:" +s);}} /** * Hlen Key * Returns the number of fields in the hash table key. * */public static void Hlen () {redis.flushdb ();//Clear Data map<string,string> map=new hashmap<string,string> (); Map.put ("id", "1"), Map.put ("name", "Liliang"), Map.put ("Age", "all"), Redis.hmset ("user", map); Long Length=redis.hlen ("user"); System.out.println ("Length:" +length);} /** * Hkeys Key * Returns all fields in the hash table key. * */public static void Hkeys () {redis.flushdb ();//Clear Data map<string,string> map=new hashmap<string,string> () ; Map.put ("id", "1"); Map.put ("Name", "Liliang"); Map.put ("Age", "all"); RediS.hmset ("user", map); Set<string> Set=redis.hkeys ("user"); for (String S:set) {System.out.println ("s:" +s);}}}
Redis Learning Note (3)-hash