我正在使用 Redis-3.2.1、Python-3.6、Powershell-v1.0 和 Windows 7。我正在尝试对 Redis 执行批量插入。我在 Python 3.6 中使用 RESP 协议(protocol)创建了一个批量插入文件。当我在Powershell中执行“cat redis_data.txt | redis-cli --pipe”时,出现以下错误:
PS C:\Users\shiner> cat redis_data.txt | redis-cli --pipe
ERR Protocol error: expected '$', got ' '
All data transferred. Waiting for the last reply...
No replies for 30 seconds: exiting.
errors: 2, replies: 1
(我是 powershell 的新手所以请记住这一点)
这是我的python代码:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
import sys
def gen_redis_proto(*args):
proto = ""
proto += "*"+str(args.__len__())+"\r\n"
for arg in args:
proto += "$"+str(str(arg).__len__())+"\r\n"
proto += str(arg)+"\r\n"
return proto
def generate_data_file():
f = open('redis_data.txt', 'w')
[f.write(gen_redis_proto("SET", "KEY{0}".format(x),
"VALUE{0}".format(x)))
for x in range(0, 400)]
generate_data_file()
文本文件的示例如下所示:
"*3$3SET$4KEY0$6VALUE0*3$3SET$4KEY1$6VALUE1*3$3SET$4KEY2$6VALUE2*3$3SET$4KEY3$6VALUE3*3$3SET$4KEY4$6VALUE4*3$3SET$4KEY5 $6VALUE5*3$3SET$4KEY6$6VALUE6*3$3SET$4KEY7$6VALUE7*3$3SET$4KEY8$6VALUE8*3$3SET$4KEY9$6VALUE9*3$3SET$5KEY10$7VALUE10*3$3SET$5KEY11$7VALUE11 *3$3SET$5KEY12$7VALUE12*3$3SET$5KEY13$7VALUE13*3$3SET$5KEY14$7VALUE14...Value399'
'Value399' 是文本文件中的最后 8 个字符。
最佳答案
您的文件格式似乎有问题。请创建一个具有以下格式的输入文件:
SET Key0 Value0
SET Key1 Value1
...
SET KeyN ValueN
上述 PowerShell 命令对我来说工作正常。 输出:
PS C:\> cat redis_data.txt | redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 2
关于python-3.x - 如何修复 : ERR Protocol error: expected '$' , 得到 ' ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54462929/