In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces Redis how to get a prefix of the key script, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
1. Background
In normal maintenance, it is often encountered to count the number of key of a prefix. In redis with more requests, keys * will directly cause blocking.
Scan can be used for incremental iteration, and queries use pipeline to reduce interaction and improve efficiency.
Advantages and disadvantages of 2.scan command
The SCAN command is SCAN,SSCAN,HSCAN,ZSCAN.
SCAN means traversing all the keys.
The other SCAN commands are the collection selected by SCAN.
The SCAN command is an incremental loop that returns only a small portion of the elements per call. So there will be no KEYS commands.
The SCAN command returns a cursor that traverses from 0 to 0.
Scan 01) "655" 2) 1) "test1" 2) "test2"
The return value is an array, a cursorId for the next loop, and an array of elements. The SCAN command does not guarantee that the values returned each time are ordered, and the same key may be returned multiple times without distinction and needs to be processed by the application.
In addition, the SCAN command can specify COUNT, which defaults to 10. But this does not mean that you can return as much as you specify. This is just a hint and there is no guarantee that so many entries will be returned.
Advantages:
Provide key space traversal operation, support cursors, complexity O (1), the whole traversal only needs O (N)
Provide result pattern matching
The number of data entries returned at a time can be set, but it is only a hints. Sometimes more data are returned.
Weak state, all states only need the client to maintain a cursor
Disadvantages:
Unable to provide a complete snapshot traversal, that is, if there are data modifications in the middle, some data involving changes may not be traversed.
The number of data returned each time is not certain, and it is extremely dependent on the internal implementation.
The returned data may be duplicated, and the application layer must be able to handle the reentrant logic
3. Implementation of python script
There is an encapsulated function in python, scan_iter--, to view all the elements-- iterators.
Content of the script:
#! / usr/bin/env python#-*-coding: UTF-8-*-# function: count the number of a prefix key and input it into a file # usage: python scan_redis.py apus* 100 prefix authorship _ = "lcl" import sysimport redis import os pool=redis.ConnectionPool (host='192.168.225.128',port=6379,db=0) r = redis.StrictRedis (connection_pool=pool) # scan matching value Pass parameter match = sys.argv [1] # per match count = sys.argv [2] # print match#print count# Total number total = output key scanned to file path = os.getcwd () # output file txt = path+ "/ keys.txt" f = open (txt, "w") for key in r.scan_iter (match = match,count = count): # f.write ("% s% s"% (key) "\ n") f.write (key+ "\ n") total = total+1f.closeprint "match:% s quantity is:% d"% (match,total) Thank you for reading this article carefully I hope the article "how to get a prefix key script for Redis" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you 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.