In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what are the commands related to database collection?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Properties of sets: uniqueness, disorder, certainty
Disorder, for example: {1pm 2} = {2jue 1}
Uniqueness, each value is unique
Example: insert five values of "python", "golang", "python", "c" and "java", but in fact, the number 4 returned by Redis means that only four values have actually been inserted. This is because there are two "python", and the collection automatically filters the second "python".
Note: in string and link commands, you can access certain characters or elements in string through range.
However, because of the disorder of the collection, some elements cannot be accessed through subscript or scope.
So if you want to see the elements, either randomly start with one or select all of them.
Sadd key value1 value2
Function: add elements to the collection key
The sadd command returns "0" when the data already exists, inserts the data and returns 1 if it does not exist. Therefore, this command can determine whether the data exists by the number returned.
Srem value1 value2
Purpose: delete the elements in the collection that are set as value1 value2
Return value: the number of elements that are actually deleted after ignoring elements that do not exist
Spop key
Function: returns and deletes a random element in the key in the collection
Random-- reflects the disorder
(spop will pop up / delete the elements in the key, which can be used to draw the first, second and third place.)
Srandmember key
Function: returns 1 random element in the set key.
(unlike spop, elements in key are not popped / deleted)
Sismember key value
Function: to determine whether value is in the key collection
Yes, return 1, no return 0
Smembers key
Function: returns all the elements in the set
Note:
The smembers command does not delete the data. However, if there is a large amount of data in the collection, you should use "get all data" with caution, as this will cause the system's Igamo resources to be exhausted instantly.
Scard key
Function: returns the number of elements in the collection
Smove source dest value
Purpose: remove the value from source and add it to the dest collection
Sinter key1 key2 key3
Function: find the intersection of the three sets of key1 key2 key3 and return
Redis 127.0.0.1 6379 > sadd s1 0 2 4 6
(integer) 4
Redis 127.0.0.1 6379 > sadd s2 12 3 4
(integer) 4
Redis 127.0.0.1 6379 > sadd s3 4 8 9 12
(integer) 4
Redis 127.0.0.1 6379 > sinter S1 S2 S3
1) "4"
Redis 127.0.0.1 6379 > sinter s3 S1 S2
1) "4"
Sinterstore dest key1 key2 key3
Function: find the intersection of the three sets of key1 key2 key3 and assign it to dest
Suion key1 key2.. Keyn
Function: find the union of key1 key2 keyn and return
Sdiff key1 key2 key3
Function: find the difference between key1 and key2 key3
That is key1-key2-key3
Application of the collection:
In a project, collections of Redis generally serve two purposes:
(I) de-duplicating and recording information according to the non-repetitive data in the set.
(2) use multiple sets to calculate intersection, union and difference sets.
Suppose that in order to do a real-time monitoring system for students' course selection, you need to know the following data in real time:
(I) how many students have taken at least one course at present?
(2) how many students take course A but not course B.
(3) how many students have taken both An and B courses.
(4) how many students have taken at least one of the two courses An and B?
You can easily do this with collections. As a collection, the value of each course is the value of each student.
The student number, as shown in figure 5-65:
Implement with python:
Import redisclient = redis.Redis (host= "xx.xx.xx.xx') all_class = ['algorithm','computer','history','circuit_design','math'] def all_student (): students = client.sunion (* all_class) return len (students) def in_a_and_in_b (class_a,class_b): students = client.sinter (class_a,class_b) return len (students) def in_a_not_in_b (class_a) Class_b): students= client.sdiff (classa,classb) return len (students) def in_a_or_in_b (class_a,class_b): students= client.sunion (classa,classb) return len (students) def in_a_or_in_b (class_a,class_b): students= client.sunion (class_a) Class_b) return len (students) / / took at least one course: all_student () / / students who chose math but not computer: in_a_not_in_b ("math", "computer") / / students who chose math and computer: in_a_and_in_b ("math", "computer") / / students who took math or computer: in_a_or_in_b ("math") "computer") "what are the commands related to database collections"? that's it. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.