【问题标题】:How to avoid read before write for updates in google datastore?如何避免在写入谷歌数据存储更新之前读取?
【发布时间】:2022-01-20 13:11:11
【问题描述】:
KeyFactory keyFactory = datastore.newKeyFactory().setKind("keyKind");
Key key = keyFactory.newKey("keyName");
Entity entity = datastore.get(key);
if (entity != null) {
  System.out.println("Updating access_time for " + entity.getString("name"));
  entity = Entity.newBuilder(entity)
      .set("access_time", DateTime.now())
      .build();
  datastore.update(entity);
}

https://github.com/googleapis/java-datastore#updating-data ,因此必须先读后写。 可以在不读取谷歌数据存储的情况下进行更新吗?在某些情况下,我不在乎某些属性是否被覆盖。

【问题讨论】:

    标签: google-cloud-datastore


    【解决方案1】:

    如果你不需要知道实体是否存在并且可以覆盖数据,那么考虑使用upsert

    【讨论】:

    • upsertcreate-or-update 一样工作,但是,有没有办法在不读取整个文档的情况下仅更新某些字段(属性子集),通过保留其他领域。就像在 Cassandra 中所做的那样?
    • 不,Datastore 是一个文档数据库,而不是像 Cassandra 这样的宽列存储。要更新特定字段,您需要读取-修改-写入实体实体。如果您担心错过冲突,则应在事务中完成更新 (cloud.google.com/datastore/docs/concepts/transactions)。
    猜你喜欢
    • 2013-03-12
    • 2023-03-20
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    相关资源
    最近更新 更多