如何使用Redis批量插入? [英] How to use Redis mass insertion?
问题描述
我已经阅读了 redis.io 提供的 mass-insert,但它真的让我感到困惑.我尝试制作一个文件,然后使用cat data.txt | redis-cli --pipe"插入:
I've read mass-insert provided at redis.io, but it really confused me. I tried to make a file then use "cat data.txt | redis-cli --pipe" to insert:
SET Key0 Value0
SET Key1 Value1
SET Key2 Value3
然后我得到了这个:
Then I got this:
All data transferred. Waiting for the last reply...
ERR wrong number of arguments for 'set' command
ERR unknown command '$4'
ERR wrong number of arguments for 'echo' command
ERR unknown command '$20'
我也试过
I also tried
*3<cr><lf>
$3<cr><lf>
SET<cr><lf>
$3<cr><lf>
key<cr><lf>
$5<cr><lf>
value<cr><lf>
然后我得到了这个:ERR 协议错误:无效的多块长度
Then I got this: ERR Protocol error: invalid multi bulk length
这真的让我很困惑.谁能给我一个简单的例子?非常感谢.
It really make me confused. Can anyone give me a simple example? Thank you very much.
推荐答案
这里是:
echo -n '*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n' | ./redis-cli --pipe
All data transferred. Waiting for the last reply...
Last reply received from server.
errors: 0, replies: 1
您的问题可能来自 cr+lf 分隔符.您可以使用 hexdump -C 命令来检查这一点:
Your problem probably comes from the cr+lf separators. You can use the hexdump -C command to check this point:
echo -n '*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n' | hexdump -C
00000000 2a 33 0d 0a 24 33 0d 0a 73 65 74 0d 0a 24 33 0d |*3..$3..set..$3.|
00000010 0a 6b 65 79 0a 0d 24 35 0d 0a 76 61 6c 75 65 0d |.key..$5..value.|
00000020 0a |.|
00000021
此外,您可能需要检查您的目标是最近的 Redis 实例,而不是 pre-1-2 版本(不支持统一协议").
Also, you may want to check your target is a recent Redis instance and not a pre-1-2 version (which does not support the "unified protocol").
注意:以上几行在 zsh 上运行良好.如果使用 bash,则需要在引用前添加 $ 以触发 ANSI-C 引用:
Note: the above lines works fine with zsh. If you use bash, you need to add a $ before the quote to trigger ANSI-C quoting:
echo -n $'*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n' | hexdump -C
这篇关于如何使用Redis批量插入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!