使用python批量刪除redis key

 

比如我的業務。剛上線默認爲超級管理員新增權限

-- 請導出id 用於清緩存 svc格式 請注意分頁 需要導出全部
select CONCAT('@rbac/ent/aclgr/',e.id) as 需要清理緩存的rediskey from ent_rbac_group e where
not exists(select p.`groupid` from ent_rbac_group_permission p where e.id=p.`groupid` and p.`targetid`='transfer::FileTransfer') and ( `name` ='超級管理員')



INSERT IGNORE INTO ent_rbac_group_permission (groupid, targetid, targettype, allow)
select e.id, 'transfer::FileTransfer', 1, 1 from ent_rbac_group e where
not exists(select p.`groupid` from ent_rbac_group_permission p where e.id=p.`groupid` and p.`targetid`='transfer::FileTransfer') and ( `name` ='超級管理員');

svc格式

 

py腳本

#!/usr/bin/python
#-*- coding:utf-8 -*-



import redis
import sys
reload(sys)
sys.setdefaultencoding("utf-8")


def redis_delbyfile(filepath):
    host = 'xxx'
    password = 'xxx'
    Pool = redis.ConnectionPool(host=host, port=26379, password=password, socket_connect_timeout=5)
    r = redis.StrictRedis(connection_pool=Pool)
    with open(filepath, 'r') as f:
        contents = f.readlines()
    for content in contents:
        content = str(content.strip('\r\n').strip('\n'))
        print(content)
        result = r.delete(content)
        print(repr(result))


if __name__ == '__main__':
    if len(sys.argv) == 2:
        filepath = sys.argv[1]
        redis_delbyfile(filepath)

1.安裝環境

pip install reload

pip install redis

執行

python3 {腳本path}.py {svcpath}.csv

 

常見問題

安裝python腳本

1.先檢測下是否安裝python:在命令行裏輸入命令

ls -l /usr/bin | grep python

如果沒有則正常安裝

未安裝redis模塊

Traceback (most recent call last):
  File "/Users/liqiang/Desktop/redis_exec.py", line 6, in <module>
    import redis
ModuleNotFoundError: No module named 'redis'

安裝redis模塊

pip install redis

1.如果未安裝pip則先安裝pip

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py

or

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

pip安裝失敗

➜  Desktop python3 get-pip.py                                     
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
    xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-brew-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip.
    
    If you wish to install a non-brew packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

推薦的做法是創建一個虛擬環境來管理您的Python包,而不是嘗試在全局環境中安裝包。

python3 -m venv myenv (這將在當前目錄下創建一個名爲myenv的新虛擬環境。)

source myenv/bin/activate (nix/Linux 激活)

myenv\Scripts\activate (windows 激活)

再執行 

pip install redis 

(myenv) ➜  Desktop pip install redis 
Collecting redis
  Downloading redis-5.0.1-py3-none-any.whl.metadata (8.9 kB)
Downloading redis-5.0.1-py3-none-any.whl (250 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 250.3/250.3 kB 1.1 MB/s eta 0:00:00
Installing collected packages: redis
Successfully installed redis-5.0.1
(myenv) ➜  Desktop 

pyhon3執行

需要刪除 

import importlib
importlib.reload(sys)
sys.setdefaultencoding("utf-8")
 

如何退出和進入環境

要進入和退出虛擬環境(myenv),您需要使用以下命令:

進入虛擬環境:

在終端或命令提示符下,使用以下命令進入虛擬環境:

對於 Unix/Linux 系統:

source myenv/bin/activate

對於 Windows 系統:

myenv\Scripts\activate

退出虛擬環境:

在虛擬環境中工作完成後,您可以通過運行以下命令來退出虛擬環境:

deactivate

這樣就可以退出虛擬環境,並返回到全局環境中。

請注意,在退出虛擬環境之前,您將無法使用deactivate命令。這個命令只在虛擬環境中有效。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章