【问题标题】:use cursor to query from google app engine in android在android中使用光标从谷歌应用引擎查询
【发布时间】:2013-07-20 04:45:01
【问题描述】:

我已经尝试了一千种方法。截至目前,我查询任何内容的唯一方法是获取整个列表并以这种方式查看!这需要很多时间。如何在谷歌应用引擎中查询某些内容,例如仅拉取超过 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();

【问题讨论】:

    标签: android database web-services google-app-engine android-cursor


    【解决方案1】:

    在 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 和服务向 General 移动而进行评估 可用性,但开发人员应考虑到这一点 使用 Google Cloud Endpoints 的预览版。

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

    ScoreCollection scores = service.scores().list().execute();
    

    【讨论】:

    • hmm 那么为什么 google 提供设置光标功能呢?我以为那是为了查询?
    • 嘿,Rsen!我目前正在这样做!哈哈,我只是想谷歌应用引擎没有做惰性获取!我以为当我以这种方式运行它时,它正在下载数据存储(数据库)中的每个对象!你知道有没有?
    • @NightSky 这很奇怪,似乎它绝对应该这样做,但我猜是因为它处于预览状态,他们还没有添加它?相反,我会考虑缓存数据,或者知道你需要什么数据并且只得到它(即分页)
    • 我现在正在做分页。如果我的实体超过 10 万,这只会让我担心……如果有什么东西变得那么大,我想我会担心的。感谢您的帮助,迄今为止最好的答案。不敢相信 google dosnt 有一种方法可以在不使用 10 个不同的库和数百行代码的情况下从 android 查询.. 太奇怪了。猜猜gae还是太新了。
    【解决方案2】:

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

    @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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多