4

我已经尝试了一千件事。截至目前,我查询任何内容的唯一方法是获取整个列表并以这种方式查看!这需要很多时间。我如何在谷歌应用程序引擎中查询某些内容,例如仅提取具有 > 100 票的实体。

尝试使用用户光标,但不确定它是如何工作的。我知道它可以使用光标,但我如何使用谷歌应用引擎设置它,因为我的数据库不在我的应用程序中?

我试过了……但是这个剂量根本不起作用……

Cursor cursor = ("select * from Votes WHERE Votes >" + 250 , null);
quotes endpoint.listquotes().setCursor(cursor).execute();

  String query  = ("select * from Votes WHERE Votes >= 40");
    quotes endpoint.listquotes().setCursor(query).execute();

我关注井字游戏示例https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-javahttps://developers.google.com/eclipse/docs/endpoints-addentities在示例中我刚刚切换报价注释。

这是我当前的代码,例如关于我如何获取实体的代码。

 protected CollectionResponseQuotes doInBackground(Context... contexts) {

  Quotesendpoint.Builder endpointBuilder = new Quotesendpoint.Builder(
      AndroidHttp.newCompatibleTransport(),
      new JacksonFactory(),
      new HttpRequestInitializer() {
      public void initialize(HttpRequest httpRequest) { }
      });
  Quotesendpoint endpoint = CloudEndpointUtils.updateBuilder(
  endpointBuilder).build();
  try {


  quotes = endpoint.listquotes().execute();

   for (Quotes quote : quotes.getItems()) {

       if (quote.getVotes() > 3) {

       quoteList.add(quote);

        }  

 }

这是我在创建端点时 Google 在应用引擎中为我生成的代码。看起来它会以某种方式查询,但我无法弄清楚。它们是两个不同的项目。

@Api(name = "quotesendpoint", namespace = @ApiNamespace(ownerDomain = "projectquotes.com"           ownerName = "projectquotes.com", packagePath = ""))
 public class quotesEndpoint {

/**
 * This method lists all the entities inserted in datastore.
 * It uses HTTP GET method and paging support.
 *
 * @return A CollectionResponse class containing the list of all entities
 * persisted and a cursor to the next page.
 */
@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listquotes")
public CollectionResponse<quotes> listquotes(
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

 EntityManager mgr = null;
Cursor cursor = null;
List<quotes> execute = null;

try {
    mgr = getEntityManager();
    Query query = mgr.createQuery("select from quotes as quotes");
    if (cursorString != null && cursorString != "") {
        cursor = Cursor.fromWebSafeString(cursorString);
        query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
    }

    if (limit != null) {
        query.setFirstResult(0);
        query.setMaxResults(limit);
    }

    execute = (List<quotes>) query.getResultList();
    cursor = JPACursorHelper.getCursor(execute);
    if (cursor != null)
        cursorString = cursor.toWebSafeString();

    // Tight loop for fetching all entities from datastore and accomodate
    // for lazy fetch.
    for (quotes obj : execute)
        ;
} finally {
    mgr.close();
}

return CollectionResponse.<quotes> builder().setItems(execute)
        .setNextPageToken(cursorString).build();
4

2 回答 2

3

在 Google App Engine 中,您需要设置一个 servlet 来为您查询数据库,然后以 JSON 格式返回结果,请参阅此处了解更多信息: https ://developers.google.com/appengine/docs/java/datastore/queries https://github.com/octo-online/robospice https://developers.google.com/appengine/docs/java/#Requests_and_Servlets https://code.google.com/p/google-gson/

您最终会使用 http://your-url/query 进行查询?+ 查询字符串

编辑: 预览!

这是 Google Cloud Endpoints 的预览版。因此,API 可能会发生变化,并且服务本身目前不在任何 SLA 或弃用政策中。这些特性将在 API 和服务迈向通用可用性时进行评估,但开发人员在使用 Google Cloud Endpoints 的预览版时应考虑到这一点。

游标功能很可能仍在开发中。但我也不确定你为什么要使用游标,因为集合更容易使用......你不想做下面的事情而不是上面的糟糕代码吗?:)

ScoreCollection scores = service.scores().list().execute();
于 2013-07-22T23:28:19.133 回答
1

更新您的列表方法以获取过滤器属性

@SuppressWarnings({ "unchecked", "unused" })
@ApiMethod(name = "listZeppaUserInfo")
public CollectionResponse<ZeppaUserInfo> listZeppaUserInfo(
        @Nullable @Named("filter") String filterString,
        @Nullable @Named("cursor") String cursorString,
        @Nullable @Named("limit") Integer limit) {

    PersistenceManager mgr = null;
    Cursor cursor = null;
    List<ZeppaUserInfo> execute = null;

    try {
        mgr = getPersistenceManager();
        Query query = mgr.newQuery(ZeppaUserInfo.class);
        if (isWebSafe(cursorString)) {
            cursor = Cursor.fromWebSafeString(cursorString);
            HashMap<String, Object> extensionMap = new HashMap<String, Object>();
            extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor);
            query.setExtensions(extensionMap);
        } else if (isWebSafe(filterString)){
            // query has a filter
            query.setFilter(filterString);
        }

        if (limit != null) {
            query.setRange(0, limit);
        }

        execute = (List<ZeppaUserInfo>) query.execute();
        cursor = JDOCursorHelper.getCursor(execute);
        if (cursor != null)
            cursorString = cursor.toWebSafeString();

        // Tight loop for fetching all entities from datastore and
        // accomodate
        // for lazy fetch.
        for (ZeppaUserInfo obj : execute)
            ;
    } finally {
        mgr.close();
    }

    return CollectionResponse.<ZeppaUserInfo> builder().setItems(execute)
            .setNextPageToken(cursorString).build();
}
于 2014-10-30T16:45:43.777 回答