开发者

How to fetch all entities in App engine datastore?

开发者 https://www.devze.com 2023-02-09 04:21 出处:网络
in http://code.google.com/appengine/docs/python/datastore/entities.html#Saving_Getting_and_Deleting_Entities

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

the batch operation for get开发者_StackOverflowting the entity are stated below:

A batch get. entities = db.get([k1, k2, k3])

How can I fetch all entities without supplying keys?


I got a solution on this and can be found in 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"); 
}

I did not add filter in query since it return all entities from datastore.


An easy was to do this is using gql with a condition that is always true and fetch the results. E.g. if your entity has a string field that's name is StringKey, you could do:

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

Note that getting more than 1000 entities is possible, but not straightforward in GAE, see this discussion.

0

精彩评论

暂无评论...
验证码 换一张
取 消