Python使用keys() 獲取 Redis 數(shù)據(jù)庫中的所有鍵
如果你了解 JSON,就會(huì)熟悉 Redis 設(shè)計(jì)系統(tǒng)。 它使用鍵值結(jié)構(gòu)和分布式內(nèi)存方法來實(shí)現(xiàn)彈性數(shù)據(jù)庫。
哈希、列表、集合、排序集合、字符串、JSON 和流是 Redis 支持的眾多數(shù)據(jù)結(jié)構(gòu)之一。 這個(gè)開源數(shù)據(jù)庫支持不同的語言,包括 Python,如果您正在使用它開發(fā)后端系統(tǒng),一些模塊和包可以提供幫助。
您經(jīng)常對(duì)數(shù)據(jù)庫執(zhí)行的許多操作之一是檢索數(shù)據(jù),在像 Redis 這樣的數(shù)據(jù)庫中,鍵對(duì)于實(shí)現(xiàn)此類操作很重要。
本文將討論獲取 Redis 數(shù)據(jù)庫中的所有鍵。
使用 keys() 獲取 Redis 數(shù)據(jù)庫中的所有鍵
要使用 redis,我們需要安裝它; 您可以查看 Redis 下載頁面以了解操作方法。 對(duì)于 Linux 和 macOS 用戶來說,這要容易得多; 但是,對(duì)于 Windows 用戶,您可能必須使用適用于 Linux 的 Windows 子系統(tǒng) (WSL2),并且您可以按照他們的說明視頻指南進(jìn)行操作。
假設(shè)您已經(jīng)設(shè)置了 Redis 數(shù)據(jù)庫,我們將安裝 redis 包,它提供對(duì) 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ù)庫返回一個(gè)與在其參數(shù)中傳遞的模式相匹配的鍵列表。 如果不傳遞任何參數(shù),則有一個(gè)默認(rèn)模式,即 *
,表示所有鍵。
為了展示工作中的方法,我們使用 +Key 按鈕或以下代碼手動(dòng)預(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()
方法將鍵值對(duì)應(yīng)用于數(shù)據(jù)庫,而 StrictRedis()
方法創(chuàng)建一個(gè) Redis 連接對(duì)象,使我們能夠訪問數(shù)據(jù)庫。
要通過 GUI 顯示數(shù)據(jù)庫(使用別名 Temp)及其密鑰,我們可以使用 RedisInsight 應(yīng)用程序,如圖所示。
手動(dòng)向數(shù)據(jù)庫中添加了 11 個(gè)密鑰以測(cè)試 key()
方法。
現(xiàn)在,對(duì)于 key()
方法,我們必須使用 StrictRedis()
方法創(chuàng)建一個(gè) Redis 連接對(duì)象來訪問鍵。 host、port 和 decode_responses 參數(shù)被傳遞來定義連接的參數(shù)。
host 和 port 定義主機(jī)名和端口號(hào),decode_responses 定義將傳遞的數(shù)據(jù)解碼為我們可以輕松使用的 Python 字符串。 keys()
方法然后訪問所有可用的鍵,因?yàn)闆]有傳遞任何參數(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']
我們?cè)?Temp 數(shù)據(jù)庫中有一個(gè)所有鍵的列表,我們可以使用它。
如果我們有所需的鍵模式,我們可以將其作為參數(shù)傳遞。 讓我們列出所有以 bar 開頭的鍵。
print(r.keys(pattern="bar*"))
輸出:
['bar-two', 'bar-one', 'bar', 'bar-three']
使用 scan_iter() 獲取 Redis 數(shù)據(jù)庫中的所有鍵
對(duì)于大型數(shù)據(jù)庫, scan_iter()
允許我們?cè)?Python 應(yīng)用程序中更好地管理數(shù)據(jù)。 此外, key()
方法會(huì)阻塞服務(wù)器并阻止其他使用操作,而對(duì)于 scan_iter()
,其基于批處理的操作允許其他使用操作。
盡管 keys()
可能更快,但它對(duì)于多個(gè)基于請(qǐng)求的系統(tǒng)來說并不是很好。
現(xiàn)在,讓我們看看它的實(shí)際效果。
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ù)庫時(shí),使用 try/except 有助于處理連接問題。 使用 StrictRedis()
連接后,我們使用 match 參數(shù)來定義我們正在尋找的鍵模式,并循環(huán)遍歷結(jié)果以給出鍵。
使用 zip_longest 獲取 Redis 數(shù)據(jù)庫中的所有鍵
正如我們所說,對(duì)于具有大量鍵的大型數(shù)據(jù)庫, scan_iter()
方法更好,但我們可以通過按指定數(shù)量的批次檢索鍵來進(jìn)一步改進(jìn)它,以更好地管理結(jié)果。
要?jiǎng)?chuàng)建批處理,我們需要 itertools 模塊,它提供可用于不同情況的不同迭代器(或方法)。 在 itertools 模塊中,我們有 zip_longest 方法,它返回一個(gè) zip_longest 對(duì)象,其 .next() 方法返回一個(gè)元組并聚合傳遞給它的 iterable 中的元素。
我們可以使用 zip_longest()
方法創(chuàng)建一個(gè)函數(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在matlab中創(chuàng)建類似字典的數(shù)據(jù)結(jié)構(gòu)方式
這篇文章主要介紹了在matlab中創(chuàng)建類似字典的數(shù)據(jù)結(jié)構(gòu)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03解決numpy矩陣相減出現(xiàn)的負(fù)值自動(dòng)轉(zhuǎn)正值的問題
這篇文章主要介紹了解決numpy矩陣相減出現(xiàn)的負(fù)值自動(dòng)轉(zhuǎn)正值的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python實(shí)現(xiàn)PS圖像抽象畫風(fēng)效果的方法
這篇文章主要介紹了Python實(shí)現(xiàn)PS圖像抽象畫風(fēng)效果的方法,涉及Python基于skimage模塊進(jìn)行圖像處理的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01使用python將excel數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫過程詳解
這篇文章主要介紹了使用python將excel數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08linux下python使用sendmail發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了linux下python使用sendmail發(fā)送郵件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05