随机查询使用Java的Google App Engine数据存储实体 [英] Random querying for Google App Engine Datastore entities using Java
问题描述
我使用
Query q = new Query(EntityTYPE)。addSort(age,SortDirection.DESCENDING);
我从另一个函数中有一个变量startPoint,告诉我需要结果的起点。现在我需要从已排序的查询中查询10个实体(startPoint to startPoint + 10)。
示例:如果startPoint = 51,我需要结果实体的排序查询的值为51-61行。
如何在Java中实现这一点?
如果需要进一步的信息,请发表评论。
像这样会使用偏移。不幸的是,它实施的方式抵消,它不会跳过看1-50。它会读取它们(花费您在日常配额/预算中的读数),并返回以下结果。它会做你想做的,但它仍然会向你收费,不幸的是,你必须写一些像
列表< Entity> getRandomEntities(){
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query queryForEntities = new Query(Entity);
PreparedQuery preppedEntityQuery = datastore.prepare(q);
返回preppedEntityQuery.asList(FetchOptions.Builder.withOffset([OFFSET_YOU_WANT])。withLimit([AMOUNT_YOU_WANT]));
}
调查这个如果你需要额外的信息:)
Let's say I have 100 entities in my datastore.
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.
Now that I need to query 10 entities (startPoint to startPoint+10) from the sorted query.
Example: If startPoint = 51, I need result entity to have values of 51-61 rows of the sorted query.
How can I implement this in 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. 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 :)
这篇关于随机查询使用Java的Google App Engine数据存储实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!