简体   繁体   English

将Google Gloud Datastore实体从一个名称空间复制到另一个名称空间

[英]Copying Google Gloud Datastore entities from one namespace to an other

I'm copying Google Cloud Datastore entities from one namespace to an other with java like this: 我正在使用Java将Google Cloud Datastore实体从一个名称空间复制到另一个名称空间,如下所示:

Key newKey = Key.newBuilder(oldEntity.getKey()).setNamespace(NEW_NAMESPACE).build();
datastore.put(Entity.newBuilder(oldEntity).setKey(newKey).build());

Since the entities have numerid id's generated by Datastore and the id's of the copied entities need to remain the same I need to also let Datastore know to allocate these id's, for that I'm using DatastoreService.allocateIdRange 由于实体具有Datastore生成的numerid ID,并且复制的实体的ID需要保持相同,因此我还需要让Datastore知道分配这些ID,为此我正在使用DatastoreService.allocateIdRange

But this is giving me an error: 但这给我一个错误:

Exceeded maximum allocated IDs 超过最大分配ID

Does this mean that there is no way to achieve what I'm trying to achieve? 这是否意味着没有办法实现我想要达到的目标?

EDIT 1 编辑1

The code that allocates the ids: 分配ID的代码:

@POST
public void post(
        @QueryParam("namespace") String namespace,
        @QueryParam("kind") String kind,
        @QueryParam("id") long id,
        @QueryParam("parentKind") String parentKind,
        @QueryParam("parentName") String parentName,
        @QueryParam("parentId") Long parentId
) {
    NamespaceManager.set(namespace);
    DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
    Key parent = null;
    if (parentKind != null) {
        if (parentName != null)
            parent = KeyFactory.createKey(parentKind, parentName);
        else
            parent = KeyFactory.createKey(parentKind, parentId);
    }
    ds.allocateIdRange(new KeyRange(parent, kind, id, id));
}

I think you can skip the key creation. 我认为您可以跳过密钥创建。 This is my code for updating namespaces: 这是我用于更新名称空间的代码:

GoogleCloudDatastore gds = new GoogleCloudDatastore();
Datastore ds = gds.getCredentials();

final KeyFactory keyFactory = ds.newKeyFactory().setKind(entityKind).setNamespace(NAME_SPACE);
Key newKey = ds.allocateId(oldEntity.getKey);
Entity newEntity= Entity.newBuilder(newKey).set("someParameterName", someParameterValue).build();

Hope this helps. 希望这可以帮助。

Here is an answer provided on Google's Issue Tracker: 这是Google问题追踪器上提供的答案

allocateIdRange only works with Sequential contiguous IDs and not Scattered IDs which are above the Sequential ID range. allocateIdRange仅适用于连续的连续ID,而不适用于超出连续ID范围的分散ID。

As explained in the 'Best Practices' documentation, if need be it is recommended to send an 'allocateIds' request to Datastore to find and allocate a contiguous range of unique IDs and use those to save entities to, instead of saving entities to the Datastore first which are auto-assigned Scattered IDs and cannot be allocated with allocateIdRange. “最佳做法”文档中所述,如果需要,建议将“ allocateIds”请求发送到数据存储区以查找并分配连续范围的唯一ID,并使用这些ID将实体保存到,而不是将实体保存到数据存储区第一个是自动分配的分散ID ,无法使用allocateIdRange进行分配。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM