简体   繁体   中英

storing set as a value for hash in redis

I have file consisting of thousands of lines(each containing 3 fields, first is ak length string, then a number, third another string) of form:-

k|1|r1
k|1|r2
k|2|r2
k1|1|r3

I load it using redis-py, by using:-

sadd('k:1', 'r1')
sadd('k:1', 'r2')
sadd('k:2', 'r2')
sadd('k1:1', 'r3')

to form a mapping like

{
     "k:1"  : ("r1", "r2"),
     "k:2"  : ("r2"),
     "k1:1" : ("r3")
}

I intend to store the values of the form, by removing the repetitive information of k(which is ak length string common for the first 3 records):

{
     "k": {
         "1"  : ("r1", "r2"),
        "2"  : ("r2")
      }
     "k1": {
       "1" : ("r3")
     }
}

I have the idea of storing the value of storing the set under a different key, which can act as value for k in the hash. Is there a better way than that?

将集合存储在另一个键下将起作用,但是,如果集合是静态的,则不需要集合功能-相反,您可以保存自己的查找并将集合直接存储在地图中,例如,以逗号分隔的值(或如果您只有几个值,则使用任何适合的分隔符);如果您有很多值,则使用逗号分隔的逗号分隔值(根据我的经验,如果字符串足够大,则g的成本(un)zip被降低的网络成本所抵消)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM