Random querying for Google App Engine Datastore en

2019-08-21 11:12发布

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.

1条回答
Viruses.
2楼-- · 2019-08-21 11:40

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 :)

查看更多
登录 后发表回答