i am having a tough time understanding how to use binary datatypes with redis. I want to use the command
set '{binary data}' 'Alex'
what if the binary data actually includes a quote symbol or /r/n? I know I can escape characters but is there an official list of characters I need to escape?
Arbitrary bytes can be input in redis-cli
using hexadecimal notation, e.g.
set "\x00\xAB\x20" "some value"
There's no need to do anything special with the data itself. All Redis strings are binary safe.
Your problem relates to redis-cli (which is a very nice redis client for getting to know Redis, but almost never what you want in production, because of usage and performance issues).
Your problem also relates to common (bash/sh/other) terminal escaping. Here's a nice explanation.
I suggest you use python for this, or any other language you are comfortable with.
Example:
import redis
cli=redis.Redis('localhost', 6379)
with open('data.txt','rb') as f:
for d in f:
t = d.partition('\t')
cli.set(t[0], t[2].rstrip())
#EOF
You can send the command as an array of bulk strings to Redis, no need to escape characters or Base64 encode. Since bulk strings begin with the data length, Redis doesn't try to parse the data bytes and instead just jumps to the end to verify the terminating CR/LF pair:
*3<crlf>
$3<crlf>SET<crlf>
${binary_key_length}<crlf>{binary_key_data}<crlf>
${binary_data_length}<crlf>{binary_data}<crlf>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With