[英]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.