Random querying for Google App Engine Datastore entities using Java

流过昼夜 提交于 2019-12-12 02:38:45

问题


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:


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



来源:https://stackoverflow.com/questions/27575440/random-querying-for-google-app-engine-datastore-entities-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!