網站首頁 編程語言 正文
注:以String類型為例
一.導出redis某個庫的數據
import redis import json file_path = "why.json" redis_conn = redis.Redis(host="192.168.1.123", port=6387, password="123zxcv", db=2, decode_responses=True) data_keys = redis_conn.keys() all_data = {} for i in data_keys: ? ? all_data[i] = json.loads(redis_conn.get(i)) file_object = open(file_path, 'w', encoding="utf8") json.dump(all_data, file_object, ensure_ascii=False) file_object.close()
使用python向Redis批量導入數據
使用pipeline進行批量導入數據。包含先使用rpush插入數據,然后使用expire改動過期時間
class Redis_Handler(Handler): ?? ?def connect(self): ?? ??? ?#print self.host,self.port,self.table ?? ??? ?self.conn = Connection(self.host,self.port,self.table)?? ? ?? ??? ? ?? ?def execute(self, action_name): ?? ??? ?filename = "/tmp/temp.txt" ?? ??? ?batch_size = 10000 ?? ??? ?with open(filename) as file: ?? ??? ??? ?try: ?? ??? ??? ??? ?count = 0 ?? ??? ??? ??? ?pipeline_redis = self.conn.client.pipeline() ?? ??? ??? ??? ?for lines in file: ?? ??? ??? ??? ??? ?(key,value) = lines.split(',') ?? ??? ??? ??? ??? ??? ?count = count + 1 ?? ??? ??? ??? ??? ??? ?if len(key)>0: ?? ??? ??? ??? ??? ??? ??? ?pipeline_redis.rpush(key,value.strip()) ?? ??? ??? ??? ??? ??? ??? ?if not count % batch_size: ?? ??? ??? ??? ??? ??? ??? ??? ?pipeline_redis.execute() ?? ??? ??? ??? ??? ??? ??? ??? ?count = 0 ?? ??? ??? ? ?? ? ?? ??? ??? ??? ?#send the last batch ?? ??? ??? ??? ?pipeline_redis.execute() ?? ??? ??? ?except Exception: ?? ??? ??? ??? ?print 'redis add error'
二.導入redis某個庫的數據
import redis import json file_path = "why.json" redis_conn = redis.Redis(host="192.168.1.123", port=6387, password="123zxcv", db=1, decode_responses=True) file_object = open(file_path, 'r', encoding="utf8") all_data = json.load(file_object) for key in all_data: ? ? redis_conn.set(key, json.dumps(all_data[key], ensure_ascii=False)) file_object.close()
原文鏈接:https://blog.csdn.net/xyy1028/article/details/122706544
相關推薦
- 2023-04-23 Go之接口型函數用法_Golang
- 2022-12-27 教你使用Psycopg2連接openGauss的方法_python
- 2022-03-24 Android實現旋轉動畫_Android
- 2022-12-06 Docker基礎和常用命令詳解_docker
- 2022-11-05 ASP.NET?MVC實現樹形導航菜單_實用技巧
- 2021-11-26 Linux下查看IP地址不顯示解決辦法_Linux
- 2022-02-12 前后端的身份認證--cookie--session--jwt--token
- 2023-01-07 Python使用TextRank算法提取關鍵詞_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支