如何获得存储在Google Appengine数据存储中的给定类型的所有条目的计数? [英] How do I get a count of all entries of a given type stored in Google appengine's datastore?

查看:94
本文介绍了如何获得存储在Google Appengine数据存储中的给定类型的所有条目的计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找的基本上是这个SQL翻译成Google AppEngine(对于Java)的条款:

  select count *)from Customers 

看起来很简单,但从阅读文档看来,我似乎必须运行一个匹配所有客户的查询,循环并计算结果,并将分页考虑在内。我不想检索每一个元素,我只想对它们进行计数。



换句话说,有一个API可以遍历给定类型的所有条目(目前无法找到确切的API)。这似乎是非常低效的,更不用说数据存储区调用也有一个有限的配额。



任何提示将不胜感激。



Thanks,Mark

解决方案

正如wooble所说,bigtable不支持行计数作为基本概念 - 你可以写一个包装函数,正如mcotton所说,但是,正如他从文档中引用的那样,它至多仍然会被限制在1000以下。

为了克服这些问题限制你需要为每一种你想要计数的实体保留一个计数器,该计数器在每次放入一个新的实体时递增,当这种实体被删除时递减。



为了让您的应用具有高度的可扩展性,您可能需要对这些计数器进行细分,请参阅 http://code.google.com/appengine/articles/sharding_counters.html (不幸的是,我不知道该配方到Java,但概念应该是相同的)。


What I'm looking for essentially is this SQL translated into Google AppEngine (for Java) terms:

select count(*) from Customers

Seems simple enough, but from reading the documentation, it seems like I would have to run a query that matches all Customers, loop though it and count the results, taking paging into account. I do not want to retrieve each and every element, I just want to count them.

Or another way, there was an API to loop over all entries of a given type (can't find the exact API at the moment). This seems to be quite inefficient, not to mention that datastore calls come with a limited quota as well.

Any hints would be appreciated.

Thanks, Mark

解决方案

As wooble says, bigtable doesn't support row counts as a fundamental concept -- you can write a wrapper function, as mcotton says, but, as he quotes from the docs, that will still be limited to 1000 at most.

To overcome these limits you'll need to keep, for each kind of entity you want to count, a counter that gets incremented everytime a new entity of that kind is put, decremented when an entity of that kind is deleted.

To keep your app highly scalable you'll probably want to shard such counters, see http://code.google.com/appengine/articles/sharding_counters.html (unfortunately I'm not aware of a translation of that recipe to Java, but the concepts should be the same).

这篇关于如何获得存储在Google Appengine数据存储中的给定类型的所有条目的计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆