Google App Engine仅使用CURSOR查询

时间:2018-06-15 10:35:28

标签: google-app-engine google-cloud-datastore datastore

我使用拥有超过10000个实体的PersistenceManager查询获取数据存储区实体,我想只获取实体的键,因为我使用限制1000的游标,但是光标是获取后为null

PersistenceManager       pm     =   PMF.get().getPersistenceManager();
      Query                       q     = null;
      List<String>           idList     = null;
      int               cursorLimit     =   1000;
      Map<String, Object> cursorMap     =   null;
      Cursor              cursorObj     =   null;
      CommonUtil            utility     =   new CommonUtil();
      HashMap<String, Object> responseMap = null;



        responseMap = new HashMap<String, Object>();
        Query   q   = pm.newQuery("select id from " + Person.class.getName());

        if( requestMap.containsKey("cursor") )
        {
            cursorMap     = new HashMap<String,Object>();
            cursorObj     = Cursor.fromWebSafeString( String.valueOf( requestMap.get("cursor") )  );
            cursorMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursorObj);
            q.setExtensions(cursorMap);
        }
        q.setRange(0, cursorLimit);
        idList = (List<String>) q.execute();
        cursorObj       = JDOCursorHelper.getCursor(idList);

        responseMap.put("contactIdList", idList);

        if(idList.size() == cursorLimit && cursorObj != null)
            responseMap.put("cursor", cursorObj.toWebSafeString());
        else
            responseMap.put("cursor", "");

        q.closeAll();
        pm.close();

但是cursorObj总是为null,有没有人提出这个问题或如何克服它

光标在获取整个实体时工作正常,但在键中仅查询它不能按预期工作

2 个答案:

答案 0 :(得分:0)

试试keys-only query

new Observable().toPromise();

答案 1 :(得分:0)

同一天,我用以下代码解决了上述光标问题,谢谢

final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
          FetchOptions fetchOptions = FetchOptions.Builder.withLimit(cursorLimit);

          if( requestMap.containsKey("cursor"))
          {
              fetchOptions.startCursor(Cursor.fromWebSafeString(requestMap.get("cursor") ));
          }

            q = new com.google.appengine.api.datastore.Query("Contact").setKeysOnly();              
            PreparedQuery pq = datastore.prepare(q);                                
            results = pq.asQueryResultList(fetchOptions);               
            cursorObj = results.getCursor();

此LowLevel API解决了问题