In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use the Redis data types string and Hash. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
String type command operation
String type is the most commonly used data type in Redis, and it is also the most commonly used or even only used data type in daily development by many programmers. As a result, many people only know how to use string type when using Redis, while ignoring the importance of other data types. So I also hope that after learning this article, you will have a new understanding of the data types in Redis, and don't just use String types for Redis operations!
Note: before operating in the client of Redis, make sure that the server interface of Redis is open, otherwise the client will refuse access or cannot open it.
Sets the value of the specified key
The method of specifying key for data settings of type string in Redis is the most basic way to set key-value.
The syntax is as follows:
SET key value key is the index value is the corresponding value
For example, we want to set the key-value pair with the index K3 and the value v3.
127.0.0.1 6379 > SET K3 v3OK
If the return value is ok, the setting is successful!
Gets the value of the specified key
If we have set up the data in Redis, we can get it through the key of the data. The syntax format is as follows:
GET key key is the index corresponding to the value to be obtained
For example, we get the data whose index is K3 and the value is v3. If the index exists, it will return the corresponding value. If it does not exist, it will return (nil), which means null.
127.0.0.1 GET 6379 > nil K3 "v3" 127.0.0.1 nil returns a substring of the string value in key
The purpose of this command is to get a substring of a string that already exists in Redis, and we can specify a subscript index to specify which character to get. The format of the command is as follows:
GETRANGE key start end key is the index of string start is the starting subscript of truncation end is the terminating subscript of truncation
For example, we intercept the substring of data with index "mykey" and value "huixiaoyuan"
127.0.0.1 ixiaoyuan 6379 > GETRANGE mykey 2 5 "ixia" 127.0.0.1 GETRANGE mykey 2-1 "ixiaoyuan" 13379 > GETRANGE mykey 1 100 "uixiaoyuan"
Explain the above code, because the starting subscript of the string is 0, so the second corresponding character is "I". If you want to intercept the substring from a certain character to the last part of the string, you can directly set the last parameter to-1.
If you set the get substring range is relatively large, has exceeded the original length of the string, then only return to the last character of the string!
Get the values of multiple given key
Unlike the GET command, the GET command can only obtain the value corresponding to one key, and this command can be used to obtain the values corresponding to multiple key in the following format:
MGET key1 [key2...] Multiple indexes can be appended after MGET, separated by spaces
For example, let's get the corresponding values of mykey, mykey1 and mykey2.
127.0.0.1 value1 6379 > MGET mykey mykey1 mykey21) "huixiaoyuan" 2) "value1" 3) "value2" returns the length of the string corresponding to key
The purpose of this command is to get the length of the string corresponding to the current index, in the following format:
STRLEN key key sets one or more key-value pairs for the index 127.0.0.1 integer 6379 > STRLEN mykey (integer) 11 corresponding to the string
We know that the function of the SET command is to set a key-value pair, but what if there are multiple key-value pairs set at the same time? At this point, you can use this command in the following format:
MSET key1 value1 [key2 value2...] Key1 value1 is the first key-value pair to be set, followed by a space
As follows, we set three key-value pairs at the same time
127.0.0.1 6379 > MSET K1 v1 K2 v2 K3 v3OK
As opposed to the above command, this command can only be set if key does not exist, otherwise all key-value assignments will fail in the following format:
MSETNX key1 value1 [key2 value2...] Key1 value1 is the first key-value pair to be set, followed by a space
If we set a value for K4 that already exists, 0 is returned, which means the setting failed. If the setting is successful, 1 is returned.
127.0.0.1 MSETNX 6379 > MSETNX K1 v1 K4 v4 (integer) 0127.0.1 purl 6379 > MSETNX K4 v4 K5 v5 (integer) 1 adds one of the values stored in key
This command can add 1 to the value of the characters we store, and then return the added result, but if the character corresponding to the key is not a number, it will return an error message in the following format:
INCR key key is the index corresponding to the number to be added by 1
As follows, if we add 1 to the K8 just set, we will report an error. Reset the key-value pair of a value, and then add 1 to return the corresponding data.
127.0.0.1 ERR value is not an integer or out of range127.0.0.1:6379 6379 > INCR K8 (error) ERR value is not an integer or out of range127.0.0.1:6379 > SET num1 10OK127.0.0.1:6379 > INCR num1 (integer) 11
There is also an INCRBY corresponding to the INCR command, which adds a specified increment to the data corresponding to the specified key, in the following format:
INCRBY key increment key is the index increment is the value to be increased 127.0.0.1 INCRBY num1 6379 > integer 5 (integer) 16 subtracts the value stored in key by one
Since there is an additional operation, it corresponds to a decreasing operation, which is in the following format:
DECR key key is the index corresponding to the number to be added by 1
In the previous step, we added 5 to the corresponding value of num1 to get 16, and now we subtract it by one.
127.0.0.1 6379 > DECR num1 (integer) 15
This command also corresponds to a command that subtracts the specified data in the following format:
DECRBY key increment key appends index decrement to the string of 127.0.0.1 DECRBY num1 6379 > integer 13 to be increased
If the key already exists and is a string, the APPEND command appends the specified value to the end of the original value of the key (value). The appended length is returned in the following format:
APPEND key value key is the index value is the string to be appended
For example, we add "hello" to the end of mykey1-value1
127.0.0.1 APPEND mykey1 hello 6379 > GET mykey1 "value1hello" Hash type
Hash type is a mapping table of field (field) and value (value) of String type. Its function is to store the data objects we define, so to put it simply, it is a case where multiple key-value are stored under a key. To give you a simple Hash structure diagram:
Next, let's briefly talk about a few commands commonly used in Redis for the data type Hash.
Set up an Hash data
The command used to set Hash data is not SET, but HMSET. You can understand that H represents Hash and M represents Map. The format of the command is as follows:
HMSET key fieId1 value1 [fieId2 value2...]
Key is the unique index corresponding to this Hash data
Field is the key of one of the key-value pairs stored below
Value is the value corresponding to the key
For example, if we set key to "myhash", the field stored in it is the three key-value pairs of name-huixiaoyuan, sex-nan and age-3, respectively.
127.0.0.1 6379 > HMSET myhash name huixiaoyuan sex nan age 3OK gets all the fields and values in the specified hash table
The command to view all the fields and values in the specified hash table is HGETALL, which fetches all the data from the hash in the following format
HGETALL key
Where key is the index corresponding to the Hash data
For example, if we look at the hash data just set up,
127.0.1 huixiaoyuan 6379 > HGETALL myhash1) "huixiaoyuan" 2) "sex" 3) "nan" 5) "age" 6) "3" gets the value of the specified field stored in the hash table
The last command is to get all the fields, so now this command only gets the values of the specified fields in the specified hash table. The format of the command is as follows:
HGET key field
Index of key hash table
The field corresponding to the value obtained by field
For example, we get the value corresponding to name in the hash table above.
127.0.0.1 6379 > HGET myhash name "huixiaoyuan" deletes one or more hash table fields
The command to delete one or more hash table fields is HDEL, which deletes the fields specified in the specified hash table 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.
If we want to delete two fields with the index "myhash" in the hash table with the value of "3", the corresponding field of "age" and the value of "nan" and the corresponding field of "sex".
127.0.0.1 HGET myhash name 6379 > HDEL myhash age sex (integer) 2127.0.1 0.1 HGETALL myhash1) "name" 2) "huixiaoyuan" gets the number of fields in the hash table
This command can get the number of fields in the specified hash table, in the following format:
HLEN key
Key is the index of the specified hash table
127.0.1 6379 > HLEN myhash (integer) 1 gets all the fields in the hash table
This command can get all the fields in the specified hash table, but does not return the corresponding values for the fields in the following format:
HKEYS key
Key is the index of the specified hash table
127.0.0.1 HKEYS myhash1 6379) "name" gets all the values in the hash table
The last command gets all the fields in the specified hash table, but does not return the corresponding values, so this command gets all the values instead of returning their corresponding fields in the following format:
HVALS key
Key is the index of the specified hash table
127.0.0.1 6379 > HVALS myhash1) "huixiaoyuan" Thank you for reading! This is the end of this article on "how to use Redis data types string and Hash". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.