In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Redis hash is a mapping table for field and value of type string, and hash is particularly suitable for storing objects.
Storing an object in a Hash type takes up less memory and makes it easier to access the entire object.
1. Hset method:
HSET key field value
Set the value of the field (field) field in the hash table key to value.
If key does not exist, a new hash table is created and HSET is performed.
If the domain field already exists in the hash table, the old value will be overwritten.
Redis > HSET website google "www.g.cn" # set the field google of hash table websit to www.g.cn (integer) 1redis > HSET website google "www.google.com" # overwrite an old field (integer) 0
2. Hsetnx method:
HSETNX key field value
Set the value of the field field in the hash table key to value if and only if the domain field does not exist.
If the domain field already exists, the operation is invalid.
If key does not exist, a new hash table is created and the HSETNX command is executed.
Invalid redis > HSETNX nosql key-value-store redis (integer) 1redis > HSETNX nosql key-value-store redis # operation, domain key-value-store already exists (integer) 0
3. Hmset method:
HMSET key field value [field value...]
Simultaneously set multiple field-value (field-value) pairs 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.
# case 1: hash table redis > HMSET website google www.google.com yahoo www.yahoo.comOKredis > HGET website google "www.google.com" redis > HGET website yahoo "www.yahoo.com" # case 2: redis > SET G 10 # error case OKredis > HMSET G name huangz age 20 (error) ERR Operation against a key holding the wrong kind of value
4. Hget method:
HGET key field
Returns the value of the given field field in the hash table key.
Redis > HSET huangz blog huangz.51cto.com (integer) 1redis > HGET huangz blog "huangz.51cto.com"
5. Hmget method:
HMGET key field [field...]
Returns the value of one or more given fields in the hash table key.
If the given field does not exist in the hash table, a Nil value is returned.
Because a non-existent key is treated as an empty hash table, a HMGET operation on a non-existent key returns a table with only Nil values.
Redis > HMSET pet dog "tudou" cat "wandou" # Save more than one value at a time OKredis > HMGET pet dog cat fake_pet # return values in the same order as the parameters passed in. 1) "tudou" 2) "wandou" 3) (nil) # fields that do not exist return Nil values
6. Hgetall method:
HGETALL key
Returns all fields and values in the hash table key.
In the return value, each domain name (field name) is followed by the domain value (value), so the length of the return value is twice the size of the hash table.
Redis > HSET hash_name jack "Jack Sparrow" (integer) 1redis > HSET hash_name gump "Forrest Gump" (integer) 1redis > HGETALL hash_name1) "jack" # Domain 2) "Jack Sparrow" # value 3) "gump" # Domain 4) "Forrest Gump" # value
7. Hdel method:
HDEL key field [field...]
Delete one or more specified fields in the hash table key, and fields that do not exist will be ignored.
Note: in versions below Redis2.4, HDEL can only delete a single domain at a time. If you need to delete multiple fields in an atomic time, please include the command in the MULTI/ EXEC block.
# Test data redis > HGETALL abbr1) "a" 2) "apple" 3) "b" 4) "banana" 5) "c" 6) "cat" 7) "d" 8) "dog" # Delete a single domain redis > HDEL abbr a (integer) "delete domain redis > HDEL abbr not-exists-field (integer)" delete multiple domains redis > HDEL abbr b c (integer) 2redis > HGETALL abbr1) "d" 2) "dog"
8. Hlen method:
HLEN key
Returns the number of fields in the hash table key.
Redis > HSET hash_name jack "Jack Sparrow" (integer) 1redis > HSET hash_name gump "Forrest Gump" (integer) 1redis > HLEN hash_name (integer) 2
9. Hexists method:
HEXISTS key field
Check the hash table key to see if the given domain field exists.
Redis > HEXISTS phone myphone (integer) 0redis > HSET phone myphone nokia-1110 (integer) 1redis > HEXISTS phone myphone (integer) 1
9. Hincrby method:
INCRBY key field increment
Add incremental increment to the value of the field field in the hash table key.
The increment can also be negative, which is equivalent to a subtraction of a given field.
If key does not exist, a new hash table is created and the HINCRBY command is executed.
If the domain field does not exist, the value of the domain is initialized to 0 before the command is executed.
Executing the HINCRBY command on a field field that stores string values will cause an error.
The value of this operation is limited to a 64-bit (bit) signed numeric representation.
# case 1:increment is positive redis > HEXISTS counter page_view # set airspace (integer) 0redis > HINCRBY counter page_view 200 (integer) 200redis > HGET counter page_view "200" # case 2:increment is negative redis > HGET counter page_view "200" redis > HINCRBY counter page_view-50 (integer) 150redis > HGET counter page_view "150" # case 3: try to execute the HINCRBY command redis > HSET myhash string hello on the field with string values World # set a string value (integer) 1redis > HGET myhash string "hello,world" redis > HINCRBY myhash string 1 # command failed Mistake. (error) ERR hash value is not an integerredis > HGET myhash string # original value unchanged "hello,world"
10. Hkeys method:
HKEYS key
Returns all fields in the hash table key.
# case 1: non-empty hash table redis > HMSET website google www.google.com yahoo www.yahoo.comOKredis > HKEYS website1) "google" 2) "yahoo" # case 2: empty hash table / key does not exist redis > EXISTS fake_key (integer) 0redis > HKEYS fake_key (empty list or set)
11. Hvals method:
HVALS key
Returns all values in the hash table key.
# case 1: non-empty hash table redis > HMSET website google www.google.com yahoo www.yahoo.comOKredis > HVALS website1) "www.google.com" 2) "www.yahoo.com" # case 2: empty hash table / keyredis that does not exist > EXISTS not_exists (integer) 0redis > HVALS not_exists (empty list or set)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.