【问题标题】:Impact of migrating app from GAE M/S datastore to HRD将应用程序从 GAE M/S 数据存储迁移到 HRD 的影响
【发布时间】:2011-08-15 18:16:02
【问题描述】:

将应用程序从 M/S 数据存储迁移到 HRD 时,需要避免一些陷阱。我在一个这样的特定区域有一个问题,它说“同一种类的实体 ID 并不总是唯一的”。

为了进一步解释,这里有一个例子。

  1. 假设我有 3 个实体客户、联系人、地址,如下所示
  2. 联系人实体引用客户使用客户密钥作为 com.google.appengine.api.datastore.Key
  3. 地址实体引用客户使用客户密钥作为 Long

这 3 个类是:

public class Customer {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key id;

  @Persistent
  private String name;
}

public class Contact {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key id;

  @Persistent
  private String name;

  @Persistent
  private Key customerId;
}


public class Address {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key id;

  @Persistent
  private String address;

  @Persistent
  private Long customerId;
}

所有实体都是根实体。

现在,当我们迁移联系人和地址实体中的 customerId 时会发生什么?它们还能工作吗?还是在迁移之前我们需要对它们做一些特别的事情?

谢谢!

【问题讨论】:

    标签: google-app-engine


    【解决方案1】:

    您所指的是与密钥的构建方式有关。实体的密钥将由:

    • 您的应用程序 ID
    • 当前命名空间
    • 父级层次结构<-- this allows ids to be non-unique within a kind
    • 种类名称
    • 给定的key_nameid

    因此,要使密钥独一无二,其中任何一个部分都可能发生变化。在单一种类中,在您的应用程序的单一命名空间中,ID 可能不唯一的唯一时间是您为该实体设置了parent

    这意味着您定义的所有根实体将具有唯一的 ID/名称

    如果您需要保证分配的 id 在一个种类中是唯一的,即使在具有祖先层次结构的实体之间也是如此,您可以;

    1. 重新思考您的设计。如果您通过 id 跨实体组引用实体而不需要/拥有父级,则您可能在不需要它们的地方应用了实体组
    2. 您可以根据 Kind 的根版本手动 allocateIds

    【讨论】:

    • 感谢您的详细解释。由于我们所有的实体都是根实体,我们可以继续使用 pm.newQuery(, "where customerId = ") 和 HRD,而不用担心重复键。它的另一面将是最终的一致性。数据是否需要 2 到 5 秒才能显示在整个 HRD 中?
    • 直接通过 id(因此是键)获取实体始终是高度一致的,最终一致性只会在对多个实体组进行查询时表现出来。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2020-02-06
    相关资源
    最近更新 更多