2013-07-20 18 views
4

我试过一千件东西。就目前而言,查询任何内容的唯一方法是获取整个列表并以这种方式查看它!这要花很多时间。我怎样才能在谷歌应用程序引擎中查询某些内容,例如,仅抽取具有> 100票的实体。使用光标从android应用程序中的谷歌应用程序引擎进行查询

尝试用户光标,但不知道它是如何工作的。我知道它可以使用游标,但我怎么设置它与谷歌应用程序引擎,因为我的数据库没有在我的应用程序每说?

我用尽...但这个剂量不工作..

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); 

     } 

} 

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

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

回答

3

在谷歌应用程序引擎,你需要建立一个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://你的网址/查询查询? +查询字符串

编辑: 预览!

这是Google Cloud Endpoints的预览版本。因此, API可能会发生变化,并且服务本身当前不是由任何SLA或弃用策略涵盖的 。这些特征将被评估为API和服务向着通用 可用性进行评估,但当 使用Google云端点预览版时,开发人员应考虑此因素。

最有可能的光标功能仍在开发中。但是我也不确定你为什么要使用Cursors,因为Collections更容易处理......你不喜欢做那些低于那么糟糕的代码吗? :)

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

嗯那么为什么谷歌提供的一组光标功能?我认为这是查询? – NightSkyCode

+1

回答编辑:) –

+0

嘿Rsen!即时通讯现在这样做!哈哈,我只是想通谷歌应用程序引擎没有做懒惰取!当我以这种方式运行它时,我认为它正在下载数据存储(数据库)中的每个对象!你知道它是否有用吗? – NightSkyCode

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