【问题标题】:Get count of items in Google Cloud Datastore获取 Google Cloud Datastore 中的项目数
【发布时间】:2016-05-24 03:37:22
【问题描述】:
在我的 android 应用程序中使用应用程序引擎的 endpoints 我需要根据过滤器获取数据存储中的项目数并将其返回到我的应用程序。
我可以在应用引擎上执行,然后将结果作为集合返回,但我不需要任何数据,只需要项目数。
由于我无法返回原始类型,因此使用 Java API 来实现这一点比较合适?
【问题讨论】:
标签:
android
google-app-engine
google-cloud-platform
google-cloud-datastore
【解决方案1】:
您可以返回Map<String, Long>/Map<String, Integer> 来代替long/int。
当您使用端点时,您将能够使用您推入地图的键轻松访问这些值。
如果不使用端点,您将在响应中将此 Map 作为 json 对象获取,只需在响应 json 中查找相关键。
【解决方案2】:
我最终做的是像这样在 CollectionResponse 中返回计数
public CollectionResponse<Integer> count(@Named("weddingId") String weddingId){
Query<CloudRecord> query = ofy().load().type(CloudRecord.class).filter("weddingId =",weddingId);
List<Integer> count = new ArrayList<Integer>();
count.add(query.count());
return CollectionResponse.<Integer>builder().setItems(count).build();
}