Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get values from Redis using keys which contains spaces?

Tags:

key

redis

Using telnet I type in command line commands like this

get field with spaces
get "field with spaces"
get 'field with spaces'

And all this three return same error.

-ERR wrong number of arguments for 'get' command
like image 855
voroninman Avatar asked May 25 '11 10:05

voroninman


People also ask

How do I get all Redis values?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

Can Redis key have multiple values?

Redis quickly stretched this concept with data types, where a single key could refer to multiple (even millions of) pieces of data.

Does Redis store data in bytes?

Redis Strings Strings is an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding. It can store any data-a string, integer, floating point value, JPEG image, serialized Ruby object, or anything else you want it to carry.

What is Keyspace in Redis?

Redis keyspace notifications allow you to subscribe to PubSub channels. Through the channel, clients receive published events when a Redis command or data alteration occurs. These notifications are useful when an application must respond to changes that occur to the value stored in a particular key or keys.


2 Answers

What version of redis are you using? It works fine for me on 2.2.2 using double quotes

root@this:~# redis-cli
redis> set "test space" hello
OK
redis> get "test space"
"hello"
redis> get 'test space'
(error) ERR wrong number of arguments for 'get' command
redis> 
like image 182
Colum Avatar answered Oct 06 '22 00:10

Colum


If you only have telnet (and not 'redis-cli'), then you need to use the Redis binary-safe unified protocol to use spaces in key names, for example:

telnet localhost 6379
*2
$3
GET
$17
field with spaces
hello (this is Redis answer if "field with spaces" contains value "hello")

Explanation:
*2 = Number of arguments (first arg is "GET" and second is "field with spaces")
$3 = length of first argument ("GET" contains 3 bytes)
$17 = length of second argument ("field with spaces" contains 17 bytes)

More information about Redis binary-safe protocol: http://redis.io/topics/protocol

like image 29
Rodrigo De Almeida Siqueira Avatar answered Oct 06 '22 00:10

Rodrigo De Almeida Siqueira