Nosql Redis: hash data type and operation commands

Source: Internet
Author: User
Hash type? Is a ing table between fields and values of the string type .? Its Addition and deletion operations are all 0 (1) (average ).? Suitable for storing objects. 1: hset? Description: sets the field value in the key field of the hash table to value. If the specified key does not exist, it is created first. if yes, it overwrites. return: if the field is created in the hash table, 1 is returned. already

Hash type? Is a ing table between fields and values of the string type .? Its Addition and deletion operations are all 0 (1) (average ).? Suitable for storing objects. 1: hset? Description: sets the field value in the key field of the hash table to value. If the specified key does not exist, it is created first. if yes, it overwrites. return: if the field is created in the hash table, 1 is returned. already

Hash type
? It is a string-type field and value ing table.
? Its Addition and deletion operations are all 0 (1) (average ).
? Suitable for storing objects.

1: hset?
Description: sets the field value in the key field of the hash table to value. If the specified key does not exist, it is overwritten when it is created.
Return: if the field is created in the hash table, 1 is returned. 1. The existing field and the old value have been overwritten by the new value. 0 is returned.
The procedure is as follows:
Redis 127.0.0.1: 6379> hset uinfo_1 name tw
(Integer) 1
Redis 127.0.0.1: 6379> hget uinfo_1 name
"Tw"
Redis 127.0.0.1: 6379> hset uinfo_1 name tw2
(Integer) 0
Redis 127.0.0.1: 6379> hget uinfo_1 name
"Tw2 ″
2: hget
Description: return the field value of the given field in the hash table KEY.
Return: if the specified field is returned, nil is returned if the field does not exist.
The procedure is as follows:
Redis 127.0.0.1: 6379> hget user_1 name
(Nil)
Redis 127.0.0.1: 6379> hget uinfo_1 name
"Tw2 ″
3: hsetnx
Description: sets the field value in the key of the hash table to value. If the field does not exist. This operation is invalid if the field already exists. If the key does not exist, a new hash table is created and executed.
Returned value: 1 indicates that the new Field is set with a new value. 0 indicates that the Key or Field already exists. This command does not perform any operation.
The procedure is as follows:
First, view all fields and values in the key.
Redis 127.0.0.1: 6379> hgetall uinfo_1
1) "name"
2) "tw2 ″
3) "age"
4) "19 ″
Redis 127.0.0.1: 6379> hsetnx uinfo_1 sex 1
(Integer) 1
Redis 127.0.0.1: 6379> hsetnx uinfo_1 sex 0
(Integer) 0
Redis 127.0.0.1: 6379> hgetall uinfo_1
1) "name"
2) "tw2 ″
3) "age"
4) "19 ″
5) "sex"
6) "1 ″
Redis 127.0.0.1: 6379> hsetnx uinfo_2 name tw2
(Integer) 1
Redis 127.0.0.1: 6379> hgetall uinfo_2
1) "name"
2) "tw2 ″

?
4: hmset
Description: Submit multiple field-values to the hash table at the same time. This command overwrites the existing fields in the hash table. If the key does not exist, an empty hash table is created and hmset is executed.
Returned value: OK is returned for successful execution. If the key is not of the hash type, an error is returned.
The procedure is as follows:

Redis 127.0.0.1: 6379> set title_1 val_1
OK
Redis 127.0.0.1: 6379> hmset title_1 val_2
(Error) ERR wrong number of arguments for 'hmset' command
Redis 127.0.0.1: 6379> hmset uinfo_1 name aaa age 30
OK
Redis 127.0.0.1: 6379> hgetall uinfo_1
1) "name"
2) "aaa"
3) "age"
4) "30 ″
5) "sex"
6) "1 ″

5: hmet
Description: return the values of one or more given fields in the hash table key. If the given domain does not exist in the hash table, an nil value is returned.
Return Value: return the value of the query domain.
The procedure is as follows:

Redis 127.0.0.1: 6379> hmet uinfo_1 name age
1) "aaa"
2) "30 ″

6: hgetall
Description: all fields and values in the key of the hash table are returned. In the return value, the field name is followed by the value of the field.

(Value), so the return value is twice the size of the hash table.
Returned value: returns the fields and values in the hash table in the form of a list. If the key is not saved, an empty list is returned.
Operation: see the preceding hsetnx.

7: hdel
Description: deletes one or more specified fields in the hash table key. nonexistent fields are ignored.
Returned value: the number of successfully removed fields, excluding the ignored fields.
The procedure is as follows:
Redis 127.0.0.1: 6379> hgetall uinfo_1
1) "name"
2) "aaa"
3) "age"
4) "30 ″
5) "sex"
6) "1 ″
Redis 127.0.0.1: 6379> hdel uinfo_1 name age
(Integer) 2
Redis 127.0.0.1: 6379> hgetall uinfo_1
1) "sex"
2) "1 ″
8: hexists
Description: checks whether the specified field exists in the key of the hash table.
Returned value: 1 is returned if no value exists. 0 is returned if no value exists.
The procedure is as follows:
Redis 127.0.0.1: 6379> hexists uinfo_1 name
(Integer) 0
Redis 127.0.0.1: 6379> hexists uinfo_1 sex
(Integer) 1

9: hincrby
Description: adds an incremental increment to the field value of the hash table key. The increment can also be a negative number, which is equivalent

The row subtraction operation.
Returned value: the field value in the key of the hash table after the HINCRBY command is executed.
The procedure is as follows:
Redis 127.0.0.1: 6379> hgetall uinfo_1
1) "sex"
2) "1 ″
Redis 127.0.0.1: 6379> hincrby uinfo_1 sex 20
(Integer) 21
Redis 127.0.0.1: 6379> hincrby uinfo_1 sex-10
(Integer) 11

10: hlen
Description: returns the number of fields in the hash table key.
Returned value: number of fields in the hash table. If the key does not exist, 0 is returned.

11: hkeys
Description: return all fields in the hash table key.
Returned value: a table that contains all fields in the hash table. If the key does not exist, an empty table is returned.

12, hvals
Description: return all values in the key of the hash table.
Returned value: a table that contains all values in the hash table. If the key does not exist, an empty table is returned.

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.