如何在 GAE Python NDB 中获取最新数据 [英] How to fetch the latest data in GAE Python NDB
问题描述
我正在使用 GAE Python.我有两个根实体:
I am using GAE Python. I have two root entities:
class X(ndb.Model):
subject = ndb.StringProperty()
grade = ndb.StringProperty()
class Y(ndb.Model):
identifier = ndb.StringProperty()
name = ndb.StringProperty()
school = ndb.StringProperty()
year = ndb.StringProperty()
result = ndb.StructuredProperty(X, repeated=True)
由于谷歌将我们的数据存储在多个数据中心,当我们进行如下查询时,我们可能无法获得最新的数据(以防某些更改已被放置"):
Since google stores our data across several data centers, we might not get the most recent data when we do a query as shown below(in case some changes have been "put"):
def post(self):
identifier = self.request.get('identifier')
name = self.request.get('name')
school = self.request.get('school')
year = self.request.get('year')
qry = Y.query(ndb.AND(Y.name==name, Y.school==school, Y.year==year))
record_list = qry.fetch()
我的问题:我应该如何修改上面的fetch操作以始终获取最新的数据
My question: How should I modify the above fetch operation to always get the latest data
我已经浏览了相关的 google 帮助文档,但无法了解如何在此处应用
I have gone through the related google help doc but could not understand how to apply that here
根据艾萨克回答的提示,以下是解决方案吗(latest_record_data"是否包含实体的最新数据):
Based on hints from Isaac answer, Would the following be the solution(would "latest_record_data" contain the latest data of the entity):
def post(self):
identifier = self.request.get('identifier')
name = self.request.get('name')
school = self.request.get('school')
year = self.request.get('year')
qry = Y.query(ndb.AND(Y.name==name, Y.school==school, Y.year==year))
record_list = qry.fetch()
record = record_list[0]
latest_record_data = record.key.get()
推荐答案
在应用引擎上有几种方法可以获得强一致性,最常见的是使用获取而不是查询和使用祖先查询.
There's a couple ways on app engine to get strong consistency, most commonly using gets instead of queries and using ancestor queries.
要在您的示例中使用 get,您可以将名称编码到实体键中:
To use a get in your example, you could encode the name into the entity key:
class Y(ndb.Model):
result = ndb.StructuredProperty(X, repeated=True)
def put(name, result):
Y(key=ndb.Key(Y, name), result).put()
def get_records(name):
record_list = ndb.Key(Y, name).get()
return record_list
祖先查询使用相似的概念来做一些更强大的事情.例如,获取具有特定名称的最新记录:
An ancestor query uses similar concepts to do something more powerful. For example, fetching the latest record with a specific name:
import time
class Y(ndb.Model):
result = ndb.StructuredProperty(X, repeated=True)
@classmethod
def put_result(cls, name, result):
# Don't use integers for last field in key. (one weird trick)
key = ndb.Key('name', name, cls, str(int(time.time())))
cls(key=key, result=result).put()
@classmethod
def get_latest_result(cls, name):
qry = cls.query(ancestor=ndb.Key('name', name)).order(-cls.key)
latest = qry.fetch(1)
if latest:
return latest[0]
祖先"是实体的第一对键.只要您可以将至少具有第一对的密钥放入查询中,您就会获得强一致性.
The "ancestor" is the first pair of the entity's key. As long as you can put a key with at least the first pair into the query, you'll get strong consistency.
这篇关于如何在 GAE Python NDB 中获取最新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!