Redis批量插入失败,出现奇怪错误

2024-04-20 08:33:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用管道玩Redis mass import,并拥有以下Python脚本:

mydata = ""
mydata += "*4\r\n$4\r\nHSET\r\n$%d\r\n%s\r\n$%d\r\n%s\r\n$%d\r\n%s\r\n" % (len('myhash'), 'myhash', len('myfield'), 'myfield', len('myvalue'), 'myvalue')

print mydata.rstrip()

它产生:

$ python redis_test.py
*4
$4
HSET
$6
myhash
$7
myfield
$7
myvalue

管道输出:

$ python redis_test.py | redis-cli -h rnode -n 5 --pipe --pipe-timeout 1
All data transferred. Waiting for the last reply...
ERR unknown command '2'
ERR unknown command '$4'
ERR wrong number of arguments for 'echo' command
ERR unknown command '$20'
ERR unknown command '�˛�ƩA\m���pm��X�[�'
No replies for 1 seconds: exiting.
errors: 6, replies: 6

虽然我让它填充数据库,我想知道是什么导致这些错误?你知道吗

rnode:6379[5]> keys *
1) "myhash"
rnode:6379[5]> hgetall myhash
1) "myfield"
2) "myvalue"

Tags: pytestredisforlen管道commandunknown
1条回答
网友
1楼 · 发布于 2024-04-20 08:33:52

回答我自己的问题:我不知道它需要一个最终的CRLF,因此:

print mydata.rstrip()

成为:

print mydata

结果:

$ python redis_test.py | redis-cli -h rnode -n 5  pipe  pipe-timeout 10
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1

相关问题 更多 >