问题
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 the different kinds of entities in my Datastore.
回答1:
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.
回答2:
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/>");
}
回答3:
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());
}
来源:https://stackoverflow.com/questions/3209324/how-to-get-all-the-kinds-in-the-google-app-engine-datastore