2010-05-25 56 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