我正在尝试执行批量删除。但是,我无法弄清楚如何将实体列表转换为键然后批量删除。数据存储区中是否有可用的特殊方法?
我正在尝试:
Query q = new Query("ENTITY_NAME").setAncestor(key).setKeysOnly();
PreparedQuery pq = datastore.prepare(q);
QueryResultIterable<Entity> result = pq.asQueryResultIterable(FetchOptions.Builder.withChunkSize(500));
datastore.delete(result); // somehow convert iterable entities to iterable key??
我现在有:
Query q = new Query("ENTITY_NAME").setAncestor(key).setKeysOnly();
PreparedQuery pq = datastore.prepare(q);
QueryResultIterable<Entity> result = pq.asQueryResultIterable(FetchOptions.Builder.withChunkSize(500));
QueryResultIterator<Entity> iterator = result.iterator();
// delete one at a time
while (iterator.hasNext()) {
datastore.delete(iterator.next().getKey());
}
答案 0 :(得分:2)
我已将代码更改为以下内容,但我还没有测试过,我不确定是否有更好的方法:
List<Key> keys = new ArrayList<Key>();
QueryResultIterator<Entity> iterator = result.iterator();
while (iterator.hasNext()) {
keys.add(iterator.next().getKey());
}
datastore.delete(keys);