skip to Main Content

I want to create hashes with books info in redis, for instance:

HMSET books key "83-7197-669-0" title "Access. DB desing" price 79.0 publisher "Helion" year 2002
HMSET books key "83-7197-786-7" title "Access XP" price 65.0 publisher "Helion" year 2003

Then I just want to find a book with a key 83-7197-669-0, I tried:

1) HGET books key "83-7197-669-0"
(error) ERR wrong number of arguments for 'hget' command

2) HGETALL books
1) "key"
2) "83-7197-786-7"
3) "title"
4) "Access XP"
5) "price"
6) "65.0"
7) "publisher"
8) "Helion"
9) "year"
10) "2003"

I dont know why, but I see here only the second book ..

Next, I wanted to find a book with a given price, with no success. I dont even know what to try. Any ideas?

2

Answers


  1. HSET in redis is like a MAP of MAP, where the books in your example is the key of outer map, where as the key, title, price, and other fields are like keys of inner map.

    To get book by key, you can set key as HSET name which and use HGET key to get the book by key.

    HMSET books:83-7197-669-0 title "Access. DB desing"  price 79.0 publisher "Helion" year 2002
    
    HGETALL books:83-7197-669-0
    

    should return all the fields of the HSET. I hope this gives you good start to redis Hashes.

    Login or Signup to reply.
  2. As you probably see, you are having all those attributes like "key" and "title" in your Redis hash.

    Solution is simple, just omit the key and store rest of the data in serialized form. Redis is basically key-value store on steroids, so turn the first command to

    HMSET "83-7197-669-0" title <title> price <price> ...

    Depending on your use case, you could also use hset, store everything in books and encode values as a single serialized entry.

    If you are looking way to access Redis through multiple keys, you are likely using a wrong tool. Relational data stores are better for that; Redis is still high-speed key-value store with some nice extra stuff baked in.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search