use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class MetadataNamespacesTest method getNamespaces.
// [START namespace_query_example]
List<String> getNamespaces(DatastoreService ds, String start, String end) {
// Start with unrestricted namespace query
Query q = new Query(Entities.NAMESPACE_METADATA_KIND);
List<Filter> subFilters = new ArrayList();
// Limit to specified range, if any
if (start != null) {
subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createNamespaceKey(start)));
}
if (end != null) {
subFilters.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN_OR_EQUAL, Entities.createNamespaceKey(end)));
}
q.setFilter(CompositeFilterOperator.and(subFilters));
// Initialize result list
List<String> results = new ArrayList<String>();
// Build list of query results
for (Entity e : ds.prepare(q).asIterable()) {
results.add(Entities.getNamespaceFromNamespaceKey(e.getKey()));
}
// Return result list
return results;
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class MetadataKindsTest method printLowercaseKinds.
// [START kind_query_example]
void printLowercaseKinds(DatastoreService ds, PrintWriter writer) {
// Start with unrestricted kind query
Query q = new Query(Entities.KIND_METADATA_KIND);
List<Filter> subFils = new ArrayList();
// Limit to lowercase initial letters
subFils.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.GREATER_THAN_OR_EQUAL, Entities.createKindKey("a")));
// Character after 'z'
String endChar = Character.toString((char) ('z' + 1));
subFils.add(new FilterPredicate(Entity.KEY_RESERVED_PROPERTY, FilterOperator.LESS_THAN, Entities.createKindKey(endChar)));
q.setFilter(CompositeFilterOperator.and(subFils));
// Print heading
writer.println("Lowercase kinds:");
// Print query results
for (Entity e : ds.prepare(q).asIterable()) {
writer.println(" " + e.getKey().getName());
}
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class IndexesServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
// [START exploding_index_example_1]
Query q = new Query("Widget").setFilter(CompositeFilterOperator.and(new FilterPredicate("x", FilterOperator.EQUAL, 1), new FilterPredicate("y", FilterOperator.EQUAL, 2))).addSort("date", Query.SortDirection.ASCENDING);
// [END exploding_index_example_1]
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
PrintWriter out = resp.getWriter();
out.printf("Got %d widgets.\n", results.size());
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class ProjectionServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
PrintWriter out = resp.getWriter();
out.printf("Latest entries from guestbook: \n");
Key guestbookKey = KeyFactory.createKey("Guestbook", GUESTBOOK_ID);
Query query = new Query("Greeting", guestbookKey);
addGuestbookProjections(query);
printGuestbookEntries(datastore, query, out);
}
use of com.google.appengine.api.datastore.Query in project java-docs-samples by GoogleCloudPlatform.
the class ListPeopleServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE);
// If this servlet is passed a cursor parameter, let's use it.
String startCursor = req.getParameter("cursor");
if (startCursor != null) {
fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor));
}
Query q = new Query("Person").addSort("name", SortDirection.ASCENDING);
PreparedQuery pq = datastore.prepare(q);
QueryResultList<Entity> results;
try {
results = pq.asQueryResultList(fetchOptions);
} catch (IllegalArgumentException e) {
// IllegalArgumentException happens when an invalid cursor is used.
// A user could have manually entered a bad cursor in the URL or there
// may have been an internal implementation detail change in App Engine.
// Redirect to the page without the cursor parameter to show something
// rather than an error.
resp.sendRedirect("/people");
return;
}
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
PrintWriter w = resp.getWriter();
w.println("<!DOCTYPE html>");
w.println("<meta charset=\"utf-8\">");
w.println("<title>Cloud Datastore Cursor Sample</title>");
w.println("<ul>");
for (Entity entity : results) {
w.println("<li>" + entity.getProperty("name") + "</li>");
}
w.println("</ul>");
String cursorString = results.getCursor().toWebSafeString();
// This servlet lives at '/people'.
w.println("<a href='/people?cursor=" + cursorString + "'>Next page</a>");
}
Aggregations