如何使用高复制数据存储 [英] How to use High Replication Datastore

查看:100
本文介绍了如何使用高复制数据存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我观看了视频,并阅读App Engine文档中的文章(包括使用高复制数据存储)。但是,我仍然完全不了解它的实际用法。我明白(从视频)的好处,他们听起来不错。但我缺乏的是几个实际的例子。 Web上有大量的主/从示例,但很少说明(具有适当的文档)高复制数据存储。在使用高复制数据存储文章通过添加以前留言簿代码示例不具有的新功能来说明祖先密钥(似乎您可以更改留言簿)。这只会增加混乱。

我经常使用 djangoforms a>,并且我想知道是否有人可以帮助我将所有这些查询转换为高度复制数据存储兼容查询(让我们暂时忘记讨论,并非所有查询都必须是高度复制数据存储兼容查询并关注示例本身)。
$ b

更新:与高复制数据存储兼容查询我指的是总是返回最新数据而不是潜在陈旧数据的查询。使用实体组似乎是这里的一种方式,但正如前面提到的,我没有很多实用的代码示例来说明如何实现这一点,所以这正是我所需要的!



因此,本文中的查询为:



本文中主要的循环查询是:

  query = db.GqlQuery(SELECT * FROM Item ORDER BY name)

我们将转换为:

  query = Item.all()。order('name')//数据存储请求


验证表单的方式如下:

  data = ItemForm(data = self.request.POST)
if data.is_valid():
#保存数据并重定向到视图页
entity = data.save(commit = False)
entity.added_by = users.get_current_user()
entity.put()//数据存储请求

并从数据存储中获取最新条目以填充表单,如下所示:

  id = int(self。 request.get('id'))
item = Item.get(db.Key.from_path('Item',id))//数据存储请求
data = ItemForm(data = self.request。 POST,instance = item)

那么我/我们需要做什么来完成所有这些数据存储请求兼容高复制数据存储?



最后一件事情对我也不清楚。使用祖先密钥,这是否对数据存储区中的模型有任何影响。例如,在他们使用的留言簿代码示例中:

  def guestbook_key(guestbook_name = None):
return db。 Key.from_path('Guestbook',guestbook_name或'default_guestbook')

然而'留言簿'不存在在模型中 ,那么你怎么能在这个上使用'db.Key.from_path',为什么会这样呢?这是否会改变数据存储在数据存储中的方式,我需要在检索数据时考虑这些数据(例如,它是否添加了另一个字段,我应该从使用djangoforms时不显示)?

就像我之前说过的,这让我很困惑,你的帮助非常感谢!

解决方案

我不确定为什么你认为你需要改变你的查询。您链接的文档中明确指出:


后端更改,但数据存储API根本没有更改。无论您使用哪个数据存储区,您都将使用相同的编程接口。


该页面的要点只是说如果您不使用实体组,则查询可能不同步。您的最终代码片段仅仅是一个例子 - 字符串留言簿完全是一个祖先的关键。我不明白你为什么认为它需要存在于模型中。再一次,这与非HR数据存储没有任何关系 - 密钥始终由路径构成,可由任意字符串组成。您可能需要重新阅读实体组和键


Okay, I have watched the video and read the articles in the App Engine documentation (including Using the High Replication Datastore). However I am still completely confused on the practical usage of it. I understand the benefits (from the video) and they sound great. But what I am lacking is a few practical examples. There are plenty of master/slave examples on the web, but very little illustrating (with proper documentation) the high replication datastore. The guestbook code example used in the Using the High Replication Datastore article illustrates the ancestor key by adding a new functionality that the previous guestbook code example does not have (seems you can change guestbook). This just adds to the confusion.

I often use djangoforms on GAE and I was wondering if someone can help me translate all these queries into high replication datastore compatible queries (let's forget for a moment the discussion that not all queries necessarily need to be high replication datastore compatible queries and focus on the example itself).

UPDATE: with high replication datastore compatible queries I refer to queries that always return the latest data and not potential stale data. Using entity groups seems to be the way to go here but as mentioned before, I don't have many practical code examples of how to do this, so that is what I am looking for!

So the queries in this article are:

The main recurring query in this article is:

query = db.GqlQuery("SELECT * FROM Item ORDER BY name")

which we will translate to:

query = Item.all().order('name')  // datastore request

validating the form happens like:

data = ItemForm(data=self.request.POST)
if data.is_valid():
    # Save the data, and redirect to the view page
    entity = data.save(commit=False)
    entity.added_by = users.get_current_user()
    entity.put()  // datastore request

and getting the latest entry from the datastore for populating a form happens like:

id = int(self.request.get('id'))
item = Item.get(db.Key.from_path('Item', id))  // datastore request
data = ItemForm(data=self.request.POST, instance=item)

So what do I/we need to do to make all these datastore requests compatible with the high replication datastore?

One last thing that is also not clear to me. Using ancestor keys, does this have any impact on the model in datastore. For example, in the guestbook code example they use:

def guestbook_key(guestbook_name=None):
  return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')

However 'Guestbook' does not exist in the model, so how can you use 'db.Key.from_path' on this and why would this work? Does this change how data is stored in the datastore which I need to keep into account when retrieving the data (e.g. does it add another field I should exclude from showing when using djangoforms)?

Like I said before, this is confusing me a lot and your help is greatly appreciated!

解决方案

I'm not sure why you think you need to change your queries at all. The documentation that you link to clearly states:

The back end changes, but the datastore API does not change at all. You'll use the same programming interfaces no matter which datastore you're using.

The point of that page is just to say that queries may be out of sync if you don't use entity groups. Your final code snippet is just an example of that - the string 'Guestbook' is exactly an ancestor key. I don't understand why you think it needs to exist in the model. Once again, this is unchanged from the non-HR datastore - it has always been the case that keys are built up from paths, which can consist of arbitrary strings. You probably need to reread the documentation on entity groups and keys.

这篇关于如何使用高复制数据存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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