微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检索大型 Firestore 文档的内容

如何解决检索大型 Firestore 文档的内容

考虑以下场景:

我有一个带有路径的 firebase 集合文档:

documents/myUser/privateDocuments

文件中有一个名为 folders 的字段。它是一个数组,其中包含 1,000 多个元素。由于性能原因,这很糟糕,我希望通过删除其中的大部分元素来更新文件夹数组。我不确定数组的确切长度,但它包含一个对象列表,这可能会导致我在下面遇到的超时错误

当我尝试使用 Python SDK 在本地获取数据时,我收到一个 google.api_core.exceptions.ServiceUnavailable 503 错误,因为文档太大了:

path = "documents/myUser/privateDocuments"
documents = db.collection(path).get()
documents_data = documents.to_dict()
print(documents_data)

生成以下堆栈跟踪

Traceback (most recent call last):
  File "/Users/timestes/Projects/GitLab/briq-scripts/env/lib/python3.8/site-packages/google/api_core/grpc_helpers.py",line 113,in next
    return six.next(self._wrapped)
  File "/Users/timestes/Projects/GitLab/briq-scripts/env/lib/python3.8/site-packages/grpc/_channel.py",line 416,in __next__
    return self._next()
  File "/Users/timestes/Projects/GitLab/briq-scripts/env/lib/python3.8/site-packages/grpc/_channel.py",line 786,in _next
    raise self
grpc._channel._MultiThreadedRendezvous: <_MultiThreadedRendezvous of RPC that terminated with:
    status = StatusCode.UNAVAILABLE
    details = "The datastore operation timed out,or the data was temporarily unavailable."
    debug_error_string = "{"created":"@1620234602.345521000","description":"Error received from peer ipv4:172.217.11.170:443","file":"src/core/lib/surface/call.cc","file_line":1068,"grpc_message":"The datastore operation timed out,or the data was temporarily unavailable.","grpc_status":14}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py",line 194,in _run_module_as_main
    return _run_code(code,main_globals,None,File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/runpy.py",line 87,in _run_code
    exec(code,run_globals)
  File "/Users/timestes/Projects/GitLab/briq-scripts/lib/firebase/count_files.py",line 58,in <module>
    count_folders(company_id)
  File "/Users/timestes/Projects/GitLab/briq-scripts/lib/firebase/count_files.py",line 49,in count_folders
    documents = db.collection(path).get()
  File "/Users/timestes/Projects/GitLab/briq-scripts/env/lib/python3.8/site-packages/google/cloud/firestore_v1/collection.py",line 169,in get
    return query.get(transaction=transaction,**kwargs)
  File "/Users/timestes/Projects/GitLab/briq-scripts/env/lib/python3.8/site-packages/google/cloud/firestore_v1/query.py",line 166,in get
    return list(result)
  File "/Users/timestes/Projects/GitLab/briq-scripts/env/lib/python3.8/site-packages/google/cloud/firestore_v1/query.py",line 212,in stream
    for response in response_iterator:
  File "/Users/timestes/Projects/GitLab/briq-scripts/env/lib/python3.8/site-packages/google/api_core/grpc_helpers.py",line 116,in next
    six.raise_from(exceptions.from_grpc_error(exc),exc)
  File "<string>",line 3,in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 The datastore operation timed out,or the data was temporarily unavailable.

如何才能在没有 SDK 超时的情况下检索此特定文档的内容?我知道这是一个糟糕的数据结构,以及我下次如何更好地实现它。但现在我坚持这个:/

(编辑)我正在使用 google-cloud-firestore==2.0.2 库。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。