18

我已经阅读了 redis.io 提供的mass-insert,但这真的让我很困惑。我尝试制作一个文件,然后使用“cat data.txt | redis-cli --pipe”插入:

    SET Key0 Value0
    SET Key1 Value1
    SET Key2 Value3

然后我得到了这个:

    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'

我也试过

    *3<cr><lf>
    $3<cr><lf>
    SET<cr><lf>
    $3<cr><lf>
    key<cr><lf>
    $5<cr><lf>
    value<cr><lf>

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

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

4

3 回答 3

8

这里是:

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 命令检查这一点:

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 实例,而不是 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
于 2012-11-02T12:50:22.383 回答
6

你可以这样做:

echo -e "$(cat data.txt)" | redis-cli --pipe

希望对你有帮助!

于 2014-12-02T03:08:05.763 回答
4

我能够使用SET Key0 Value0表格。

请查看https://stackoverflow.com/a/30511742/2613942

回复是关于LPUSH命令的。它也适用于SET.

总而言之,双引号参数

SET "mykey" "myval"

使用以下命令将文件格式从 unix 更改为 windows unix2dos

unix2dos myfile.txt

然后使用导入

cat myfile.txt | src/redis-cli --pipe

这对我有用。

于 2015-05-28T16:20:31.400 回答