如何获取App引擎数据存储区中的所有实体?

时间:2011-02-07 10:23:38

标签: google-app-engine datastore

http://code.google.com/appengine/docs/python/datastore/entities.html#Saving_Getting_and_Deleting_Entities

中的

获取实体的批处理操作如下所述:

批量获取。 entities = db.get([k1,k2,k3])

如何在不提供密钥的情况下获取所有实体?

2 个答案:

答案 0 :(得分:3)

我得到了一个解决方案,可以找到 Datastore Queries - Query interface example

Query q = new Query("Person") 
        PreparedQuery pq = datastore.prepare(q);  
    for (Entity result : pq.asIterable()) {   
       String firstName = (String) result.getProperty("firstName");   
       String lastName = (String) result.getProperty("lastName");   
       Long height = (Long) result.getProperty("height");   
       System.out.println(lastName + " " + firstName + ", " + height.toString() + "inches tall"); 
}

我没有在查询中添加过滤器,因为它从数据存储区返回所有实体。

答案 1 :(得分:0)

一个简单的方法是使用gql,条件始终为真,fetch结果。例如。如果你的实体有一个字符串字段,其名称是StringKey,你可以这样做:

entities = db.gql("WHERE StringKey >''").fetch(1000)

请注意,可以获得超过1000个实体,但在GAE中并不简单,请参阅this discussion