This is the 12th day of my participation in the First Challenge 2022

Hello. Hello ah, I am gray little ape, a super will write bug program ape!

In the last article, we introduced the use of common commands for string data in Redis. Today, I will share with you the common command operations for the second of Redis’ five data types: Hash

Hash type

A Hash is a mapping of String fields and values. It can be used to store data objects that we define. Therefore, it is simply a key.

Let’s take a look at some of the commands that Redis uses to Hash data types.

Set a Hash data

The command used to SET Hash data is not SET, but HMSET. H stands for Hash and M stands for Map. The format of this command is as follows:

HMSET key fieId1 value1 [fieId2 value2…]

  • Key is the unique index corresponding to the Hash data
  • Field is the key of a key-value pair stored below
  • Value indicates the value of the key

For example, if the key is set to “myhash”, the field stored in it is name-huixiaoyuan, sex-nan and age-3

127.0.0.1:6379> HMSET myhash name huixiaoyuan sex nan age 3
OK
Copy the code

Gets all the fields and values in the specified hash table

The HGETALL command to view all the fields and values in the specified hash table is HGETALL, which extracts all the data in the hash table in the following format

HGETALL key

  • Key is the index corresponding to the Hash data

Let’s look at the hash data we just set

127.0.0.1:6379> HGETALL myhash
1) "name"
2) "huixiaoyuan"
3) "sex"
4) "nan"
5) "age"
6) "3"
Copy the code

Gets the value of the specified field stored in the hash table

Select * from hash table; select * from hash table; select * from hash table;

HGET key field

  • Key Indicates the index of the hash table
  • Field Specifies the field corresponding to the value obtained

If we get the value of name in the hash table above

127.0.0.1:6379> HGET myhash name
"huixiaoyuan"
Copy the code

Deletes one or more hash table fields

The command to delete one or more hash table fields is HDEL. This command can delete the specified hash table fields and their corresponding values in the following format:

HDEL key field1 [field2…]

  • Key is the index of the specified hash table
  • Field is the field corresponding to the value to be deleted. If you want to delete multiple fields, separate them with Spaces

For example, if we want to delete the hash table whose index is “myHash”, the value is “3”, the corresponding field is “age”, and the value is “nan”, the corresponding field is “sex”.

127.0.0.1:6379> HGET myhash name
"huixiaoyuan"
127.0.0.1:6379> HDEL myhash age sex
(integer) 2
127.0.0.1:6379> HGETALL myhash
1) "name"
2) "huixiaoyuan"
Copy the code

Gets the number of fields in the hash table

To obtain the number of fields in a specified hash table, run the following command:

HLEN key

  • Key is the index of the specified hash table
127.0.0.1:6379> HLEN myhash
(integer) 1
Copy the code

Gets all fields in the hash table

This command is used to obtain all the fields in the specified hash table, but does not return the corresponding values of the fields. The format is as follows:

HKEYS key

  • The key for The index of the specified hash table
127.0.0.1:6379> HKEYS myhash
1) "name"
Copy the code

Gets all the values in the hash table

Select * from hash table; select * from hash table; select * from hash table;

HVALS key

  • The key for The index of the specified hash table
127.0.0.1:6379> HVALS myhash 1)"Copy the code

This is how to use the most common commands for hash data in Redis.

I’m Grey Ape, and I’ll see you next time!