Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving/Listing all key/value pairs in a Redis db

I'm using an ORM called Ohm in Ruby that works on top of Redis and am curious to find out how the data is actually stored. I was wondering if there is way to list all the keys/values in a Redis db.

Any lead will go a long way in helping me out (I'm basically stuck atm). Thanks in advance!

Update:
A note for others trying this out using redis-cli, use this:

$ redis-cli keys * (press * followed by Ctrl-D) ... (prints a list of keys and exits) $ 

Thanks @antirez and @hellvinz!

like image 378
Jagtesh Chadha Avatar asked Sep 26 '10 17:09

Jagtesh Chadha


People also ask

How do you get all keys value to Redis?

There is no native way of doing this. The Redis command documentation contains no native commands for getting the key and value of multiple keys. The most native way of doing this would be to load a lua script into your redis using the SCRIPT LOAD command or the EVAL command.

Which command is used to obtain all the keys in a database?

The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS. The keys in Redis themselves are stored in a dictionary (aka a hash table).

How do I view Redis data?

A Redis server has 16 databases by default. You can check the actual number by running redis-cli config get databases. In interactive mode, the database number is displayed in the prompt within square braces. For example, 127.0. 0.1:6379[13] shows that the 13th database is in use.

Where are Redis keys stored?

redis is an in-memory, key/value store. Think of it as a dictionary with any number of keys, each of which has a value that can be set or retrieved. However, Redis goes beyond a simple key/value store as it is actually a data structures server, supporting different kinds of values.


2 Answers

You can explore the Redis dataset using the redis-cli tool included in the Redis distribution.

Just start the tool without arguments, then type commands to explore the dataset.

For instance KEYS will list all the keys matching a glob-style pattern, for instance with: keys * you'll see all the keys available.

Then you can use the TYPE command to check what type is a given key, if it's a list you can retrieve the elements inside using LRANGE mykey 0 -1. If it is a Set you'll use instead SMEMBERS mykey and so forth. Check the Redis documentation for a list of all the available commands and how they work.

like image 62
antirez Avatar answered Sep 22 '22 02:09

antirez


From the command line, you can also use the dump command, available since Redis 2.6.0

redis-cli KEYS \* | xargs -n 1 redis-cli dump 

(note that this also works with the get command for earlier versions if you don't mind)

UPDATE (V2.8 or greater): SCAN is a superior alternative to KEYS, in the sense that it does not block the server nor does it consume significant resources. Prefer using it.

like image 36
N.Martignole Avatar answered Sep 21 '22 02:09

N.Martignole