Redis and escaping binary data

I'm having a hard time understanding how to use binary data types with redis. I want to use the command

   set '{binary data}' 'Alex'

      

what if the binary data actually includes a quote character or / r / n? I know I can get away from the characters, but is there an official list of characters that I need to run?

+4


source to share


7 replies


There is no need to do anything special with the data itself. All Redis strings are binary.

Your problem is with the redis-cli (this is a very good redis client to familiarize yourself with Redis, but almost never what you want in production due to usage and performance issues).

Your problem also relates to general (bash / sh / other) terminal escaping. Here's a good explanation.



I suggest you use python for this or whatever 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

      

+3


source


You can send command as array of massive strings in Redis, no need to escape characters or Base64 encoding. Since bulky strings start at the length of the data, Redis doesn't try to parse the data bytes and instead just goes to the end to check for 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>

      

+1


source


Arbitrary bytes can be entered redis-cli

using hexadecimal notation, for example

set "\x00\xAB\x20" "some value"

      

+1


source


I found it better to use the Redis protocol for this, since the boundaries can be defined before the datatype.

0


source


0


source


Found the answer: Redis says it is binary safe strings

You need to use Base64 on binary data to get a binary safe string.

Java converts bytes to binary safe string

As for your problem, when trying to pipe your redis text commands to the cli, you probably want to step through the file and avoid single quotes.

-1


source


FastoNoSQL and FastoRedis can handle binary keys without escaping.

binary keys in redis

-1


source







All Articles