Question: I want to get the entity Ids of Google Datastore auto generated by Google Datastore (I can't use uuid) in Apache Beam pipeline using python.

I'm using the following code to pass the entity kind and key values when building the entities in Datastore.

```
from googledatastore import helper as datastore_helper

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

```

in the above code, I can't use `uuid.uuid4()` to randomly generate unique Ids according to the project requirement. I have to use Google Datastore's auto Id generation. After a lot of reading, I am still unsure what to pass to the datastore helper to make it take care of the unique id generation without using uuids. Please help.

Answer: 
It's an artifical limitation imposed by the sdk, by this `if` statement inside `WriteToDatastore`:

```
  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>

Dataflow steps can be re-run a couple of times, so i think this limitation is there to prevent you from generating duplicate entries in your datastore.

The function behind `WriteToDatastore` is `batch.commit()` from the new 'cloud datastore' library <https://googleapis.dev/python/datastore/latest/index.html> (which is used in `write_mutations` in `apache_beam/io/gcp/datastore/v1/helper.py`):

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

The primary `Client()` class in the Cloud Datastore library has an `allocate_ids()` function:
<https://googleapis.dev/python/datastore/latest/client.html?highlight=allocate_ids#google.cloud.datastore.client.Client.allocate_ids>

You should be able to use that to auto-gen ids for your entities before passing them to `WriteToDatastore`
In Cloud Datastore, if the numeric id for the entity is omitted when an entity is created, then datastore will generate one using the ‘kind’. 

The allocateId() from the cloud datastore client API allows you to generate a block of IDs based on an incomplete/partial key, typically when only the namespace is specified. This can then be used as a base to allocate id’s. 

More information along with some code snippets can be found at <https://cloud.google.com/datastore/docs/concepts/entities#kinds_and_identifiers> 
and also at

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

For usage information for google cloud datastore helper’s .add\_key\_path, the helper.py script is available at <https://github.com/GoogleCloudPlatform/google-cloud-datastore/blob/master/python/googledatastore/helper.py>. 

Please reference as of line 153.