首页 > 代码库 > google datastore filter

google datastore filter

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 Service
DatastoreService 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 filters
Filter heightRangeFilter =
  CompositeFilterOperator.and(heightMinFilter, heightMaxFilter);//一个查询对应多个Filter用CompositeFilter,搭建过滤器



Filter



// Use class Query to assemble a query
Query q = new Query("Person").setFilter(heightRangeFilter);

// Use PreparedQuery interface to retrieve results
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(firstName + " " + lastName + ", " + height + " inches tall");
}