来直接上脚本(想了解的同学,请自行百度查询学习)
执行脚本前,需要你将文件进行分割,大小自己决定,或者不分也行,你机器贵+好就不用,不然你就等着内存溢出吧。
import redis
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
REDIS_PASSWORD = 'password'
REDIS_DB = 0
# 批量大小,可以根据需要进行调整
BATCH_SIZE = 20000
# ID文件路径
file_path = ['file.txt']
# 连接到Redis
r = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, password=REDIS_PASSWORD)
# 使用管道进行批量操作
pipe = r.pipeline()
for file_path in file_paths:
print(f"正在处理文件: {file_path}")
try:
with open(file_path, 'r') as f:
count = 0
for line in f:
id_ = line.strip() # 去除行尾的换行符
if not id_: # 跳过空行
continue
key = f'{id_}'
pipe.set(key, FIXED_VALUE)
count += 1
# 当达到批量大小时,执行管道中的命令并重置管道
if count % BATCH_SIZE == 0:
pipe.execute()
pipe.reset()
# 重置计数器
count = 0
pipe.execute()
pipe.reset()
except FileNotFoundError:
print(f"文件 {file_path} 不存在,跳过...")
except Exception as e:
print(f"处理文件 {file_path} 时发生错误: {e}")
print("所有键值对已成功写入Redis。")