Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get redis hash data via cli

Tags:

redis

I've been experimenting with Redis today. I've managed to store cached values from Drupal, but I'm looking to investigate a bit further and view the value stored in cache_my_custom_cache..

127.0.0.1:6379> keys *
 1) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:lookup_cache"
 2) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:module_implements"
 3) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:bootstrap_modules"
 4) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:system_list"
 5) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:variables"
 6) "ff3169bd93659dc31322abc32835ef3e:path:a:und"
 7) "myhash"
 8) "ff3169bd93659dc31322abc32835ef3e:path:s:und"
 9) "ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup"
10) "ff3169bd93659dc31322abc32835ef3e:cache_bootstrap:hook_info"
127.0.0.1:6379> type ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup
hash
  • this reveals it's of type hash.. But when I run:
127.0.0.1:6379> HGET ff3169bd93659dc31322abc32835ef3e:cache_qbe:qbe_markup
(error) ERR wrong number of arguments for 'hget' command

It doesn't like it! Completely new to this, can someone offer a solution?

like image 376
williamsowen Avatar asked Feb 25 '15 11:02

williamsowen


People also ask

How do I access Redis from command line?

To start Redis client, open the terminal and type the command redis-cli. This will connect to your local server and now you can run any command.

How can I get all my data from Redis?

Redis GET all Keys 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.

What does Redis-cli command do?

The Redis command line interface ( redis-cli ) is a terminal program used to send commands to and read replies from the Redis server.

How do I get Redis Hgetall?

Redis - Hash Hgetall CommandRedis HGETALL command is used to get all the fields and values of the hash stored at the key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.


2 Answers

Besides the key you will need to specify the field in the HGET command.

To get a list of all fields in the hash you can run this:

hkeys ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup

Then you can do:

hget ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup FIELD

You can also get all the values in the hash like this:

hvals ff3169bd93659dc31322abc32835ef3e:cache_my_custom_cache:custom_cache_markup
like image 80
Titi Avatar answered Nov 09 '22 20:11

Titi


HGET expects an additional parameter after the key name that enumerates which field of your hash you would like returned. Something like this:

HGET my_hash_key my_hash_field

If you're trying to retrieve all fields of your hash at once, you should use HGETALL:

HGETALL my_hash_key

Documentation for HGET: here

Documentation for HGETALL: here

Discussion of Redis types (including hashes): here

like image 22
rchang Avatar answered Nov 09 '22 19:11

rchang