In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "the method of Python Redis data processing". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the method of Python Redis data processing".
1. Preface
Redis:Remote Dictionary Server, namely: remote dictionary service, the underlying Redis is written in C language, is an open source, memory-based NoSql database
Redis is often used in scenarios such as caching data and high-speed read and write because of its far higher performance than other databases and supports the advantages of cluster, distribution and master-slave synchronization.
two。 Prepare for
Let's take installing Redis-Server on CVM Centos 7.8 as an example
First, install the Redis database on the CVM
# download epel repository yum install epel-release# and install redisyum install redis
Then, modify the Redis configuration file through the vim command, open the remote connection, and set the connection password
Configuration file directory: / etc/redis.conf
Bind is changed to 0.0.0.0 to allow public network access
Requirepass sets an access password
# vim / etc/redis.conf# 1, bing changed from 127.0.0.1 to 0.0.0.0, open remote connection bind 0.0.0.0 # 2, set password requirepass 123456
It should be pointed out that in order to ensure the security of CVM data, the password must be strengthened when Redis opens remote access.
Next, start the Redis service, open the firewall and port, and configure the CVM security group
By default, the port number used by the Redis service is 6379
In addition, you need to configure it in the CVM security group to ensure that the Redis database can be connected properly.
# start the Redis service. The default redis port number is 6379systemctl start redis # Open firewall systemctl start firewalld.service# Open port 6379 firewall-cmd-- zone=public-- add-port=6379/tcp-- permanent # configuration takes effect immediately firewall-cmd-- reload
After doing this, we can connect through Redis-CLI or Redis client tools.
Finally, to manipulate Redis using Python, we need to install a dependency using pip
# installation dependency, easy to operate redispip3 install redis3. Actual combat
Before we can manipulate the data in Redis, we need to instantiate a Redis connection object with Host, port number, and password
From redis import Redisclass RedisF (object): def _ _ init__ (self): # instantiate Redis object # decode_responses=True If not, the byte type # host: remote connection address # port:Redis port number # password:Redis authorization password self.redis_obj = Redis (host='139.199.**.**',port=6379,password='123456',decode_responses=True,charset='UTF-8', encoding='UTF-8')
Next, let's take manipulating strings, lists, set collections, zset collections, hash tables and transactions as examples to show how Python manipulates these data.
1. String operation
There are two ways to manipulate a string: set () and mset ()
Where: set () can only save one value at a time. The meaning of the parameter is as follows.
Name:key, which stands for key
Value:value, the value to be saved
Ex: expiration time (in seconds). If it is not set, it will never expire; otherwise, it will be deleted.
Px: expiration time in milliseconds
Whether the nx/xx:set operation is performed is related to the existence of the name key.
The operation methods for obtaining and deleting values are get (Key) and delete (Key or Keys), respectively.
# set (): single string operation # add a value and set the timeout to 120s self.redis_obj.set ('name',' airpython', ex=120) # get (): get this value print (self.redis_obj.get ('name')) # delete (): delete one or more values self.redis_obj.delete (' name') print (self.redis_obj.get ('name'))
For the setting of multi-valued data, you only need to call the mset () method to form a dictionary of the data to be inserted as a parameter.
Similarly, Redis provides the mget () method, which can get the values of multiple keys at a time.
# mset (): set multiple values self.redis_obj.mset ({"foo": "foo1", "zoo": "zoo1"}) # mget (): get multiple values result = self.redis_obj.mget ("foo", "zoo") print (result)
2. List operation
Redis provides many methods for operation lists, among which the more common ones are as follows:
Lpush/rpush: inserts one or more values into the head or tail of the list, where lpush represents header insertion and rpush represents tail insertion of data
Lset: inserts the value into the corresponding position in the list through the index
Linsert: inserts data before or after a list element
Lindex: gets an element in the list by index, where 0 represents the first element and-1 represents the last element
Lrange: gets the value of the specified area from the list by setting the start and end positions
Llen: gets the length of the list. If the list corresponding to Key does not exist, 0 is returned.
Lpop: removes and returns the first element in the list
Rpop: removes and returns the last element in the list
The example code is as follows:
Def manage_list (self): "" Operation list: return: "" # 1. Add a list and insert a data to the left # Note: you can add multiple elements at a time You can also add self.redis_obj.lpush ('company',' Ali', 'Tencent', 'Baidu') # 2, remove the first element self.redis_obj.lpop ("company") # 3, and insert data self.redis_obj.rpush on the right ('company',' byte jump') one by one. Remove the last element self.redis_obj.rpop ("company") # 5, get the length of the list self.redis_obj.llen ("company") # 6, pass the index Get an element in the list (the second element) print ('the second element in the list is:', self.redis_obj.lindex ("company", 1)) # 7. View all the values print (self.redis_obj.lrange ('company', 0,-1)) in the list according to the range
3. Manipulate the Set collection
Set is an unordered collection of elements, and the elements in the collection cannot be repeated. Redis also provides many methods to manipulate the Set collection.
Among them, the more common methods are as follows:
Sadd: add elements to the collection. Elements that already exist in the collection will be ignored. If the collection does not exist, create a new collection.
Scard: returns the number of collection elements
Smembers: returns all elements in the collection
Srem: removes one or more elements from the collection and ignores them if they do not exist
Sinter: returns the intersection of two sets, and the result is still a set
Sunion: returns the union of two sets
Sdiff: returns the difference between two sets based on the first set parameter
Sunionstore: calculates the union of two sets and saves them to a new set
Sismember: determines whether an element exists in the collection
Spop: randomly deletes an element from the collection and returns
The specific example code is as follows:
Def manage_set (self): "" manipulate the set collection: return: "" self.redis_obj.delete ("fruit") # 1, sadd: add an element to the collection # add an element: banana self.redis_obj.sadd ('fruit',' banana') # and add two elements self.redis_obj.sadd ('fruit',' apple') (oranges) # 2. Number of collection elements print ('number of collection elements:', self.redis_obj.scard ('fruit')) # 3. Remove an element self.redis_obj.srem ("fruit", "oranges") # and define a set self.redis_obj.sadd ("fruit_other", "bananas", "grapes") Grapefruit) # 4. Get the intersection of two sets result = self.redis_obj.sinter ("fruit", "fruit_other") print (type (result)) print ('intersection is:', result) # 5, get the union of two sets result = self.redis_obj.sunion ("fruit", "fruit_other") print (type (result)) print ('union is:', result) # 6, difference Take the first collection as the standard result = self.redis_obj.sdiff ("fruit", "fruit_other") print (type (result)) print ('difference is:', result) # 7, merge and save to the new collection self.redis_obj.sunionstore ("fruit_new", "fruit", "fruit_other") print ('new collection is:' Self.redis_obj.smembers ('fruit_new')) # 8. Determine whether an element exists in the collection result = self.redis_obj.sismember ("fruit", "apple") print (' whether the apple exists in the set', result) # 9, randomly delete an element from the collection Then return result = self.redis_obj.spop ("fruit") print ('deleted elements are:', result) # 3, all elements in the collection result = self.redis_obj.smembers ('fruit') print ("the last fruit collection contains:", result)
4. Manipulate the zset collection
The zset set is ordered compared to the ordinary set set. The elements in the zset set contain values and scores, where scores are used to sort.
Among them, the more common methods are as follows:
Zadd: add elements to the collection, create a new collection if the collection does not exist, and then insert data
Zrange: returns the element value in the collection (excluding scores) through the start and end points; if withscores=True is set, the result will be returned with a score
Zscore: get the score corresponding to an element
Zcard: gets the number of elements in the collection
Zrank: gets the index of the element in the collection
Zrem: delete elements in the collection
Zcount: judge the number of elements with scores in this range by the minimum and maximum values
The practice code is as follows:
Def manage_zset (self): "" operate the zset collection: return: "" self.redis_obj.delete ("fruit") # add elements to the collection: zadd () # three elements are: "banana", 1 / "apple", 2 / "pear", 3 self.redis_obj.zadd ("fruit", "banana", 1, "apple", 2, "pear") 3) # View all elements in the collection (without scores) result = self.redis_obj.zrange ("fruit", 0,-1) # ['banana',' apple', 'pear'] print (' elements in the collection (without scores) are:', result) # View all elements in the collection (with scores) result = self.redis_obj.zrange ("fruit", 0,-1) Withscores=True) # [('banana', 1. 0), (' apple', 2. 0), ('pear', 3. 0)] print (', result) # gets the score of an element in the collection result = self.redis_obj.zscore ("fruit", "apple") print ("apple corresponds to:", result) # through the minimum and maximum values The number of elements whose scores are in this range result = self.redis_obj.zcount ("fruit", 1,2) print ("the score in the set is greater than 1") The number of elements less than 2 are: ", result) # get the number of elements in the collection count = self.redis_obj.zcard (" fruit ") print ('collection element format:', count) # get the value of the element get the index number index = self.redis_obj.zrank (" fruit "," apple ") print ('the index of the apple element is:' Index) # Delete elements in the collection: zrem self.redis_obj.zrem ("fruit", "apple") print ('after deleting the apple element The remaining elements are:', self.redis_obj.zrange ("fruit", 0,-1))
4. Operation hash
The hash table contains many key-value pairs, and each key is unique
Redis manipulates the hash table. The following methods are commonly used:
Hset: add a key value pair to the hash table
Hmset: add multiple key-value pairs to the hash table
Hget: gets the value of a single key in a hash table
Hmget: gets a list of values for multiple keys in a hash table
Hgetall: gets all the key-value pairs in the hash table
Hkeys: gets a list of all the keys in the hash table
Hvals: gets a list of all the values in the hash table
Hexists: determines whether a key exists in the hash table
Hdel: deletes a key-value pair in the hash table
Hlen: returns the number of key-value pairs in the hash table
The corresponding operation code is as follows:
Def manage_hash (self): "" operate hash table: a key corresponds to a value, and the key does not allow repetition: return: "" self.redis_obj.delete ("website") # 1. Create a new hash table with key as website # add data to it: baidu (field) Www.baidu.com (value) self.redis_obj.hset ('website',' baidu', 'www.alibababaidu.com') self.redis_obj.hset (' website', 'google',' www.google.com') # 2. Add multiple key-value pairs to the hash table self.redis_obj.hmset ("website", {"tencent": "www.qq.com") "alibaba": "www.taobao.com"}) # 3. Get the value of a key result = self.redis_obj.hget ("website", 'baidu') print ("the key is baidu:", result) # 4, get the value of multiple keys result = self.redis_obj.hmget ("website", "baidu", "alibaba") print ("the value of multiple keys is:" Result) # 5. View all the values in the hash table result = self.redis_obj.hgetall ('website') print ("all key-value pairs in the hash table are:", result) # 6. List of all keys in the hash table # [' baidu', 'google',' tencent', 'alibaba'] result = self.redis_obj.hkeys ("website") print ("hash table") All keys (list) are: ", result) # 7, list of all values in the hash table # ['www.alibababaidu.com',' www.google.com', 'www.qq.com',' www.taobao.com'] result = self.redis_obj.hvals (" website ") print (" hash table ") All values (list) are: ", result) # 8, determine whether a key exists result = self.redis_obj.hexists (" website "," alibaba ") print ('alibaba exists:', result) # 9, delete a key-value pair self.redis_obj.hdel (" website ", 'baidu') print (' delete baidu key-value pair) The data of the hash table includes:', self.redis_obj.hgetall ('website')) # 10, the number of key-value pairs in the hash table count = self.redis_obj.hlen ("website") print (' hash table key-value pairs altogether:', count)
5. Operate the transaction pipeline
Redis supports transaction pipeline operations and can submit several operations to be executed uniformly.
The steps are as follows:
First, define a transaction pipeline
Then perform a series of operations through the transaction object
Commit the transaction operation and end the transaction operation
The following is illustrated by a simple example:
Def manage_steps (self): "" perform transaction operations: return: "" # 1, define a transaction pipeline self.pip = self.redis_obj.pipeline () # define a series of operations self.pip.set ('age' 18) # add one-year-old self.pip.incr ('age') # decrease one-year-old self.pip.decr (' age') # execute the transaction operation self.pip.execute () defined above () # judge print ('by some of the actions listed above Age becomes:', self.redis_obj.get ('age')) so far, I believe you have a deeper understanding of "the method of Python Redis data processing". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.