简体   繁体   English

如何在AppEngine数据存储区上执行批量更新

[英]How to perform bulk update on AppEngine Datastore

The following code is not working. 以下代码不起作用。 Does anyone know how I might get it to work? 有谁知道我如何使它工作?

Query q = new Query("Product");
    Iterable<Entity> entities = datastore.prepare(q).asIterable();
    for (Entity entity : datastore.prepare(q).asIterable()) {
        entity.setProperty(“sale”, false);
    }
    datastore.put(entities);

sale is a completely new field that I am adding to the entity kind. sale是我要添加到实体种类中的全新领域。 So it does not exist yet. 因此它尚不存在。

UPDATE UPDATE

I fixed it as below but the code is still not working 我如下修复,但代码仍然无法正常工作

Query q = new Query("Product");
    Iterable<Entity> entities = datastore.prepare(q).asIterable();
    for (Entity entity : entities) {
        entity.setProperty(“sale”, false);
    }
    datastore.put(entities);

There is an error in your code. 您的代码中有错误。 You never update entities . 您永远不会更新entities It should be: 它应该是:

Query q = new Query("Product");
List<Entity> entities = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());

for (Entity entity : entities) {
    entity.setProperty(“sale”, false);
}
datastore.put(entities);

Maybe someone else can explain to you why exactly it does not work, but I know how to make it work. 也许别人可以给你解释到底为什么它不工作,但我知道如何使它发挥作用。

For some reason the entities iterable does not behave like a proper Java collection. 由于某些原因,可迭代的entities行为不像适当的Java集合。 In a Java collection, the elements are pointers. 在Java集合中,元素是指针。 But for whatever reason, here each entity that you get inside the for-loop is an independent deep copy. 但是无论出于何种原因,这里您进入for循环的每个实体都是一个独立的深层副本。 So instead, do the following and it will work 因此,请执行以下操作,它将起作用

    Query q = new Query("Product");
    List<Entity> products = new ArrayList<Entity>();
    for (Entity entity : datastore.prepare(q).asIterable()) {
        entity.setProperty("sale", false);
        products.add(entity);
    }
    datastore.put(products);

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

相关问题 如何以编程方式更新Appengine数据存储区中的数据? - How to update data in appengine datastore programmatically? 如何使用 Java 对 MongoDB 中的文档执行批量更新? - How to perform a bulk update of documents in MongoDB with Java? 如何使用mapreduce批量更新满足查询的数据存储区实体? - How to use mapreduce to bulk update datastore entities that satisfy a query? 如何使用Java在MongoDB 3中使用多个过滤器执行文档的批量更新 - How to perform a bulk update of documents with multiple filters in MongoDB 3 with Java 如何在Appengine数据存储区中应用依赖的文件过滤器? - How to apply dependent fileds filters in appengine datastore? 如何在AppEngine数据存储区中存储单个实例对象 - How to store a single instance object in the AppEngine datastore 如何在Java中设置Appengine DataStore的loglevel - How to set loglevel of the Appengine DataStore in Java Appengine数据存储区父级或索引 - Appengine datastore parent or index 如何从AppEngine访问Cloud Datastore? - How do I access Cloud Datastore from AppEngine? 如何使用Java JDO 3将创建的/ lastUpdate字段存储在AppEngine DataStore中? - How to store created/lastUpdate fields in AppEngine DataStore using Java JDO 3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM