NoSQL----Redis 2.4--hash

Source: Internet
Author: User

Redis Hash is a string-type field and value mapping table, and hash is particularly useful for storing objects.

1, Hset Method:

Hset key field value

Set the value of the field (field) fields in the hash table key to value.

If the key does not exist, a new hash table is created and the Hset operation is performed.

If the field field already exists in the hash table, the old value will be overwritten.

redis> Hset website google "www.g.cn" #对hash表websit的字段google设置为www. g.cn (integer) 1redis> hset website Google "www. Google.com "# Overwrite an old domain (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 field field already exists, the operation is not valid.

If key does not exist, a new hash table is created and the HSETNX command is executed.

redis> hsetnx nosql key-value-store redis (integer) 1redis> hsetnx nosql key-value-store Redis # operation invalid, domain Key-value-sto Re already exists (integer) 0

3, Hmset Method:

Hmset key field value [field value ...]

Set multiple Field-value (domain-value) pairs to the hash table key at the same time.

This command overwrites a domain that already exists in the hash table.

If the key does not exist, an empty hash table is created and the Hmset operation is performed.


# scenario 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: Type error when redis> SET G 10 # error condition okredis> hmset G name Huangz age (ER ROR) 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 a hash table key.

Returns a nil value if the given domain does not exist in the hash table.

Because the nonexistent key is treated as an empty hash table, a hmget operation on a nonexistent key returns a table with a nil value.


redis> hmset pet dog "Tudou" Cat "Wandou" # Save multiple values at once okredis> hmget pet dog Cat Fake_pet # The Order of return values is the same as the order of incoming parameters. 1) "Tudou" 2) "Wandou" 3) (nil) # Non-existent field returns nil value

6, Hgetall Method:

Hgetall Key

Returns all the fields and values in the hash table key.

In the return value, followed by each domain name (field name) is the value of the domain (value), so the length of the return value is twice times the size of the hash table.

Redis> hset hash_name Jack "Jack Sparrow" (integer) 1redis> hset hash_name Gump "Forrest Gump" (integer) 1redis> HG Etall hash_name1) "Jack" # domain 2) "Jack Sparrow" # value 3) "Gump" # Domain 4) "Forrest Gump" # value

7, Hdel Method:

Hdel key field [field ...]

Deletes one or more specified domains in the hash table key, and the nonexistent fields are ignored.

Note: In the following versions of Redis2.4, Hdel You can delete only a single domain at a time, if you need to delete multiple domains in an atomic time, include the command in the MULTI EXEC inside the block.

# test Data redis> Hgetall abbr1) "a" 2) "Apple" 3) "B" 4) "banana" 5) "C" 6) "cat" 7) "D" 8) "Dog" # Delete single domain redis> Hdel abbr A (inte GER) # Delete a nonexistent domain redis> hdel abbr not-exists-field (integer) 0# Delete multiple domains redis> Hdel abbr b C (integer) 2redis> hgetall A BBR1) "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> HL EN hash_name (integer) 2

9, Hexists Method:


Hexists key Field

View the hash table key for the existence of a given domain field.

redis> hexists phone myphone (integer) 0redis> hset phone myphone nokia-1110 (integer) 1redis> hexists phone Myphon E (integer) 1

9, Hincrby Method:

Incrby key field Increment

Adds an increment increment to the value of the field field in the hash table key.

The increment can also be negative, which is equivalent to subtracting 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 domain field that stores a string value 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 (integer ) 200redis> Hget counter Page_view "200" # Case 2:increment Negative redis> hget counter Page_view "$" redis> Hincrby Counter page_view-50 (integer) 150redis> hget counter Page_view "150" # Scenario 3: Attempt to execute a hincrby command on a string value's domain redis> hset myhash String Hello,world # Sets a String value (integer) 1redis> hget myhash string "Hello,world" redis> Hincrby myhash string 1 # Command execution failed with error. (Error) ERR hash value is not a integerredis> hget myhash string # Original value unchanged "Hello,world"

10, Hkeys Method:


Hkeys Key

Returns all the fields in the hash table key.

# scenario 1: Hash table non-empty 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.


# scenario 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" # scenario 2: Empty hash table/nonexistent keyredis> EXISTS not_exists (integer) 0redis> hvals not_exists (empty list or set)


NoSQL----Redis 2.4--hash

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.