Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

datastore count query fetchOptions

I want to do the following:

PreparedQuery pq = datastore.prepare(q);
int count = pq.countEntities(FetchOptions.ALL);

But there is no ALL option. So how do I do it?

For context, say I want to count all entry in my table where color is orange.

If I can't do this directly using DatastoreService, can I use Datanucleus's JPA? As in do they support SELECT COUNT(*) ... for the appengine datastore?

like image 873
Pouton Gerald Avatar asked Jun 03 '26 08:06

Pouton Gerald


2 Answers

You can count total no of record using following code.

com.google.appengine.api.datastore.Query qry = new com.google.appengine.api.datastore.Query("EntityName");
com.google.appengine.api.datastore.DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
int totalCount = datastoreService.prepare(qry).countEntities(FetchOptions.Builder.withDefaults());

i hope it will help you.

like image 52
Divyesh Rupawala Avatar answered Jun 04 '26 21:06

Divyesh Rupawala


The marked answer is not correct, it will max out in 1000

This is how one will get correct count

DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
 Query query = new Query("__Stat_Kind__");
 Query.Filter eqf = new Query.FilterPredicate("kind_name",
                            Query.FilterOperator.EQUAL,
                            "MY_ENTITY_KIND");
query.setFilter(eqf);
Entity entityStat = ds.prepare(query).asSingleEntity();
Long totalEntities = (Long) entityStat.getProperty("count");
like image 32
varun Avatar answered Jun 04 '26 21:06

varun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!