網(wǎng)站首頁 編程語言 正文
注:以String類型為例
一.導(dǎo)出redis某個(gè)庫的數(shù)據(jù)
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批量導(dǎo)入數(shù)據(jù)
使用pipeline進(jìn)行批量導(dǎo)入數(shù)據(jù)。包含先使用rpush插入數(shù)據(jù),然后使用expire改動(dòng)過期時(shí)間
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'
二.導(dǎo)入redis某個(gè)庫的數(shù)據(jù)
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
相關(guān)推薦
- 2022-09-22 使用 sed 替換字符串中最后一次出現(xiàn)的字符
- 2022-05-12 HarmonyOS 單擊 雙擊 長(zhǎng)按 滑動(dòng)事件
- 2022-04-28 關(guān)于k8s中subpath的使用詳解_云其它
- 2022-07-15 Python中五種實(shí)現(xiàn)字符串反轉(zhuǎn)的方法_python
- 2022-08-01 Flask-Sqlalchemy的基本使用詳解_python
- 2022-03-23 Qt實(shí)現(xiàn)兩個(gè)獨(dú)立窗口的信號(hào)通信_(tái)C 語言
- 2022-06-02 jquery實(shí)現(xiàn)淘寶商品圖片局部放大_jquery
- 2022-08-06 python?sns.countplot()?繪畫條形圖詳情_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支