在Google App Engine数据存储中存储分层数据? [英] Storing hierarchical data in Google App Engine Datastore?

查看:107
本文介绍了在Google App Engine数据存储中存储分层数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以说明我可以如何在谷歌应用程序引擎数据存储中存储和轻松查询分层数据吗?

解决方案

最佳选择取决于您的要求。这里有一些解决方案(我假设你使用Python,因为你没有指定):


  1. 如果你需要做整个树上的事务更新,并且对任何一棵树的持续更新不会超过约1QPS,您可以使用内置支持来存储叶片。创建实体时,可以传递parent属性来指定父实体或键,并且在查询时,可以使用.ancestor()方法(或GQL中的'ANCESTOR IS')来检索给定实体的所有后代。
  2. 如果您不需要事务更新,则可以复制实体组的功能而不存在争用问题(和事务安全性):将db.ListProperty(db.Key)添加到你的模型称为'祖先',并用你所插入的对象的祖先列表填充它,然后你可以很容易地从MyModel.all()中获取给定祖先的所有后代。filter('ancestors =',parent_key )。
  3. 如果您不需要事务,并且您只关心检索实体的直接子对象(不是所有子对象),请使用上面概述的方法,而不是ListProperty只需将一个ReferenceProperty用于父实体即可,这就是所谓的邻接列表。

是其他方法可用,但这三个应该涵盖最常见的情况。


Can someone illustrate how I can store and easily query hierarchical data in google app engine datastore?

解决方案

The best option depends on your requirements. Here's a few solutions (I'm assuming you're using Python, since you didn't specify):

  1. If you need to do transactional updates on an entire tree, and you're not going to have more than about 1QPS of sustained updates to any one tree, you can use the built in support for heirarchial storage. When creating an entity, you can pass the "parent" attribute to specify a parent entity or key, and when querying, you can use the .ancestor() method (or 'ANCESTOR IS' in GQL to retrieve all descendants of a given entity.
  2. If you don't need transactional updates, you can replicate the functionality of entity groups without the contention issues (and transaction safety): Add a db.ListProperty(db.Key) to your model called 'ancestors', and populate it with the list of ancestors of the object you're inserting. Then you can easily retrieve everything that's descended from a given ancestor with MyModel.all().filter('ancestors =', parent_key).
  3. If you don't need transactions, and you only care about retrieving the direct children of an entity (not all descendants), use the approach outlined above, but instead of a ListProperty just use a ReferenceProperty to the parent entity. This is known as an Adjacency List.

There are other approaches available, but those three should cover the most common cases.

这篇关于在Google App Engine数据存储中存储分层数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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