Get count of items in Google Cloud Datastore

时间:2016-02-12 20:46:02

标签: android google-app-engine google-cloud-platform google-cloud-datastore

Using app engine's 1.jpg 2.jpg 3.jpg in my android app I need to get the number of items in the data store based on a filter and return that to my app.

I could do on app engine and then return the results as a collection but I dont need any of that data and just need the number of items.

What would be the appropriate was to achieve this using the Java API since I cannot return primitive types?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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