Python使用keys() 獲取 Redis 數(shù)據(jù)庫中的所有鍵
如果你了解 JSON,就會熟悉 Redis 設(shè)計系統(tǒng)。 它使用鍵值結(jié)構(gòu)和分布式內(nèi)存方法來實現(xiàn)彈性數(shù)據(jù)庫。
哈希、列表、集合、排序集合、字符串、JSON 和流是 Redis 支持的眾多數(shù)據(jù)結(jié)構(gòu)之一。 這個開源數(shù)據(jù)庫支持不同的語言,包括 Python,如果您正在使用它開發(fā)后端系統(tǒng),一些模塊和包可以提供幫助。
您經(jīng)常對數(shù)據(jù)庫執(zhí)行的許多操作之一是檢索數(shù)據(jù),在像 Redis 這樣的數(shù)據(jù)庫中,鍵對于實現(xiàn)此類操作很重要。
本文將討論獲取 Redis 數(shù)據(jù)庫中的所有鍵。
使用 keys() 獲取 Redis 數(shù)據(jù)庫中的所有鍵
要使用 redis,我們需要安裝它; 您可以查看 Redis 下載頁面以了解操作方法。 對于 Linux 和 macOS 用戶來說,這要容易得多; 但是,對于 Windows 用戶,您可能必須使用適用于 Linux 的 Windows 子系統(tǒng) (WSL2),并且您可以按照他們的說明視頻指南進(jìn)行操作。
假設(shè)您已經(jīng)設(shè)置了 Redis 數(shù)據(jù)庫,我們將安裝 redis 包,它提供對 Redis 數(shù)據(jù)庫的客戶端訪問。 要安裝它,我們將使用 pip 命令。
pip install redis
輸出:
Collecting redis
Downloading redis-4.3.4-py3-none-any.whl (246 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 246.2/246.2 kB 794.4 kB/s eta 0:00:00
Collecting deprecated>=1.2.3
Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting async-timeout>=4.0.2
Downloading async_timeout-4.0.2-py3-none-any.whl (5.8 kB)
Collecting packaging>=20.4
Downloading packaging-21.3-py3-none-any.whl (40 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 kB 1.9 MB/s eta 0:00:00
Collecting wrapt<2,>=1.10
Downloading wrapt-1.14.1-cp310-cp310-win_amd64.whl (35 kB)
Collecting pyparsing!=3.0.5,>=2.0.2
Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.3/98.3 kB 624.8 kB/s eta 0:00:00
Installing collected packages: wrapt, pyparsing, async-timeout, packaging, deprecated, redis
Successfully installed async-timeout-4.0.2 deprecated-1.2.13 packaging-21.3 pyparsing-3.0.9 redis-4.3.4 wrapt-1.14.1
因此,redis 包使用 wrapt、pyparsing、async-timeout、打包和不推薦使用的模塊來為其模塊提供支持。
要使用 redis 包,我們需要導(dǎo)入它。
import redis
進(jìn)入正題,我們可以使用redis模塊提供的keys()方法來訪問并獲取其中的所有key。
key() 方法從給定的 Redis 數(shù)據(jù)庫返回一個與在其參數(shù)中傳遞的模式相匹配的鍵列表。 如果不傳遞任何參數(shù),則有一個默認(rèn)模式,即 * ,表示所有鍵。
為了展示工作中的方法,我們使用 +Key 按鈕或以下代碼手動預(yù)填充了別名 Temp 和一些鍵的 redis 數(shù)據(jù)庫。
import redis
redisHost = 'localhost'
redisPort = 6379
redisDecodeRes = True
r = redis.StrictRedis(
host=redisHost,
port=redisPort,
decode_responses=redisDecodeRes
)
r.set("ConnectionPool", "Ox1212af34w3141")上面的代碼導(dǎo)致鍵 ConnectionPool 被添加到具有相應(yīng)值的 Temp 數(shù)據(jù)庫中。
set() 方法將鍵值對應(yīng)用于數(shù)據(jù)庫,而 StrictRedis() 方法創(chuàng)建一個 Redis 連接對象,使我們能夠訪問數(shù)據(jù)庫。
要通過 GUI 顯示數(shù)據(jù)庫(使用別名 Temp)及其密鑰,我們可以使用 RedisInsight 應(yīng)用程序,如圖所示。

手動向數(shù)據(jù)庫中添加了 11 個密鑰以測試 key() 方法。
現(xiàn)在,對于 key() 方法,我們必須使用 StrictRedis() 方法創(chuàng)建一個 Redis 連接對象來訪問鍵。 host、port 和 decode_responses 參數(shù)被傳遞來定義連接的參數(shù)。
host 和 port 定義主機(jī)名和端口號,decode_responses 定義將傳遞的數(shù)據(jù)解碼為我們可以輕松使用的 Python 字符串。 keys() 方法然后訪問所有可用的鍵,因為沒有傳遞任何參數(shù)。
import redis
redisHost = 'localhost'
redisPort = 6379
redisDecodeRes = True
r = redis.StrictRedis(
host=redisHost,
port=redisPort,
decode_responses=redisDecodeRes,
db=0
)
print(r.keys())輸出:
['bar-two', 'information', 'bar-one', 'details', 'foo', 'jinku', 'bar', 'User-One', 'delft', 'bar-three', 'ConnectionPool']
我們在 Temp 數(shù)據(jù)庫中有一個所有鍵的列表,我們可以使用它。
如果我們有所需的鍵模式,我們可以將其作為參數(shù)傳遞。 讓我們列出所有以 bar 開頭的鍵。
print(r.keys(pattern="bar*"))
輸出:
['bar-two', 'bar-one', 'bar', 'bar-three']
使用 scan_iter() 獲取 Redis 數(shù)據(jù)庫中的所有鍵
對于大型數(shù)據(jù)庫, scan_iter() 允許我們在 Python 應(yīng)用程序中更好地管理數(shù)據(jù)。 此外, key() 方法會阻塞服務(wù)器并阻止其他使用操作,而對于 scan_iter() ,其基于批處理的操作允許其他使用操作。
盡管 keys() 可能更快,但它對于多個基于請求的系統(tǒng)來說并不是很好。
現(xiàn)在,讓我們看看它的實際效果。
import redis
redisHost = 'localhost'
redisPort = 6379
redisDecodeRes = True
try:
r = redis.StrictRedis(
host=redisHost,
port=redisPort,
decode_responses=redisDecodeRes,
db=0
)
for key in r.scan_iter(match="bar*"):
print(key)
except Exception as e:
print(e)輸出:
bar-three
bar-one
bar-two
bar
當(dāng)我們嘗試使用數(shù)據(jù)庫時,使用 try/except 有助于處理連接問題。 使用 StrictRedis() 連接后,我們使用 match 參數(shù)來定義我們正在尋找的鍵模式,并循環(huán)遍歷結(jié)果以給出鍵。
使用 zip_longest 獲取 Redis 數(shù)據(jù)庫中的所有鍵
正如我們所說,對于具有大量鍵的大型數(shù)據(jù)庫, scan_iter() 方法更好,但我們可以通過按指定數(shù)量的批次檢索鍵來進(jìn)一步改進(jìn)它,以更好地管理結(jié)果。
要創(chuàng)建批處理,我們需要 itertools 模塊,它提供可用于不同情況的不同迭代器(或方法)。 在 itertools 模塊中,我們有 zip_longest 方法,它返回一個 zip_longest 對象,其 .next() 方法返回一個元組并聚合傳遞給它的 iterable 中的元素。
我們可以使用 zip_longest() 方法創(chuàng)建一個函數(shù),該函數(shù)根據(jù)傳遞的參數(shù)創(chuàng)建一批指定數(shù)量的鍵。 比如我們創(chuàng)建一批2,可以用于很多情況。
import redis
from itertools import zip_longest
redisHost = 'localhost'
redisPort = 6379
redisDecodeRes = True
try:
r = redis.StrictRedis(
host=redisHost,
port=redisPort,
decode_responses=redisDecodeRes,
db=0
)
def batch(iterable, num):
initIter = [iter(iterable)] * num
return zip_longest(*initIter)
for keyBatch in batch(r.scan_iter('bar*'), 2):
print(keyBatch)
except Exception as e:
print(e)輸出:
('bar-three', 'bar-one')
('bar-two', 'bar')
到此這篇關(guān)于使用 Python 獲取 Redis 數(shù)據(jù)庫中的所有鍵的文章就介紹到這了,更多相關(guān)Python 獲取 Redis所有鍵內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中的getter與setter及deleter使用示例講解
這篇文章主要介紹了Python中的getter與setter及deleter使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01
Python如何向SQLServer存儲二進(jìn)制圖片
這篇文章主要介紹了Python如何向SQLServer存儲二進(jìn)制圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python中創(chuàng)建數(shù)值列表的4種方法總結(jié)
在Python中列表(List)是一種有序、可變的數(shù)據(jù)類型,被廣泛用于存儲和處理多個元素,這篇文章主要給大家介紹了關(guān)于Python中創(chuàng)建數(shù)值列表的4種方法,需要的朋友可以參考下2024-05-05
python實現(xiàn)將一個數(shù)組逆序輸出的方法
今天小編就為大家分享一篇python實現(xiàn)將一個數(shù)組逆序輸出的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

