我想要获取Google Datastore (我不能使用uuid)自动生成的Google Datastore的实体Ids,它是使用python在Apache光束管道中生成的。
在Datastore中构建实体时,我使用以下代码传递实体种类和键值。
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的生成。请帮帮忙。
发布于 2019-08-31 03:36:54
这是由sdk强加的人为限制,通过WriteToDatastore中的以下if语句
if client_entity.key.is_partial:
raise ValueError('Entities to be written to Cloud Datastore must '
'have complete keys:\n%s' % client_entity)数据流步骤可以重新运行几次,所以我认为这个限制是为了防止您在数据存储中生成重复的条目。
WriteToDatastore背后的函数是来自新的“云数据存储”库https://googleapis.dev/python/datastore/latest/index.html的batch.commit() (在apache_beam/io/gcp/datastore/v1/helper.py的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
发布于 2019-09-04 08:49:28
在云数据存储中,如果在创建实体时省略了实体的数字id,则数据存储将使用“kind”生成一个。
云数据存储客户端应用编程接口中的allocateId()允许您基于不完整/部分键生成is块,通常是在仅指定名称空间的情况下。然后可以将其用作分配id的基础。
更多信息以及一些代码片段可以在https://cloud.google.com/datastore/docs/concepts/entities#kinds_and_identifiers找到,也可以在
有关google cloud datastore helper的.add_key_path的使用信息,可以在https://github.com/GoogleCloudPlatform/google-cloud-datastore/blob/master/python/googledatastore/helper.py上找到helper.py脚本。
请参考第153行。
https://stackoverflow.com/questions/57729844
复制相似问题