我是 Google Datastore 的新手,正在关注此 Google 文档。
https://cloud.google.com/datastore/docs/datastore-api-tutorial
我已通过阅读此文档完成调用 Google API 的授权。
https://developers.google.com/identity/protocols/application-default-credentials#callingpython
我只有两个文件:
1. client_secret.json
2. datastoreConnection.py
到目前为止,这是我的 datastoreConnection.py:
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '/path/to/client_secret.json'
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
from google.cloud import datastore
client = datastore.Client(project ="xyz",namespace = "staging")
def list_tasks(client):
query = client.query(kind='testData')
query.projection = ['patientName']
return list(query.fetch(limit=10))
print(list_tasks(client))
此代码运行良好,并向我返回了所需的数据。
当我应用具有多个属性的投影时出现问题。 例如
query.projection = ['patientName','age']
代码给我错误:
google.cloud.exceptions.PreconditionFailed: 412 no matching index found. recommended index is:<br/>- kind: testData<br/> properties:<br/> - name: age<br/> - name: patientName<br/>
为了进行投影查询,我读了这个。 https://cloud.google.com/datastore/docs/concepts/queries#datastore-projection-query-python
我已经交叉检查了属性名称,但仍然有同样的错误。 我该如何解决这个问题?
我在其他与此类问题相关的问题中看到了 index.yaml 文件。这有必要使用吗?它有什么好处?
请您参考如下方法:
任何时候您需要检索多个属性,但不是所有属性 (*),您都需要有一个匹配的复合索引。在这种情况下,您需要一个关于患者姓名和年龄属性的复合索引。您可以使用 index.yaml 文件或 gcloud 命令行工具创建复合索引。以下文档应该对您有所帮助:
