How to get all the kinds in the google app engine datastore?
I'm using java to code for GAE, I've read through the GAE Java low level API and can't find any answer to my question yet.
I wanna know if there's a way where I can call a method/do a JDOPL and it returns all th开发者_如何学Ce different kinds of entities in my Datastore.
You could use the datastore statistics API:
http://code.google.com/appengine/docs/java/datastore/stats.html
It looks like the __Stat_Kind__
statistic will give you the info you want.
I found a working solution here. (it doesn't work in local deployment server as of July 9, 2010)
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
PreparedQuery global = datastore.prepare(new Query("__Stat_Kind__"));
for( Entity globalStat : global.asIterable() )
{
Long totalBytes = (Long) globalStat.getProperty("bytes");
Long totalEntities = (Long) globalStat.getProperty("count");
String kindName = (String) globalStat.getProperty("kind_name");
resp.getWriter().println("[" + kindName + "] has " + totalEntities + " entities and takes up " + totalBytes + "bytes<br/>");
}
You can use the Metadata API. For example:
Query query = new Query(Entities.KIND_METADATA_KIND);
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
Iterable<Entity> entityIterable = datastoreService.prepare(query).asIterable();
for(Entity entity : entityIterable) {
System.out.println("Entity kind: " + entity.getKey().getName());
}
精彩评论