GAE Query Filter, Sort, Limit, Paging

Categories: Development Notes; Tagged with: ; @ April 20th, 2011 22:37

GAE关于Java的资料这是不多, 而且多数都要翻墙, 昨晚做个分页, 差点累死啊!

闲言碎语不多说, 上代码:

DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query("User");
		query.addFilter("name", FilterOperator.NOT_EQUAL, ""); // 设置Filter
		query.addSort("name", SortDirection.ASCENDING); // 要设置Sort, 则Filter的Attribute必须Sort, 且要放在其他Sort之前(Attribute要Index)
		query.addSort("ip", SortDirection.DESCENDING); // 设置Sort, 要在Filter对应Attribute的Sort之后
		PreparedQuery pq = datastoreService.prepare(query);
		List listUsers = pq.asList(FetchOptions.Builder.withLimit(2)); // 设置Limit
 
Properties in Inequality Filters Must Be Sorted before Other Sort Orders

If a query has both a filter with an inequality comparison and one or more sort orders, the query must include a sort order for the property used in the inequality, and the sort order must appear before sort orders on other properties.

This query is not valid, because it uses an inequality filter and does not order by the filtered property;

文档: http://code.google.com/appengine/docs/java/datastore/queries.html#Restrictions_on_Queries

翻不了墙的同学可以看这个 – 主要是分页: 

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.QueryResultList;

public class ListPeopleServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
          throws ServletException, IOException {

        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Query q = new Query("Person");
        PreparedQuery pq = datastore.prepare(q);
        int pageSize = 15;

        resp.setContentType("text/html");
        resp.getWriter().println("
    "); FetchOptions fetchOptions = FetchOptions.Builder.withLimit(pageSize); String startCursor = req.getParameter("cursor"); // If this servlet is passed a cursor parameter, let's use it if (startCursor != null) { fetchOptions.startCursor(Cursor.fromWebSafeString(startCursor)); } QueryResultList results = pq.asQueryResultList(fetchOptions); for (Entity entity : results) { resp.getWriter().println("
  • " + entity.getProperty("name") + ""); } resp.getWriter().println("
"); String cursor = results.getCursor().toWebSafeString(); // Assuming this servlet lives at '/people' resp.getWriter().println( "Next page"); } }

 

关于分页的另一篇:

Implementing Paging on Google App Engine Java (gae/j) and GWT — the query cursor way

Google app engine 设置Cron Jobs GAE设置定时任务(Java版)

Categories: Development Notes; Tagged with: ; @ April 20th, 2011 21:24

GAE的 Cron Job是通过配置文件实现的 – “Cron jobs are defined in cron.yaml (Python) or cron.xml (Java)”

Java版的cron.xml配置文件位于WEB-INFO目录下(位置同web.xml), 典型的一个:

<?xml version=”1.0″ encoding=”UTF-8″?>
<cronentries>
  <cron>
    <url>/GetIPLocation</url>
    <description>get the ip location by api every 2 minutes</description>
    <schedule>every 2 minutes</schedule>
  </cron>
</cronentries>

该任务每两分钟执行一次, GepIPLocation是一个Servlet.

时间格式举例:

every 12 hours
every 5 minutes from 10:00 to 14:00
2nd,third mon,wed,thu of march 17:00
every monday 09:00
1st monday of sep,oct,nov 17:00
every day 00:00

API地址: http://code.google.com/appengine/docs/java/config/cron.html (可能需要翻墙)

Google App Engine新手入门教程(Java) 留言并发送邮件实例

Categories: Development Notes; Tagged with: ; @ March 27th, 2010 16:54

在这个漫山遍野都是架构师的狗屎年代, 不懂点高科技, 连挑大粪的工作都找不上.

 

于是我今天稍微了解了下GAE, 俺也写个小教程, 否则, 或许在日后的口水战中, 会被人质问:"你为社区做过多少贡献?"(Flash程序员可点此看口水战).

先说已有资源跟文档:

Getting Started(使用入门) 是有中文版的, 因此无需再去找翻译的入门资料, 更为重要的是, 英语也不难; 对于大多数人来说, 也就是读汉语更快一些吧 =>看文档<=

除了使用入门之外, 还有一个讲基于GAE Java开发的概览: http://code.google.com/intl/zh-CN/appengine/docs/java/overview.html

当然, 文档中写的很全了, 我只是总结下而已:

1. 首先搭建环境, 这年头什么都简单了, 直接用Eclipse Update 就可: http://code.google.com/intl/zh-CN/appengine/docs/java/tools/eclipse.html#Installing_the_Google_Plugin_for_Eclipse

update完毕后, 会重启Eclipse.  这里一次可选SDK和Plugin都有了.

2. 创建新"Web Application Project"工程(注意Google前有Google标志), 注意:

取消选中“使用 Google Web 工具包”,并确保选中了“使用 Google App Engine”

3. 先创建个最简单的Servlet, 配置web.xml, 怕不行, 就现在本地debug一下

4. 配置ApplicationID, 在appengine-web.xml中, 在application标签中间填入你的Application名称.

5. 点"Deploy App Engine Project",填入用户名密码.
image

6. 看Console提示信息:

Closing update: new version is ready to start serving.
Uploading index definitions.
Deployment completed successfully

7. 进入app.appsopt.com, 查看该应用.

如: http://xjulab.appspot.com/

实例代码:

按照入门文档中的留言本制作, 一个Entity, 同时在保存成功后, 发送邮件到指定邮箱, 仅供初学者学习参考.

下载地址: http://www.box.net/shared/8x016g2bpq



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.