本文整理汇总了Java中com.google.appengine.api.datastore.Query.addSort方法的典型用法代码示例。如果您正苦于以下问题:Java Query.addSort方法的具体用法?Java Query.addSort怎么用?Java Query.addSort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.appengine.api.datastore.Query
的用法示例。
在下文中一共展示了Query.addSort方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doGet
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
resp.getWriter().println("<html><body>");
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
Query query = new Query("SharedList");
query.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
List<Entity> results = datastore.prepare(query).asList(
FetchOptions.Builder.withDefaults());
for (Entity entity : results) {
String email = (String) entity.getProperty("email");
String productID = (String) entity.getKey().getName();
String productName = (String) entity.getProperty("productName");
Date sharedDate = (Date) entity.getProperty("sharedDate");
resp.getWriter().println(
email + " shared <a href=\"http://www.amazon.com/dp/"
+ productID + "\" target=\"_blank\">" + "<b>"
+ productName + "</b></a>!<br>");
}
resp.getWriter().println("</body></html>");
}
示例2: getEntitiesBrowser
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
@GET
@Produces(MediaType.TEXT_XML)
public List<SharedProduct> getEntitiesBrowser() {
List<SharedProduct> list = new ArrayList<SharedProduct>();
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
Query query = new Query("SharedList");
query.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
List<Entity> results = datastore.prepare(query).asList(
FetchOptions.Builder.withDefaults());
for (Entity entity : results) {
String email = (String) entity.getProperty("email");
String productID = (String) entity.getKey().getName();
String productName = (String) entity.getProperty("productName");
Date sharedDate = (Date) entity.getProperty("sharedDate");
list.add(new SharedProduct(email, productID, productName,
sharedDate));
}
return list;
}
示例3: getEntities
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<SharedProduct> getEntities() {
List<SharedProduct> list = new ArrayList<SharedProduct>();
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
Query query = new Query("SharedList");
query.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
List<Entity> results = datastore.prepare(query).asList(
FetchOptions.Builder.withDefaults());
for (Entity entity : results) {
String email = (String) entity.getProperty("email");
String productID = (String) entity.getKey().getName();
String productName = (String) entity.getProperty("productName");
Date sharedDate = (Date) entity.getProperty("sharedDate");
list.add(new SharedProduct(email, productID, productName,
sharedDate));
}
return list;
}
示例4: projectionQuery_grouping_filtersDuplicates
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
@Test
public void projectionQuery_grouping_filtersDuplicates() {
putTestData("some duplicate", 0L);
putTestData("some duplicate", 0L);
putTestData("too big", 1L);
// [START grouping]
Query q = new Query("TestKind");
q.addProjection(new PropertyProjection("A", String.class));
q.addProjection(new PropertyProjection("B", Long.class));
q.setDistinct(true);
q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L));
q.addSort("B", Query.SortDirection.DESCENDING);
q.addSort("A");
// [END grouping]
List<Entity> entities =
datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5));
assertThat(entities).hasSize(1);
Entity entity = entities.get(0);
assertThat((String) entity.getProperty("A")).named("entity.A").isEqualTo("some duplicate");
assertThat((long) entity.getProperty("B")).named("entity.B").isEqualTo(0L);
}
示例5: printPropertyRange
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
void printPropertyRange(DatastoreService ds, PrintWriter writer) {
// Start with unrestricted keys-only property query
Query q = new Query(Entities.PROPERTY_METADATA_KIND).setKeysOnly();
// Limit range
q.setFilter(
CompositeFilterOperator.and(
new FilterPredicate(
Entity.KEY_RESERVED_PROPERTY,
Query.FilterOperator.GREATER_THAN_OR_EQUAL,
Entities.createPropertyKey("Employee", "salary")),
new FilterPredicate(
Entity.KEY_RESERVED_PROPERTY,
Query.FilterOperator.LESS_THAN_OR_EQUAL,
Entities.createPropertyKey("Manager", "salary"))));
q.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
// Print query results
for (Entity e : ds.prepare(q).asIterable()) {
writer.println(e.getKey().getParent().getName() + ": " + e.getKey().getName());
}
}
示例6: projectionQuery_grouping_filtersDuplicates
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
@Test
public void projectionQuery_grouping_filtersDuplicates() {
putTestData("some duplicate", 0L);
putTestData("some duplicate", 0L);
putTestData("too big", 1L);
// [START grouping]
Query q = new Query("TestKind");
q.addProjection(new PropertyProjection("A", String.class));
q.addProjection(new PropertyProjection("B", Long.class));
q.setDistinct(true);
q.setFilter(Query.FilterOperator.LESS_THAN.of("B", 1L));
q.addSort("B", Query.SortDirection.DESCENDING);
q.addSort("A");
// [END grouping]
List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withLimit(5));
assertThat(entities).hasSize(1);
Entity entity = entities.get(0);
assertThat((String) entity.getProperty("A")).named("entity.A").isEqualTo("some duplicate");
assertThat((long) entity.getProperty("B")).named("entity.B").isEqualTo(0L);
}
示例7: getEntitiesBrowser
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
@GET
@Produces(MediaType.TEXT_XML)
public List<WishlistProduct> getEntitiesBrowser() {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user == null) {
System.out.println("Login first");
return null;
}
List<WishlistProduct> list = new ArrayList<WishlistProduct>();
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
Query query = new Query(user.getEmail());
query.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
List<Entity> results = datastore.prepare(query).asList(
FetchOptions.Builder.withDefaults());
for (Entity entity : results) {
String productID = (String) entity.getKey().getName();
String productName = (String) entity.getProperty("productName");
double currentPrice = (double) entity.getProperty("currentPrice");
double lowestPrice = (double) entity.getProperty("lowestPrice");
Date lowestDate = (Date) entity.getProperty("lowestDate");
list.add(new WishlistProduct(productID, productName, currentPrice,
lowestPrice, lowestDate));
}
return list;
}
示例8: getEntities
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public List<WishlistProduct> getEntities() {
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user == null) {
System.out.println("Login first");
return null;
}
List<WishlistProduct> list = new ArrayList<WishlistProduct>();
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
Query query = new Query(user.getEmail());
query.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
List<Entity> results = datastore.prepare(query).asList(
FetchOptions.Builder.withDefaults());
for (Entity entity : results) {
String productID = (String) entity.getKey().getName();
String productName = (String) entity.getProperty("productName");
double currentPrice = (double) entity.getProperty("currentPrice");
double lowestPrice = (double) entity.getProperty("lowestPrice");
Date lowestDate = (Date) entity.getProperty("lowestDate");
list.add(new WishlistProduct(productID, productName, currentPrice,
lowestPrice, lowestDate));
}
return list;
}
示例9: doGet
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/html");
resp.getWriter().println("<html><body>");
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
if (user == null) {
resp.getWriter().println("Please login first!");
resp.getWriter().println("</body></html>");
return;
}
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
Query query = new Query(user.getEmail());
query.addSort(Entity.KEY_RESERVED_PROPERTY, SortDirection.ASCENDING);
List<Entity> results = datastore.prepare(query).asList(
FetchOptions.Builder.withDefaults());
resp.getWriter().println("<table border=\"0\">");
resp.getWriter()
.println(
"<tr><b><td>Product name</td><td>Current price</td><td>History lowest</td></b></tr>");
int i = 0;
for (Entity entity : results) {
String productID = (String) entity.getKey().getName();
String productName = (String) entity.getProperty("productName");
double currentPrice = (double) entity.getProperty("currentPrice");
double lowestPrice = (double) entity.getProperty("lowestPrice");
Date lowestDate = (Date) entity.getProperty("lowestDate");
if (i % 2 == 0) {
resp.getWriter().println(
"<tr style=\"background-color: #f5f5f5\">");
} else {
resp.getWriter().println("<tr>");
}
++i;
resp.getWriter().println("<td>");
resp.getWriter().println(
"<a href=\"http://www.amazon.com/dp/" + productID
+ "\" target=\"_blank\">" + "<b>" + productName
+ "</b></a>");
resp.getWriter().println("</td>");
resp.getWriter().println("<td>");
resp.getWriter().println("$" + currentPrice);
resp.getWriter().println("</td>");
resp.getWriter().println("<td>");
resp.getWriter().println("$" + lowestPrice);
resp.getWriter().println("</td>");
resp.getWriter().println("</tr>");
}
resp.getWriter().println("</table>");
resp.getWriter().println("</body></html>");
}
示例10: getImpliedDominantAttributeFilter
import com.google.appengine.api.datastore.Query; //导入方法依赖的package包/类
/**
* If there is no filter criteria on the dominant sort attribute, we need
* to query the database to find the min/max value for the attribute and then
* apply that to the query. This is a work-around for broken GAE functionality.
*
* @return null if no records in table; otherwise, produce the needed filter.
* @throws ODKDatastoreException
*/
private SimpleFilterTracker getImpliedDominantAttributeFilter() throws ODKDatastoreException {
// GAE production throws an exception when a query with equality
// filters and a sort on a column not constrained by any filter
// is issued.
//
// To work around this, issue a query to return the current min/max
// value of the dominant sort attribute and apply a GE/LE
// constraint using that value. This is effectively a no-op,
// but keeps GAE happy.
//
// this is the dominant sort:
SortTracker dominantSort = sortList.get(0);
DataField dominantSortAttr = dominantSort.getAttribute();
SortDirection dominantSortDirection =
dominantSort.direction.equals(Direction.ASCENDING)
? SortDirection.ASCENDING
: SortDirection.DESCENDING;
DatastoreService ds = datastore.getDatastoreService();
EntityRowMapper m = new EntityRowMapper(relation, user);
Query orderingHack = new com.google.appengine.api.datastore.Query(
relation.getSchemaName() + "." + relation.getTableName());
orderingHack.addSort(dominantSort.getAttribute().getName(), dominantSortDirection);
PreparedQuery orderingPrep = ds.prepare(orderingHack);
logger.debug("hqrLoop: finding min/max in " + relation.getSchemaName() + "."
+ relation.getTableName() + " of dominantSortAttr: " + dominantSortAttr.getName());
List<com.google.appengine.api.datastore.Entity> values = orderingPrep
.asList(FetchOptions.Builder.withDefaults().prefetchSize(1).chunkSize(20).limit(3));
datastore.recordQueryUsage(relation, values.size());
if (values == null || values.isEmpty()) {
// the table is empty -- no need to go further...
return null;
}
Object dominantFilterValue = null;
// and apply the filter...
try {
CommonFieldsBase odkEntity = (CommonFieldsBase) m.mapRow(datastore, values.get(0), 0);
dominantFilterValue = EngineUtils.getDominantSortAttributeValue(odkEntity, dominantSortAttr);
} catch (SQLException e) {
throw new ODKDatastoreException("[" + loggingContextTag + "] Unable to complete request", e);
}
SimpleFilterTracker impliedDominantFilter = constructFilter(dominantSortAttr,
dominantSort.direction.equals(Direction.ASCENDING)
? FilterOperation.GREATER_THAN_OR_EQUAL
: FilterOperation.LESS_THAN_OR_EQUAL, dominantFilterValue );
return impliedDominantFilter;
}