python導(dǎo)入導(dǎo)出redis數(shù)據(jù)的實(shí)現(xiàn)
注:以String類型為例
一.導(dǎo)出redis某個(gè)庫(kù)的數(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)過(guò)期時(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è)庫(kù)的數(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()
到此這篇關(guān)于python導(dǎo)入導(dǎo)出redis數(shù)據(jù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python導(dǎo)入導(dǎo)出redis內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python操作Redis數(shù)據(jù)庫(kù)的超詳細(xì)教程
- python?實(shí)現(xiàn)?redis?數(shù)據(jù)庫(kù)的操作
- Python?Flask?+?Redis?程序練習(xí)
- 解決python-redis-lock分布式鎖的問(wèn)題
- python3使用python-redis-lock解決并發(fā)計(jì)算問(wèn)題
- python動(dòng)態(tài)網(wǎng)站爬蟲(chóng)實(shí)戰(zhàn)(requests+xpath+demjson+redis)
- Python訪問(wèn)Redis的詳細(xì)操作
- Python利用Redis計(jì)算經(jīng)緯度距離案例
相關(guān)文章
使用Python的datetime庫(kù)處理時(shí)間(RPA流程)
datetime 是 Python 處理日期和時(shí)間的標(biāo)準(zhǔn)庫(kù)。這篇文章主要介紹了使用Python的datetime庫(kù)處理時(shí)間(RPA流程),需要的朋友可以參考下2019-11-11
安裝pytorch時(shí)報(bào)sslerror錯(cuò)誤的解決方案
這篇文章主要介紹了安裝pytorch時(shí)報(bào)sslerror錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
利用python中集合的唯一性實(shí)現(xiàn)去重
scrapy與selenium結(jié)合爬取數(shù)據(jù)(爬取動(dòng)態(tài)網(wǎng)站)的示例代碼
Python實(shí)現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運(yùn)算示例
詳解python數(shù)據(jù)結(jié)構(gòu)之棧stack

