0

我希望有人可以向我解释如何在 App Engine 中使用偏移量或游标。我正在使用gcloud远程访问实体进行大量数据迁移,并希望以 100 个为单位抓取数据。

我猜有一种非常简单的方法可以做到这一点,但文档并没有过多地深入游标。这是我到目前为止所拥有的:

client = datastore.Client(dataset_id=projectID)

# retrieve 100 Articles
query = client.query(kind='Article').fetch(100)

for article in query:
  print article

我怎么能标记那批 100 的结束,然后进入下一个?非常感谢!

编辑:

我应该提到我无法访问应用程序引擎环境,这就是为什么我现在有点迷失...... :(

4

1 回答 1

1

我对 gcloud 没有任何经验,但我认为这应该不会有太大的不同。

当您查询时,您将使用fetch_page而不是fetch函数。fetch_page函数返回三件事(结果、游标、更多)。光标是您的查询书签,如果可能有更多结果,则更多

处理完 100 个实体后,您可以将 urlsafe 形式的光标传递给请求处理程序的 URI,在此您将从新光标处开始继续该过程。

from google.appengine.datastore.datastore_query import Cursor

class printArticles(webapp2.RequestHandler):
    def post(self):

        query = Client.query()

        #Retrieve the cursor. 
        curs = Cursor(urlsafe=self.request.get('cursor'))

        #fetch_page returns three things
        articles, next_curs, more = query.fetch_page(100, start_cursor=curs)

        #do whatever you need to do
        for article in articles:
            print article

        #If there are more results to fetch
        if more == True and next_curs is not None:

            #then pass along the cursor
            self.redirect("/job_URI?cursor=" + next_curs.urlsafe())
于 2015-08-27T21:38:18.740 回答