How to get all the kinds in the google app engine datastore?

十年热恋 提交于 2019-12-25 04:51:35

问题


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

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