How does one get a count of rows in a Datastore model in Google App Engine?

心已入冬 提交于 2019-11-26 20:09:48

问题


I need to get a count of records for a particular model on App Engine. How does one do it?

I bulk uploaded more than 4000 records but modelname.count() only shows me 1000.


回答1:


You should use Datastore Statistics:

Query query = new Query("__Stat_Kind__");
query.addFilter("kind_name", FilterOperator.EQUAL, kind);       
Entity entityStat = datastore.prepare(query).asSingleEntity();
Long totalEntities = (Long) entityStat.getProperty("count");

Please note that the above does not work on the development Datastore but it works in production (when published).

I see that this is an old post, but I'm adding an answer in benefit of others searching for the same thing.




回答2:


As of release 1.3.6, there is no longer a cap of 1,000 on count queries. Thus you can do the following to get a count beyond 1,000:

count = modelname.all(keys_only=True).count()

This will count all of your entities, which could be rather slow if you have a large number of entities. As a result, you should consider calling count() with some limit specified:

count = modelname.all(keys_only=True).count(some_upper_bound_suitable_for_you)



回答3:


count = modelname.all(keys_only=True).count(some_upper_limit)

Just to add on to the earlier post by dar, this 'some_upper_limit' has to be specified. If not, the default count will still be a maximum of 1000.




回答4:


This is a very old thread, but just in case it helps other people looking at it, there are 3 ways to accomplish this:

  1. Accessing the Datastore statistics
  2. Keeping a counter in the datastore
  3. Sharding counters

Each one of these methods is explained in this link.




回答5:


In GAE a count will always make you page through the results when you have more than 1000 objects. The easiest way to deal with this problem is to add a counter property to your model or to a different counters table and update it every time you create a new object.




回答6:


I still hit the 1000 limit with count so adapted dar's code (mine's a bit quick and dirty):

class GetCount(webapp.RequestHandler):
    def get(self):
        query = modelname.all(keys_only=True)

        i = 0
        while True:
            result = query.fetch(1000)
            i = i + len(result)
            if len(result) < 1000:
                break
            cursor = query.cursor()
            query.with_cursor(cursor)

        self.response.out.write('<p>Count: '+str(i)+'</p>')



回答7:


DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
Query query = new Query("__Stat_Kind__");
Query.Filter eqf = new Query.FilterPredicate("kind_name",
                                Query.FilterOperator.EQUAL,
                                "SomeEntity");
query.setFilter(eqf);
Entity entityStat = ds.prepare(query).asSingleEntity();
Long totalEntities = (Long) entityStat.getProperty("count");



回答8:


Another solution is using a key only query and get the size of the iterator. The computing time with this solution will rise linearly with the amount of entrys:

Datastore datastore = DatastoreOptions.getDefaultInstance().getService();
KeyFactorykeyFactory = datastore.newKeyFactory().setKind("MyKind");
Query query = Query.newKeyQueryBuilder().setKind("MyKind").build();
int count = Iterators.size(datastore.run(query));


来源:https://stackoverflow.com/questions/751124/how-does-one-get-a-count-of-rows-in-a-datastore-model-in-google-app-engine

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