How to use Redis mass insertion?

前端 未结 3 2040
小蘑菇
小蘑菇 2020-12-17 10:53

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:

           


        
相关标签:
3条回答
  • 2020-12-17 11:23

    You can do it like this:

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

    I hope that helps you!

    0 讨论(0)
  • 2020-12-17 11:35

    Here it is:

    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
    

    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
    

    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").

    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
    
    0 讨论(0)
  • 2020-12-17 11:42

    I was able to work with the SET Key0 Value0 form.

    Please have a look at https://stackoverflow.com/a/30511742/2613942

    The reply is about the LPUSH command. It also works fine with SET.

    To summarize, double-quote the parameters

    SET "mykey" "myval"
    

    Change the format of the file from unix to windows with unix2dos:

    unix2dos myfile.txt
    

    Then import using

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

    That worked for me.

    0 讨论(0)
提交回复
热议问题