获取 Google 云数据存储中的项目数

Get count of items in Google Cloud Datastore

在我的 android 应用程序中使用 App Engine 的 endpoints 我需要根据过滤器获取数据存储中的项目数量,并 return 我的应用程序。

我可以在 App Engine 上做,然后 return 结果作为一个集合,但我不需要任何这些数据,只需要项目的数量。

因为我不能 return 基本类型,所以使用 Java API 来实现这个是什么合适的?

您可以 return 用 Map<String, Long>/Map<String, Integer> 代替 long/int
当您使用端点时,您将能够使用推入地图的键轻松访问这些值。 如果不使用端点,您将在响应中将此映射作为 json 对象,只需在响应中查找相关键 json.

我最后做的是像这样在 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();
}