How to get all the kinds in the google app engine

2019-08-19 03:36发布

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.

3条回答
祖国的老花朵
2楼-- · 2019-08-19 04:10

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());
}
查看更多
神经病院院长
3楼-- · 2019-08-19 04:30

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.

查看更多
戒情不戒烟
4楼-- · 2019-08-19 04:30

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/>");
}
查看更多
登录 后发表回答