p1gd0g
p1gd0g

Reputation: 711

How to set utf-8 character into redis using redis-cli?

I want to set a utf-8 character "\u042e" into redis using redis-cli.

I tried:

127.0.0.1:6379> set test "\u042e"
OK
127.0.0.1:6379> get test
"u042e"

Then I tried:

127.0.0.1:6379> set test '\u042e'
OK
127.0.0.1:6379> get test
"\\u042e"

and

127.0.0.1:6379> set test `\u042e`
OK
127.0.0.1:6379> get test
"`\\u042e`"

But what I want is (which I set from golang):

127.0.0.1:6379> get test
"\u042e"

Even I add --raw, that does not work.

Finally I tried:

127.0.0.1:6379> set test Ю
OK
127.0.0.1:6379> get test
"\xd0\xae"

It seems they are same bytes, but I am not totally sure.

Could I set a utf-8 charater into redis directly using redis-cli?

Upvotes: 5

Views: 9025

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50122

The redis-cli formats the output of commands by default - in your case it prints the bytes that encode the UTF-8 characters escaped, in the form of '\xNN'.

You can override this behavior and have the cli print the raw output with the '--raw' switch, like so:

$ redis-cli --raw
127.0.0.1:6379> SET test Ю
OK
127.0.0.1:6379> GET test
Ю

Upvotes: 14

Related Questions