[英]Random querying for Google App Engine Datastore entities using Java
Let's say I have 100 entities in my datastore. 假设我的数据存储区中有100个实体。
I have sorted my query on basis of a property say "age" using 我已经根据使用“年龄”的属性对查询进行了排序,
Query q = new Query("EntityTYPE").addSort("age", SortDirection.DESCENDING);
I have a variable startPoint from another function which tells me the start point of the result needed. 我从另一个函数有一个变量startPoint,它告诉我所需结果的起点。
Now that I need to query 10 entities (startPoint to startPoint+10) from the sorted query. 现在,我需要从已排序的查询中查询10个实体(从startPoint到startPoint + 10)。
Example: If startPoint = 51, I need result entity to have values of 51-61 rows of the sorted query. 示例:如果startPoint = 51,则我需要结果实体具有已排序查询的51-61行的值。
How can I implement this in Java? 如何用Java实现呢?
Please do comment if any further information is necessary. 如果需要进一步的信息,请发表评论。
The way to do something like this would be to use an "offset". 做这样的事情的方法是使用“偏移”。 Unfortunately, the way offset it implemented, it doesn't "skip" looking at 1-50. 不幸的是,它实现的补偿方式并没有“跳过” 1-50。 It'll read them (costing you a read in your daily quotas/budgets), and return the following results. 它将读取它们(使您的每日配额/预算花费一读),然后返回以下结果。 It will do what you want, but it will still charge you, unfortunately, 它会做您想要的,但不幸的是,它仍然会向您收费,
You'd have to write something like 你必须写一些像
List<Entity> getRandomEntities() {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query queryForEntities = new Query("Entity");
PreparedQuery preppedEntityQuery = datastore.prepare(q);
return preppedEntityQuery.asList(FetchOptions.Builder.withOffset([OFFSET_YOU_WANT]).withLimit([AMOUNT_YOU_WANT]));
}
Look into this if you need additional info :) 如果需要其他信息,请查看此信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.