import com.google.appengine.api.datastore.DatastoreServiceFactory;import com.google.appengine.api.datastore.DatastoreService;import com.google.appengine.api.datastore.Query.Filter;import com.google.appengine.api.datastore.Query.FilterPredicate;import com.google.appengine.api.datastore.Query.FilterOperator;import com.google.appengine.api.datastore.Query.CompositeFilter;import com.google.appengine.api.datastore.Query.CompositeFilterOperator;import com.google.appengine.api.datastore.Query;import com.google.appengine.api.datastore.PreparedQuery;import com.google.appengine.api.datastore.Entity;
// Get the Datastore ServiceDatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Filter heightMinFilter = new FilterPredicate("height", FilterOperator.GREATER_THAN_OR_EQUAL, minHeight);//一个查询对应一个过滤器Filter用FilterPredicate;heightMaxFilter = new FilterPredicate("height", FilterOperator.LESS_THAN_OR_EQUAL, maxHeight);
//Use CompositeFilter to combine multiple filtersFilter heightRangeFilter = CompositeFilterOperator.and(heightMinFilter, heightMaxFilter);//一个查询对应多个Filter用CompositeFilter,搭建过滤器
Filter
// Use class Query to assemble a queryQuery q = new Query("Person").setFilter(heightRangeFilter);
// Use PreparedQuery interface to retrieve resultsPreparedQuery 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(firstName + " " + lastName + ", " + height + " inches tall");}
google datastore filter
时间: 2024-11-26 12:28:02