如何获取GAE Python NDB中的最新数据 [英] How to fetch the latest data in GAE Python NDB

查看:106
本文介绍了如何获取GAE Python NDB中的最新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GAE Python。我有两个根实体:

  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)

由于谷歌将数据存储在多个数据中心中,因此当我们执行查询时,我们可能无法获取最新数据(如果有一些更改已经 ):

  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()

我的问题:我应该如何修改上面的获取操作,以获取最新的数据



我浏览了相关的 Google帮助文档,但无法理解如何在这里应用

基于Isaac答案的提示,以下是解决方案(latest_record_data是否包含实体的最新数据):

  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()


解决方案

在应用程序引擎中有几种方法可以获得强大的一致性,通常使用gets而不是查询并使用祖先查询。

要在你的例子中使用get,你可以将这个名字编码为实体键:

  class Y(ndb.Model) :
result = ndb.StructuredProperty(X,repeated = True)

def put(name,result):
Y(key = ndb.Key(Y,name),结果).put()

def get_records(name):
record_list = ndb.Key(Y,name).get()
return record_list

祖先查询使用类似的概念来做更强大的事情。例如,获取具有特定名称的最新记录:

 进口时间

class Y(ndb 。模型):
result = ndb.StructuredProperty(X,repeated = True)

@classmethod
def put_result(cls,name,result):
#Don对键中的最后一个字段不使用整数。 (一个奇怪的技巧)
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))。 (-cls.key)
latest = qry.fetch(1)
if latest:
return latest [0]

祖先是实体关键字的第一对。只要您至少可以将第一对关键字放入查询中,您就可以获得强大的一致性。




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()  

My question: How should I modify the above fetch operation to always get the latest data

I have gone through the related google help doc but could not understand how to apply that here

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.

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屋!

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