我可以在谷歌的BigTable数据存储中存储python字典而无需显式序列化它? [英] Can I store a python dictionary in google's BigTable datastore without serializing it explicitly?

查看:91
本文介绍了我可以在谷歌的BigTable数据存储中存储python字典而无需显式序列化它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python字典,我想将它存储在Google的BigTable数据存储中(它是 db.Model 类中的一个属性)。



有没有简单的方法来做到这一点?即使用 db.DictionaryProperty ?或者我必须使用pickle来序列化我的字典?我的字典相对简单。它由作为键的字符串组成,但也可能包含某些键的子字典。例如:

  {
'myKey':100,
'another':'aha',
'子字典':{'a':1,'b':2}
}

PS:我想序列化为二进制文件,如果可能的话不是文本。 解决方案

这是另一种方法

 class DictProperty(db.Property):
data_type = dict
$ b $ def get_value_for_datastore(self,model_instance):
value = super(DictProperty ,self).get_value_for_datastore(model_instance)
return db.Blob(pickle.dumps(value))
$ b $ def make_value_from_datastore(self,value):
如果value是None:
返回dict()
返回pickle.loads(值)

def default_value(self):
如果self.default为无:
return dict( )
else:
return super(Dict Property,self).default_value()。copy()

def validate(self,value):
如果不是isinstance(value,dict):
raise db.BadValueError( 'property%s需要可转换'
'为类dict'%(self.name,value)的字典实例(%s))
返回超级(DictProperty,self).validate(value )

def empty(self,value):
返回值是


I have a python dictionary that I would like to store in Google's BigTable datastore (it is an attribute in a db.Model class).

Is there an easy way to do this? i.e. using a db.DictionaryProperty? Or do I have to use pickle to serialize my dictionary? My dictionary is relatively straight forward. It consists of strings as keys, but it may also contain sub dictionaries for some keys. For example:

{ 
    'myKey' : 100,
    'another' : 'aha',
    'a sub dictionary' : { 'a': 1, 'b':2 }
}

PS: I would like to serialize as binary, not text if possible.

解决方案

Here's another approach:

class DictProperty(db.Property):
  data_type = dict

  def get_value_for_datastore(self, model_instance):
    value = super(DictProperty, self).get_value_for_datastore(model_instance)
    return db.Blob(pickle.dumps(value))

  def make_value_from_datastore(self, value):
    if value is None:
      return dict()
    return pickle.loads(value)

  def default_value(self):
    if self.default is None:
      return dict()
    else:
      return super(DictProperty, self).default_value().copy()

  def validate(self, value):
    if not isinstance(value, dict):
      raise db.BadValueError('Property %s needs to be convertible '
                             'to a dict instance (%s) of class dict' % (self.name, value))
    return super(DictProperty, self).validate(value)

  def empty(self, value):
    return value is None

这篇关于我可以在谷歌的BigTable数据存储中存储python字典而无需显式序列化它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆