使用App Engine查询游标(Java - JDO)

时间:2012-12-30 06:28:34

标签: java google-app-engine google-cloud-datastore jdo

我需要在我的结果提取中实现分页,所以在环顾四周时,我在Google Developer网站上获得了Query Cursors的链接,但文章解释了使用低级API的Query Cursors的使用(我避免这样的事情)瘟疫)。我在JDO上看到的所有内容都使用setRange(start, end)(如果我是对的)效率不高,因为您仍然需要支付获取和丢弃范围之前的结果所涉及的开销。 如何在App Engine数据存储区之上使用JDO中的查询游标?

1 个答案:

答案 0 :(得分:5)

来自appengine docs:

Query q = pm.newQuery(Person.class);
q.setRange(0, 20);

List<Person> results = (List<Person>) q.execute();
// Use the first 20 results

Cursor cursor = JDOCursorHelper.getCursor(results);

https://developers.google.com/appengine/docs/java/datastore/jdo/queries

然后,使用光标(也在提供的文档中,您应该阅读):

String cursorString = cursor.toWebSafeString();
// Send cursor around as string


// Query q = the same query that produced the cursor;
Cursor cursor = Cursor.fromWebSafeString(cursorString);
Map<String, Object> extensionMap = new HashMap<String, Object>();
extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
q.setExtensions(extensionMap);
q.setRange(0, 20); //note, the same range; 
//the query should cover everything you expect to load.
q.execute();
相关问题