Redis hash 是一個 string 類型的 field(字段) 和 value(值) 的映射表,hash 特別適合用於存儲對象。html
Redis 中每一個 hash 能夠存儲 232 - 1 鍵值對(40多億)。java
簡單示例:redis
127.0.0.1:6379> HMSET runoobkey name "redis tutorial" description "redis basic commands for caching" likes 20 visitors 23000 OK 127.0.0.1:6379> HGETALL runoobkey 1) "name" 2) "redis tutorial" 3) "description" 4) "redis basic commands for caching" 5) "likes" 6) "20" 7) "visitors" 8) "23000"
hset:設置值。格式是:hset hash的key 項的key 項的值spa
操做以下:
code
java示例代碼:orm
import redis.clients.jedis.Jedis; /** * redis中對於hash類型的: hset 操做示例 * hset:設置值。格式是:hset hash的key 項的key 項的值 */ public class Hash_hset_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); /** * 示例1: hset hash的key 項的key 項的值 */ jedis.hset("myhash","id","3"); jedis.hset("myhash","name","xiaohong"); jedis.hset("myhash","age","13"); } }
hmset:同時設置多對值。格式是:hmset hash的key 項的key 項的值。(項的key和項的值能夠多對)htm
操做以下:
對象
java示例代碼:blog
import redis.clients.jedis.Jedis; import java.util.HashMap; import java.util.Map; /** * redis中對於hash類型的: hmset 操做示例 * hmset:同時設置多對值。格式是:hmset hash的key 項的key 項的值。(項的key和項的值能夠多對) */ public class Hash_hmset_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); /** * 示例1: hmset hash的key 項的key 項的值。(項的key和項的值能夠多對) */ Map<String,String> map = new HashMap<>(); map.put("id","1"); map.put("name","dongdong"); map.put("age","3"); jedis.hmset("myhash",map); } }
hget:獲取值。格式是:hget hash的key 項的key
操做以下:
ip
java示例代碼:
import redis.clients.jedis.Jedis; /** * redis中對於hash類型的: hget 操做示例 * hget:獲取值。格式是:hget hash的key 項的key */ public class Hash_hget_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); /** * 示例1: hget hash的key 項的key */ String id = jedis.hget("myhash", "id"); String name = jedis.hget("myhash", "name"); String age = jedis.hget("myhash", "age"); System.out.println(String.format("id = %s, name = %s, age = %s",id,name,age)); } }
hmget:同時獲取多對值。格式是:hmget hash的key 項的key。(項的key能夠有多個)
操做以下:
java示例代碼:
import redis.clients.jedis.Jedis; import java.util.List; /** * redis中對於hash類型的: hmget 操做示例 * hmget:同時獲取多對值。格式是:hmget hash的key 項的key。(項的key能夠有多個) */ public class Hash_hmget_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); /** * 示例1: hmget hash的key 項的key。(項的key能夠有多個) */ List<String> hmget = jedis.hmget("myhash", "id", "name", "age"); System.out.println("hmget = " + hmget); } }
hgetall:獲取該key下全部的值。格式是:hgetall hash的key
操做以下:
java示例代碼:
import redis.clients.jedis.Jedis; import java.util.Map; /** * redis中對於hash類型的: hgetall 操做示例 * hgetall:獲取該key下全部的值。格式是:hgetall hash的key */ public class Hash_hgetall_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); /** * 示例1: hgetall hash的key */ Map<String, String> myhash = jedis.hgetAll("myhash"); System.out.println("myhash = " + myhash); } }
hdel:刪除某個項。格式是:hdel hash的key 項的key
操做以下:
java示例代碼:
import redis.clients.jedis.Jedis; import java.util.Map; /** * redis中對於hash類型的: hdel 操做示例 * hdel:刪除某個項。格式是:hdel hash的key 項的key */ public class Hash_hdel_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); Map<String, String> myhash = jedis.hgetAll("myhash"); System.out.println("myhash = " + myhash); /** * 示例1: hdel hash的key 項的key */ Long hdel = jedis.hdel("myhash", "age"); System.out.println("hdel = " + hdel); Map<String, String> myhash1 = jedis.hgetAll("myhash"); System.out.println("myhash1 = " + myhash1); } }
hlen:獲取key裏面的鍵值對數量。格式是:hlen hash的key
操做以下:
java示例代碼
import redis.clients.jedis.Jedis; import java.util.Map; /** * redis中對於hash類型的: hlen 操做示例 * hlen:獲取key裏面的鍵值對數量。格式是:hlen hash的key */ public class Hash_hlen_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); Map<String, String> myhash = jedis.hgetAll("myhash"); System.out.println("myhash = " + myhash); /** * 示例1: hlen hash的key */ Long hlen = jedis.hlen("myhash"); System.out.println("hlen = " + hlen); } }
hexists:判斷鍵值是否存在。格式是:hexists hash的key 項的key
操做以下:
java示例代碼:
import redis.clients.jedis.Jedis; import java.util.Map; /** * redis中對於hash類型的: hexists 操做示例 * hexists:判斷鍵值是否存在。格式是:hexists hash的key 項的key */ public class Hash_hexists_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); Map<String, String> myhash = jedis.hgetAll("myhash"); System.out.println("myhash = " + myhash); /** * 示例1: hexists hash的key 項的key */ Boolean hexists = jedis.hexists("myhash", "id"); System.out.println("hexists = " + hexists); } }
hkeys:獲取全部的item的key,格式是:hkeys hash的key
操做以下:
java示例代碼:
import redis.clients.jedis.Jedis; import java.util.Map; import java.util.Set; /** * redis中對於hash類型的: hexists 操做示例 * hkeys:獲取全部的item的key,格式是:hkeys hash的key */ public class Hash_hkeys_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); Map<String, String> myhash = jedis.hgetAll("myhash"); System.out.println("myhash = " + myhash); /** * 示例1: hkeys hash的key */ Set<String> hkeys = jedis.hkeys("myhash"); System.out.println("hkeys = " + hkeys); } }
hvals:獲取全部的item的值,格式是:hvals hash的key
操做以下:
java示例代碼:
import redis.clients.jedis.Jedis; import java.util.List; import java.util.Map; /** * redis中對於hash類型的: hexists 操做示例 * hvals:獲取全部的item的值,格式是:hvals hash的key */ public class Hash_hvals_operation { public static void main(String[] args) { Jedis jedis = new Jedis("127.0.0.1",6379); Map<String, String> myhash = jedis.hgetAll("myhash"); System.out.println("myhash = " + myhash); /** * 示例1: hvals hash的key */ List<String> hvals = jedis.hvals("myhash"); System.out.println("hvals = " + hvals); } }