简体   繁体   English

Objectify - 我们可以为 QueryKeys 设置 startAt(cursor) 吗? 或者如何迭代许多 Keys 查询?

[英]Objectify - can we set a startAt(cursor) for QueryKeys? Or how to iterate over many Keys query?

I'm trying to iterate over 1000 keys of an entity at a time using this code but unlike Query , QueryKeys does not have a startAt() method for me to set it's cursor:我正在尝试使用此代码一次迭代超过 1000 个实体键,但与Query不同, QueryKeys没有startAt()方法让我将其设置为 cursor:

QueryKeys<Car> queryKeys = ofy().load().type(Car.class)
    .limit(1000)
     .keys();

// queryKeys has no startAt()
queryKeys = queryKeys.startAt(cursor)

Is there a way to loop through keys with QueryKeys just like Query ?有没有办法像Query一样使用QueryKeys遍历键?

The reason I want to loop through keys is that I need to delete those entities from the Datastore.我想遍历键的原因是我需要从数据存储中删除这些实体。 I might be deleting 100k - 1 million entities of a single entity type and wanted to do it in chunks because I am afraid that loading that many keys may slow things down too much or maybe error out somehow.我可能会删除 100k - 100 万个单一实体类型的实体,并希望分块进行,因为我担心加载这么多键可能会减慢速度,或者可能会以某种方式出错。

You'll want to set the cursor before calling keys, eg objectify.get().load().type(Car.class).setStartCursor(...).limit(1000).keys() .您需要在调用键之前设置 cursor,例如objectify.get().load().type(Car.class).setStartCursor(...).limit(1000).keys()

Also, you are best to use the cursor from the previous query instead of relying on starting the query again to avoid skipping over tombstones as noted at https://cloud.google.com/datastore/docs/best-practices#deletions .此外,您最好使用上一个查询中的 cursor 而不是依赖再次启动查询以避免跳过墓碑,如https://cloud.google.com/datastore/docs/best-practices#deletions中所述。

If you delete a page synchronously, there's not need for pagination:如果您同步删除页面,则不需要分页:

 public void deleteAllEntities() {
    boolean continueDeletionLoop = true;

    while (continueDeletionLoop) {
        QueryKeys<Car> queryKeys = objectify.get().load().type(Car.class)
            .limit(1000)
            .keys();

        if (queryKeys.list().isEmpty()) {
            continueDeletionLoop = false;
        } else {
            objectify.get().delete().keys(queryKeys).now();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 排序顺序在 Objectify 查询中被忽略 - Sort order is ignored in Objectify query 如何遍历 Firebase 实时数据库中多个推送键的子项? - How can I iterate through children of multiple push keys in Firebase Realtime Database? 我们可以在谷歌云 https 负载均衡器中配置多少个前端公共 ip? - How many frontend public ip can we configure in google cloud https load balancer? 我们如何在 ios swift 上对 firebase / firestore 查询进行分页? - How can we paginate a firebase / firestore query on ios swift? Firebase RTDB:游标的第二个参数(startAt,endAt ...)究竟是如何工作的? - Firebase RTDB: How exactly the 2nd argument of cursors(startAt, endAt...) work? 如何在 Cloud Firestore 中查询不存在的文档键 - How to query Cloud Firestore for non-existing keys of documents 我们如何查看由 Google Cloud BigQuery 执行的查询计划? - How do we see a query plan executed by Google Cloud BigQuery? 来自多个键的 Dynamodb 查询 - Dynamodb query from multiple keys GAE 缓存对象化查询 - GAE caching objectify queries 作为对整个实例没有管理员权限的用户,如何为 GitLab 上的所有新项目设置默认分支名称? - How can I set the default branch name for all new projects on GitLab, as an user without admin right over entire instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM