首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google Datastore自动实体ID生成

Google Datastore自动实体ID生成
EN

Stack Overflow用户
提问于 2019-08-30 23:49:27
回答 2查看 611关注 0票数 0

我想要获取Google Datastore (我不能使用uuid)自动生成的Google Datastore的实体Ids,它是使用python在Apache光束管道中生成的。

在Datastore中构建实体时,我使用以下代码传递实体种类和键值。

代码语言:javascript
复制
from googledatastore import helper as datastore_helper

entity = entity_pb2.Entity()
        datastore_helper.add_key_path(entity.key, entityName.get(), str(uuid.uuid4()))

在上面的代码中,我不能使用uuid.uuid4()根据项目需求随机生成唯一的Ids。我必须使用Google Datastore的自动Id生成。经过大量阅读之后,我仍然不确定应该向数据存储助手传递什么,以使其在不使用uuid的情况下处理唯一id的生成。请帮帮忙。

EN

回答 2

Stack Overflow用户

发布于 2019-08-31 03:36:54

这是由sdk强加的人为限制,通过WriteToDatastore中的以下if语句

代码语言:javascript
复制
  if client_entity.key.is_partial:
    raise ValueError('Entities to be written to Cloud Datastore must '
                     'have complete keys:\n%s' % client_entity)

https://beam.apache.org/releases/pydoc/2.14.0/_modules/apache_beam/io/gcp/datastore/v1new/datastoreio.html#WriteToDatastore

数据流步骤可以重新运行几次,所以我认为这个限制是为了防止您在数据存储中生成重复的条目。

WriteToDatastore背后的函数是来自新的“云数据存储”库https://googleapis.dev/python/datastore/latest/index.htmlbatch.commit() (在apache_beam/io/gcp/datastore/v1/helper.pywrite_mutations中使用):

https://beam.apache.org/releases/pydoc/2.14.0/_modules/apache_beam/io/gcp/datastore/v1new/helper.html#write_mutations

云数据存储库中的主要Client()类有一个allocate_ids()函数:https://googleapis.dev/python/datastore/latest/client.html?highlight=allocate_ids#google.cloud.datastore.client.Client.allocate_ids

在将实体传递给WriteToDatastore之前,您应该能够使用它为实体自动生成ids

票数 1
EN

Stack Overflow用户

发布于 2019-09-04 08:49:28

在云数据存储中,如果在创建实体时省略了实体的数字id,则数据存储将使用“kind”生成一个。

云数据存储客户端应用编程接口中的allocateId()允许您基于不完整/部分键生成is块,通常是在仅指定名称空间的情况下。然后可以将其用作分配id的基础。

更多信息以及一些代码片段可以在https://cloud.google.com/datastore/docs/concepts/entities#kinds_and_identifiers找到,也可以在

https://medium.com/google-cloud/entity-groups-ancestors-and-indexes-in-datastore-a-working-example-3ee40cc185ee

有关google cloud datastore helper的.add_key_path的使用信息,可以在https://github.com/GoogleCloudPlatform/google-cloud-datastore/blob/master/python/googledatastore/helper.py上找到helper.py脚本。

请参考第153行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57729844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档