我有一个parent
hashmap数据结构,其中一个字符串作为键,并将hashmap数据结构作为子项(guess child1
,child2
,...,childN
) 。每个子节点都是一个简单的键值映射,其数字为键,字符串为值。
在伪代码中:
parent['key1'] = child1; // child1 is a hash map data structure
child1[0] = 'foo';
child[1] = 'bar';
...
我需要将此数据结构实现为数据库系统中的快速查找表。 让我们以Python作为参考语言。
解决方案的要求:
parent
哈希估计总重量最多为500 MB 用例如下:
您会推荐内存中的键值数据存储(例如Redis)还是更经典的“关系型”数据库解决方案?您建议我使用哪种数据模型?
答案 0 :(得分:2)
绝对适合Redis。它不仅非常快,而且还能完全处理您需要的结构:http://redis.io/commands#hash
在您的情况下,您可以避免阅读整个'子哈希',因为客户端“从哈希中提取特定值(它已经知道要使用哪个密钥)”
redis> HMSET myhash field1 "Hello" field2 "World"
OK
redis> HGET myhash field1
"Hello"
redis> HGET myhash field2
"World"
或者,如果你想要整个哈希:
redis> HGETALL myhash
1) "field1"
2) "Hello"
3) "field2"
4) "World"
redis>
当然,使用client library可以在可行的对象中提供结果,在您的情况下,是一个Python字典。
答案 1 :(得分:2)
使用redis-py的示例代码,假设您已安装Redis(理想情况为hiredis),将每个父项保存为哈希字段,子项为序列化字符串,并处理序列化和反序列化客户方:
JSON版本:
## JSON version
import json
# you could use pickle instead,
# just replace json.dumps/json.loads with pickle/unpickle
import redis
# set up the redis client
r = redis.StrictRedis(host = '', port = 6379, db = 0)
# sample parent dicts
parent0 = {'child0': {0:'a', 1:'b', 2:'c',}, 'child1':{5:'e', 6:'f', 7:'g'}}
parent1 = {'child0': {0:'h', 1:'i', 2:'j',}, 'child1':{5:'k', 6:'l', 7:'m'}}
# save the parents as hashfields, with the children as serialized strings
# bear in mind that JSON will convert the int keys to strings in the dumps() process
r.hmset('parent0', {key: json.dumps(parent0[key]) for key in parent0})
r.hmset('parent1', {key: json.dumps(parent0[key]) for key in parent1})
# Get a child dict from a parent
# say child1 of parent0
childstring = r.hget('parent0', 'child1')
childdict = json.loads(childstring)
# this could have been done in a single line...
# if you want to convert the keys back to ints:
for key in childdict.keys():
childdict[int(key)] = childdict[key]
del childdict[key]
print childdict
pickle版本:
## pickle version
# For pickle, you need a file-like object.
# StringIO is the native python one, whie cStringIO
# is the c implementation of the same.
# cStringIO is faster
# see http://docs.python.org/library/stringio.html and
# http://www.doughellmann.com/PyMOTW/StringIO/ for more information
import pickle
# Find the best implementation available on this platform
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
import redis
# set up the redis client
r = redis.StrictRedis(host = '', port = 6379, db = 0)
# sample parent dicts
parent0 = {'child0': {0:'a', 1:'b', 2:'c',}, 'child1':{5:'e', 6:'f', 7:'g'}}
parent1 = {'child0': {0:'h', 1:'i', 2:'j',}, 'child1':{5:'k', 6:'l', 7:'m'}}
# define a class with a reusable StringIO object
class Pickler(object):
"""Simple helper class to use pickle with a reusable string buffer object"""
def __init__(self):
self.tmpstr = StringIO()
def __del__(self):
# close the StringIO buffer and delete it
self.tmpstr.close()
del self.tmpstr
def dump(self, obj):
"""Pickle an object and return the pickled string"""
# empty current buffer
self.tmpstr.seek(0,0)
self.tmpstr.truncate(0)
# pickle obj into the buffer
pickle.dump(obj, self.tmpstr)
# move the buffer pointer to the start
self.tmpstr.seek(0,0)
# return the pickled buffer as a string
return self.tmpstr.read()
def load(self, obj):
"""load a pickled object string and return the object"""
# empty the current buffer
self.tmpstr.seek(0,0)
self.tmpstr.truncate(0)
# load the pickled obj string into the buffer
self.tmpstr.write(obj)
# move the buffer pointer to start
self.tmpstr.seek(0,0)
# load the pickled buffer into an object
return pickle.load(self.tmpstr)
pickler = Pickler()
# save the parents as hashfields, with the children as pickled strings,
# pickled using our helper class
r.hmset('parent0', {key: pickler.dump(parent0[key]) for key in parent0})
r.hmset('parent1', {key: pickler.dump(parent1[key]) for key in parent1})
# Get a child dict from a parent
# say child1 of parent0
childstring = r.hget('parent0', 'child1')
# this could be done in a single line...
childdict = pickler.load(childstring)
# we don't need to do any str to int conversion on the keys.
print childdict