【问题标题】:Google App Engine Keys Only Query with CURSORGoogle App Engine Keys Only Query with CURSOR
【发布时间】:2018-06-15 10:35:28
【问题描述】:

我正在使用具有 10000 多个实体的 PersistenceManager 查询获取数据存储实体,我只想获取实体的键,因为我使用限制为 1000 的游标,但获取后游标为空

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 总是为空,有没有人提出这个问题或如何克服它

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

【问题讨论】:

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


    【解决方案1】:

    试试keys-only query

    Query<Key> query = Query.newKeyQueryBuilder().setKind("Task").build();
    

    【讨论】:

      【解决方案2】:

      我当天用下面的代码解决了上面的光标问题,谢谢

      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 解决了问题

      【讨论】:

        猜你喜欢
        • 2021-05-14
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        • 2012-06-13
        • 1970-01-01
        • 1970-01-01
        • 2018-02-14
        相关资源
        最近更新 更多