Question

Ive tried a thousand things. As of right now the only way for me to query anything is to get the entire list and look through it that way! which takes way to much time. How can I query something in google app engine, for example pull only the entities that have > 100 votes for example.

Tried to user cursor but not sure how it works. I know it can use a cursor but how do I set it up with google app engine since my database isnt in my app per say??

Ive tried... but this dose not work at all..

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

and

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

Im following the tic-tac-toe example https://github.com/GoogleCloudPlatform/appengine-endpoints-tictactoe-java and https://developers.google.com/eclipse/docs/endpoints-addentities In the example I just switched notes for quotes.

Heres my current code for example on how im getting the entities.

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

        }  

 }

Here is the code that Google generated in the app engine for me when I created the endpoint. It looks like it will query somehow but I cant figure it out. They are two different projects.

@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();
Was it helpful?

Solution

In Google App Engine you need to set up a servlet to query the database for you and then return the results in JSON, see here for more information: 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/

You would end up querying using http:// your-url/query? + query string

EDIT: Preview!

This is a Preview release of Google Cloud Endpoints. As a result, the API is subject to change and the service itself is currently not covered by any SLA or deprecation policy. These characteristics will be evaluated as the API and service moves towards General Availability, but developers should take this into consideration when using the Preview release of Google Cloud Endpoints.

Most likely the cursor function is still in development. But I'm also unsure why you would want to use Cursors, as Collections are so much easier to work with... Wouldn't you prefer to do what's below then the awful code above? :)

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

OTHER TIPS

Update your list method to take in a filter attribute

@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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top