How to fetch all entities in App engine datastore?

会有一股神秘感。 提交于 2019-12-06 01:37:36
Lynard

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.

Gergely Orosz

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.

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