Google 数据存储 API 查询祖先过滤器
Google Datastore API Query Ancestor Filter
我正在尝试 运行 使用 python API(描述为 here)在 Google 数据存储中进行查询。我在数据存储中的数据模型是我有某些 Location
s,包含名称、纬度和经度,然后 WeatherData
包含天气数据(如温度等)和时间戳的对象。每个 WeatherData
都有一个它所属的父 Location
实体。我正在尝试在特定时间范围内查询给定 Location
的所有 WeatherData
。但是,当我尝试 运行 祖先查询时,我相信它也在将过滤器应用于祖先,这是行不通的,因为 Location
具有与 WeatherData
不同的字段。请参阅下面的代码:
from google.cloud import datastore
from datetime import datetime
client = datastore.Client()
dad = client.key('Location','Boston')
query = client.query(kind='WeatherData', ancestor=dad)
start_date = datetime(2018, 1, 20, 0, 0)
end_date = datetime(2018, 1, 21, 0, 0)
query.add_filter('time', '>', start_date)
query.add_filter('time', '<', end_date)
query_iter = query.fetch()
temps = [entity.get('apparentTemperature') for entity in query_iter]
我可以只查询祖先,也可以不查询祖先但使用时间过滤器。我也试过 运行 祖先查询并简单地按时间排序而不过滤它,但它不起作用。当我 运行 上面的代码时,它会产生错误:
Traceback (most recent call last):
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 121, in inner
return to_call(*args)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 68, in inner
return a_func(*updated_args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/grpc/_channel.py", line 484, in __call__
return _end_unary_response_blocking(state, call, False, deadline)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/grpc/_channel.py", line 434, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.FAILED_PRECONDITION, no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
)>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 74, in _catch_remap_gax_error
yield
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 134, in run_query
return super(GAPICDatastoreAPI, self).run_query(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/gapic/datastore/v1/datastore_client.py", line 258, in run_query
return self._run_query(request, options)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 452, in inner
return api_caller(api_call, this_settings, request)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 438, in base_caller
return api_call(*args)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 376, in inner
return a_func(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 127, in inner
' classified as transient', exception)
google.gax.errors.RetryError: RetryError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.FAILED_PRECONDITION, no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
)>)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/api_core/page_iterator.py", line 186, in _items_iter
for page in self._page_iter(increment=False):
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/api_core/page_iterator.py", line 217, in _page_iter
page = self._next_page()
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/query.py", line 498, in _next_page
query=query_pb,
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 134, in run_query
return super(GAPICDatastoreAPI, self).run_query(*args, **kwargs)
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 82, in _catch_remap_gax_error
six.reraise(error_class, new_exc, sys.exc_info()[2])
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/six.py", line 692, in reraise
raise value.with_traceback(tb)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 74, in _catch_remap_gax_error
yield
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 134, in run_query
return super(GAPICDatastoreAPI, self).run_query(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/gapic/datastore/v1/datastore_client.py", line 258, in run_query
return self._run_query(request, options)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 452, in inner
return api_caller(api_call, this_settings, request)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 438, in base_caller
return api_call(*args)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 376, in inner
return a_func(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 127, in inner
' classified as transient', exception)
google.api_core.exceptions.PreconditionFailed: 412 no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
两个回溯具有相同的根本原因:
no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
基本上,您的查询需要一个复合索引(正是消息中包含的那个),但在您的数据存储索引配置中找不到。您需要将该索引定义添加到您的 index.yaml
配置文件并(重新)部署该文件。
来自 About index.yaml(感兴趣的是整个文档页面):
Every Cloud Datastore query made by an application needs a
corresponding index. Indexes for simple queries, such as queries over
a single property, are created automatically. Indexes for complex
queries must be defined in a configuration file named index.yaml
.
This file is uploaded with the application to create indexes in Cloud
Datastore.
注意:虽然您的查询 可能 看起来是针对单个 属性 - time
,但事实并非如此:它也是一个祖先查询,dad
祖先算作额外 属性。因此,您查询需要一个复合索引。
我正在尝试 运行 使用 python API(描述为 here)在 Google 数据存储中进行查询。我在数据存储中的数据模型是我有某些 Location
s,包含名称、纬度和经度,然后 WeatherData
包含天气数据(如温度等)和时间戳的对象。每个 WeatherData
都有一个它所属的父 Location
实体。我正在尝试在特定时间范围内查询给定 Location
的所有 WeatherData
。但是,当我尝试 运行 祖先查询时,我相信它也在将过滤器应用于祖先,这是行不通的,因为 Location
具有与 WeatherData
不同的字段。请参阅下面的代码:
from google.cloud import datastore
from datetime import datetime
client = datastore.Client()
dad = client.key('Location','Boston')
query = client.query(kind='WeatherData', ancestor=dad)
start_date = datetime(2018, 1, 20, 0, 0)
end_date = datetime(2018, 1, 21, 0, 0)
query.add_filter('time', '>', start_date)
query.add_filter('time', '<', end_date)
query_iter = query.fetch()
temps = [entity.get('apparentTemperature') for entity in query_iter]
我可以只查询祖先,也可以不查询祖先但使用时间过滤器。我也试过 运行 祖先查询并简单地按时间排序而不过滤它,但它不起作用。当我 运行 上面的代码时,它会产生错误:
Traceback (most recent call last):
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 121, in inner
return to_call(*args)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 68, in inner
return a_func(*updated_args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/grpc/_channel.py", line 484, in __call__
return _end_unary_response_blocking(state, call, False, deadline)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/grpc/_channel.py", line 434, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.FAILED_PRECONDITION, no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
)>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 74, in _catch_remap_gax_error
yield
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 134, in run_query
return super(GAPICDatastoreAPI, self).run_query(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/gapic/datastore/v1/datastore_client.py", line 258, in run_query
return self._run_query(request, options)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 452, in inner
return api_caller(api_call, this_settings, request)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 438, in base_caller
return api_call(*args)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 376, in inner
return a_func(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 127, in inner
' classified as transient', exception)
google.gax.errors.RetryError: RetryError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.FAILED_PRECONDITION, no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
)>)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <listcomp>
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/api_core/page_iterator.py", line 186, in _items_iter
for page in self._page_iter(increment=False):
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/api_core/page_iterator.py", line 217, in _page_iter
page = self._next_page()
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/query.py", line 498, in _next_page
query=query_pb,
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 134, in run_query
return super(GAPICDatastoreAPI, self).run_query(*args, **kwargs)
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 82, in _catch_remap_gax_error
six.reraise(error_class, new_exc, sys.exc_info()[2])
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/six.py", line 692, in reraise
raise value.with_traceback(tb)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 74, in _catch_remap_gax_error
yield
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/datastore/_gax.py", line 134, in run_query
return super(GAPICDatastoreAPI, self).run_query(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/cloud/gapic/datastore/v1/datastore_client.py", line 258, in run_query
return self._run_query(request, options)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 452, in inner
return api_caller(api_call, this_settings, request)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 438, in base_caller
return api_call(*args)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/api_callable.py", line 376, in inner
return a_func(*args, **kwargs)
File "/Users/andyw/frontend_data/venv/lib/python3.6/site-packages/google/gax/retry.py", line 127, in inner
' classified as transient', exception)
google.api_core.exceptions.PreconditionFailed: 412 no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
两个回溯具有相同的根本原因:
no matching index found. recommended index is:
- kind: WeatherData
ancestor: yes
properties:
- name: time
基本上,您的查询需要一个复合索引(正是消息中包含的那个),但在您的数据存储索引配置中找不到。您需要将该索引定义添加到您的 index.yaml
配置文件并(重新)部署该文件。
来自 About index.yaml(感兴趣的是整个文档页面):
Every Cloud Datastore query made by an application needs a corresponding index. Indexes for simple queries, such as queries over a single property, are created automatically. Indexes for complex queries must be defined in a configuration file named
index.yaml
. This file is uploaded with the application to create indexes in Cloud Datastore.
注意:虽然您的查询 可能 看起来是针对单个 属性 - time
,但事实并非如此:它也是一个祖先查询,dad
祖先算作额外 属性。因此,您查询需要一个复合索引。