问题
I am trying to get all data entities from the datastore. When I came across google docs I found something similar to Query Projection (Link to the Docs). This is the code I used to get all entities from the datastore.
def do_the_query_projection(self, kind_name):
query = self.client.query(kind=kind_name)
query.projection = ['attr_1', 'attr_2', 'attr_3']
#create a list to store
f, m, r = [], [], []
for task in query.fetch():
f.append(task['attr_1'])
m.append(task['attr_2'])
r.append(task['attr_3'])
return f, m, r
This is the Error I am facing now
> google.api_core.exceptions.FailedPrecondition: 400 no matching index
> found. recommended index is:
> - kind: <kind_name> properties:
> - name: attr_1
> - name: attr_2
> - name: attr_3
Is there any other method to retrieve all data entities from the Cloud Datastore? Do we need to create the composite Indexes to retrieve the data? I am new to the GCP.
回答1:
Try making a query without a projection.
Projection queries are useful when you already have a composite index on the columns you want to retrieve, because it allows you to retrieve these columns from the index without retrieving anything from the data table. If you don't have the index, however, the Datastore would have to retrieve the full data either way, so it doesn't allow you to specify a projection in this case.
回答2:
To retrieve all entities in the datastore
def retrieve_all_entities(self):
query = self.client.query(kind=self.kind_name)
all_keys = query.fetch() #fetches all the entities from the datastore
kinds, r, m, f = [] , [], [], []
for keys in all_keys:
kinds.append(keys.key.id_or_name)
r.append(keys['attr_1'])
m.append(keys['attr_2'])
f.append(keys['attr_3'])
return kinds, r, m, f
来源:https://stackoverflow.com/questions/52511894/gcp-get-all-entities-from-the-datastore