【问题标题】:How to fix: ERR Protocol error: expected '$', got ' '如何修复:ERR 协议错误:预期为“$”,得到“”
【发布时间】:2019-06-25 01:15:46
【问题描述】:

我正在使用 Redis-3.2.1、Python-3.6、Powershell-v1.0 和 Windows 7。我正在尝试批量插入 Redis。我在 Python 3.6 中使用 RESP 协议创建了一个批量插入文件。当我在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 个字符。

【问题讨论】:

    标签: python-3.x windows powershell redis cat


    【解决方案1】:

    您的文件格式似乎有问题。请创建以下格式的输入文件:

    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 以该格式编写多个键。我从blog.csprabala.com/2016/07/23/… 得到了上面尝试过的python 代码。也许它是为不同版本的 Redis 设计的。
    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 2012-12-25
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 2018-05-12
    相关资源
    最近更新 更多