In many cases, it could be useful to know the number of rows in a table (a kind) in a datastore using Google Application Engine.
There is not clear and fast solution . At least I have not found one.. Have you?
Let’s count all rows in the table. COUNT (*) counts the total number of rows in the table: Instead of passing in the asterisk as the argument, you can use the name of a specific column: In this case, COUNT (id) counts the number of rows in which id is not NULL. Use the COUNT aggregate function to count the number of rows in a table.
The safest way to determine the number of rows in a dataframe is to count the length of the dataframe’s index. To return the length of the index, write the following code: The Pandas .shape attribute can be used to return a tuple that contains the number of rows and columns, in the following format (rows, columns).
You can display a count by doing a CountRows on the data source that you set for the Items property of your DataTable. If you could screenshot/share whatever error your received when you used CountRows, or explain more precisely how it didn't work, we can guide you in the right direction.
We get 145460 as the length which is equal to the number of rows in the dataframe. Note that both of the above methods, .shape [0] or len () are constant time operations and are thus pretty fast.
You can efficiently get a count of all entities of a particular kind (i.e., number of rows in a table) using the Datastore Statistics. Simple example:
from google.appengine.ext.db import stats
kind_stats = stats.KindStat().all().filter("kind_name =", "NameOfYourModel").get()
count = kind_stats.count
You can find a more detailed example of how to get the latest stats here (GAE may keep multiple copies of the stats - one for 5min ago, one for 30min ago, etc.).
Note that these statistics aren't constantly updated so they lag a little behind the actual counts. If you really need the actual count, then you could track counts in your own custom stats table and update it every time you create/delete an entity (though this will be quite a bit more expensive to do).
Update 03-08-2015: Using the Datastore Statistics can lead to stale results. If that's not an option, another two methods are keeping a counter or sharding counters. (You can read more about those here). Only look at these 2 if you need real-time results.
There's no concept of "Select count(*)" in App Engine. You'll need to do one of the following:
You can count no. of rows in Google App Engine using com.google.appengine.api.datastore.Query as follow:
int count;
Query qry=new Query("EmpEntity");
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
count=datastoreService.prepare(qry).countEntities(FetchOptions.Builder.withDefaults());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With