Redis - Sorted set, find item by property value -
in redis store objects in sorted set. in solution, it's important able run ranged query dates, store items score being timestamp of each items, example:
# score value 0 1443476076 {"id":"92","ref":"7adt","dtime":1443476076,"atime":1443901554,"extime":0,"spname":"7adt33cfsau6","stpname":"7adt33cfsau6"} 1 1443482969 {"id":"11","ref":"dajt","dtime":1443482969,"atime":1443901326,"extime":0,"spname":"dajtjtt4t02o","stpname":"dajtjtt4t02o"}
however, in other situations need find single item in set based on it's id. know can't query data structure if nosql db, tried using zscan
, didn't work.
zscan myset 0 match id:92 count 1
it returns; "empty list or set"
maybe need serialize different? have serialized using json.net.
how, if possible, can achieve this; using dates score , still able lookup item it's id?
many thanks,
lars
edit:
assume it's not possible, thoughts or inputs welcome:
ref: http://openmymind.net/2011/11/8/redis-zero-to-master-in-30-minutes-part-1/
in redis, data can queried key. if use hash, can't me keys wherever field race equal sayan.
edit 2:
i tried do:
zscan myset 0 match *87* 127.0.0.1:6379> zscan myset 0 match *87* 1) "192" 2) 1) "{\"id\":\"64\",\"ref\":\"xqh4\",\"dtime\":1443837798,\"atime\":1444187707,\"extime\":0,\"spname\":\"xqh4bpgw47fm\",\"stpname\":\"xqh4bpgw47fm\"}" 2) "1443837798" 3) "{\"id\":\"87\",\"ref\":\"5cy6\",\"dtime\":1443519199,\"atime\":1444172326,\"extime\":0,\"spname\":\"5cy6dhp23rxb\",\"stpname\":\"5cy6dhp23rxb\"}" 4) "1443519199"
and finds desired item, finds 1 occurance of 87 in property atime. having more unique, longer ids might work way , have filter results in code find 1 exact value in property.
still open suggestions.
i think it's simple.
solution 1(inferior, not recommended)
your way of zscan myset 0 match id:92 count 1
didn't work out because stored string "{\"id\":\"92\"...
not "{\"id:92\"...
. string has been changed format. try use match id\":\"64
or match json serialized data in redis. i'm not familiar json.net, actual string leaves discover.
by way, have ask did zscan myset 0 match id:92 count 1
return cursor? suspect used zscan
in wrong way.
solution 2(better, recommended)
zscan
when sorted set not large , know how save network roundtrip time redis' lua transaction. still make "look id" operation o(n). therefore, better solution change data model in following way:
change sorted set from
# score value 0 1443476076 {"id":"92","ref":"7adt","dtime":1443476076,"atime":1443901554,"extime":0,"spname":"7adt33cfsau6","stpname":"7adt33cfsau6"} 1 1443482969 {"id":"11","ref":"dajt","dtime":1443482969,"atime":1443901326,"extime":0,"spname":"dajtjtt4t02o","stpname":"dajtjtt4t02o"}
to
# score value 0 1443476076 id:92 1 1443482969 id:11
move rest detailed data in set of hashes type keys:
# key field-value field-value ... 0 id:92 ref-7adt dtime-1443476076 ... 1 id:11 ref-7adt dtime-1443476076 ...
then, locate id doing hgetall id:92
. ranged query date, need zrangebyscore sortedset mindate maxdate
hgetall
every id 1 one. you'd better use lua wrap these commands in 1 , still super fast!
data in nosql database need organized in redundant way above. may make usual operation involve more 1 commands , roundtrip, can tackled redis's lua feature. recommend lua feature of redis, cause wrap commands 1 network roundtrip, executed on redis-server side , atomic , super fast!
reply if there's don't know
Comments
Post a Comment