Can I store a python dictionary in google's BigTable datastore without serializing it explicitly?

后端 未结 3 797
别跟我提以往
别跟我提以往 2021-02-11 09:20

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 t

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-11 09:58

    I think you cannot avoid serializing your objects.

    I would define the following model to store each key, value pair:

    class DictModel(db.Model):
        value = db.TextProperty()
    

    To save to the datastore I'd use:

    def set_value(key, value):
        key = DictModel(value=pickle.dumps(value), key_name=key)
        key.save()
        return key
    

    And to retrieve data:

    def get_value(key):
        return pickle.loads(DictModel.get_by_key_name(key).value)
    

提交回复
热议问题