如何使用Redis大量插入?

wyp*_*wyp 15 redis

我读过redis.io提供的mass-insert,但它让我很困惑.我试着制作一个文件,然后用"cat data.txt | redis-cli --pipe"插入:

    SET Key0 Value0
    SET Key1 Value1
    SET Key2 Value3
Run Code Online (Sandbox Code Playgroud)

然后我得到了这个:

    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'
Run Code Online (Sandbox Code Playgroud)

我也试过了

    *3<cr><lf>
    $3<cr><lf>
    SET<cr><lf>
    $3<cr><lf>
    key<cr><lf>
    $5<cr><lf>
    value<cr><lf>
Run Code Online (Sandbox Code Playgroud)

然后我得到了这个:ERR协议错误:多个批量长度无效

这让我很困惑.谁能给我一个简单的例子?非常感谢你.

Did*_*zia 7

这里是:

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
Run Code Online (Sandbox Code Playgroud)

您的问题可能来自cr + lf分隔符.您可以使用hexdump -C命令来检查这一点:

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
Run Code Online (Sandbox Code Playgroud)

此外,您可能希望检查目标是最近的Redis实例而不是1-2之前的版本(不支持" 统一协议 ").

注意:以上行与zsh一起正常工作.如果使用bash,则需要在引号前添加$以触发ANSI-C引用:

echo -n $'*3\r\n$3\r\nset\r\n$3\r\nkey\r\n$5\r\nvalue\r\n' | hexdump -C
Run Code Online (Sandbox Code Playgroud)


小智 5

你可以这样做:

echo -e "$(cat data.txt)" | redis-cli --pipe
Run Code Online (Sandbox Code Playgroud)

我希望对你有帮助!