Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis and escaping binary data

Tags:

redis

binary

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?

like image 341
user1978109 Avatar asked Aug 29 '14 17:08

user1978109


3 Answers

Arbitrary bytes can be input in redis-cli using hexadecimal notation, e.g.

set "\x00\xAB\x20" "some value"
like image 119
ghik Avatar answered Oct 20 '22 00:10

ghik


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
like image 20
Tw Bert Avatar answered Oct 20 '22 02:10

Tw Bert


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>
like image 21
bmalec Avatar answered Oct 20 '22 01:10

bmalec