javagoogle-app-enginegoogle-cloud-datastorelow-level-api

Retrive all the entities from a datastore


I m using the low-level API in the App Engine Datastore . To retrieve an entity I use

Entity post = datastore.get(postKey);

but this code will return only the post with this postkey. What shall i do if i want to return all the posts ?


Solution

  • you need todo a query, not a get,

    get ist just a single instance by id, query is result of your query.

    just do:

    // Use class Query to assemble a query
    Query q = new Query("Post");
    
    // Use PreparedQuery interface to retrieve results
    PreparedQuery pq = datastore.prepare(q);
    
    
    for (Entity result : pq.asIterable()) {
      String postName = (String) result.getProperty("PostName");
    }