Reputation: 2118
I'm doing a class project and I have Redis set up with about 150,000 hashes, where the keys are timestamps of the format "2008-08-29T15:09:06.493"
, with the fields Tag
and Title
. I would like to find all keys of the same year with similar tags.
I'm doing KEYS *2008*
to get all hashes with the year 2008
, but I'm not sure how I would check if the hash had a particular tag, and to increment a counter. How could I go about doing this?
Upvotes: 0
Views: 1350
Reputation: 38949
So, three points here:
The way you have your data setup is terribly inefficient for what you're trying to do. What you're asking can be done (see point 3), but if you want to do it with any regularity, I'd strongly advise storing the data differently. If, for example, you want to do a lot of time slicing, A sorted set where the key is the time stamp would be a great way to go.
Never ever use KEYS *
in anything similar to a production environment. I know this is just for a class project, but I'd really recommend getting out of the habit of using KEYS *
for anything except offline ad-hoc digging.
Now that the warnings are out of the way, there are a few ways you can do this. The easiest (but not the most efficient) would be just connecting to your Redis server through a scripting language like Python using something like Redis Py, using HGETALL
for each hash to grab everything, and then filtering things out as desired in Python.
If you have to stick within Redis for doing this, or you want to be more efficient with the query while keeping your current data structures, use a Lua script. You can have it run your KEYS 2008*
statement and then filter through the tags of all the results while keeping a running counter. At the end of the script, you'd just return the counter.
Upvotes: 2