2010-05-25 54 views
0

如果我应该通过不同的方法来解决这个问题,请提出建议。我正在创建一个基于项目的协作过滤器。我使用LinkRating2类填充数据库,并为每个链接有超过1000个用户,我需要调用并收集他们的评级以执行计算,然后使用它们创建另一个表。所以我需要为给定的链接调用超过1000个实体。如何取得超过1000个实体非密钥基础?

例如,假设有超过1000个用户评为'link1',那么对于给定的链接属性,我需要调用超过1000个此类的实例。
我该如何完成这个例子?

class LinkRating2(db.Model): 
    user = db.StringProperty() 
    link = db.StringProperty() 
    rating2 = db.FloatProperty() 

query =LinkRating2.all() 
link1 = 'link string name' 
a = query.filter('link = ', link1) 
aa = a.fetch(1000)##how would i get more than 1000 for a given link1 as shown? 


##keybased over 1000 in other post example i need method for a subset though not key 
class MyModel(db.Expando): 
     @classmethod 
     def count_all(cls): 
      """ 
      Count *all* of the rows (without maxing out at 1000) 
      """ 
      count = 0 
      query = cls.all().order('__key__') 
      while count % 1000 == 0: 
       current_count = query.count() 
       if current_count == 0: 
        break 
       count += current_count 

       if current_count == 1000: 
        last_key = query.fetch(1, 999)[0].key() 
        query = query.filter('__key__ > ', last_key) 

      return count 

回答

1

最近取消1000个实体的提取限制;您可以根据需要获取尽可能多的数量,但前提是您可以在时限内完成。你的实体看起来很小,所以你可以在请求中获取超过1000个。

+0

+1:哇,我完全忽略了这种改进!谢谢Wooble。 – 2010-05-25 23:02:53

+0

那么一个提取所有命令是什么样子呢?如果我不知道它的大小并想要获取所有内容?我不能把一个数字,并不能留空获取() – user291071 2010-06-04 03:45:58

+0

你仍然需要给一个限制。如果您真的想要获取所有内容,请使用任意大的整数。请注意,如果您拥有大量实体,无论如何您都无法在获取DeadlineExceededError之前获取所有实体,并且显示所有记录的视图对于大型数据集不太可用。 – geoffspear 2010-06-04 12:40:30

1

Wooble指出,1000个实体限制是过去的事情了,所以你其实并不需要使用游标来做到这一点 - 只取一次的一切(这将是比速度越来越快他们在1000个批次实体也自会有往返少到数据存储等)

的1000个实体限制去除在1.3.1版本中删除:http://googleappengine.blogspot.com/2010/02/app-engine-sdk-131-including-major.html

旧的解决方案中使用游标:

使用query cursors以获取超出前1000个实体的结果:

# continuing from your code ... get ALL of the query's results: 
more = aa 
while len(more) == 1000: 
    a.with_cusor(a.cursor()) # start the query where we left off 
    more = a.fetch(1000)  # get the next 1000 results 
    aa = aa + more   # copy the additional results into aa