如何获取超过1000个非键的实体?

2024-09-26 17:44:23 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我应该用不同的方法来解决这个问题,请建议这样做。我正在创建一个基于项目的协作过滤器。我用LinkRating2类填充数据库,对于每个链接,我需要调用1000多个用户并收集他们的评分,以执行计算,然后使用这些计算创建另一个表。所以我需要为一个给定的链接调用1000多个实体。在

例如,假设有1000多个用户被评为“link1”,对于给定的link属性,我需要调用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

Tags: key用户db链接countlinkfetchcurrent
2条回答

Wooble指出,1000个实体的限制现在已经成为过去,所以实际上不需要使用游标来执行此操作—只需一次获取所有内容(这将比以1000个实体批处理获取它们更快,因为到数据存储的往返次数将更少,等等)

在版本1.3.1中删除了1000个实体限制: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

最近删除了1000个实体的获取限制;如果可以在时间限制内执行,则可以根据需要获取任意数量的实体。你的实体看起来很小,所以你可以在一个请求中获取超过1000个。在

相关问题 更多 >

    热门问题