【问题标题】:google.api_core.exceptions.ServiceUnavailable: 503 The datastore operation timed out, or the data was temporarily unavailable when using streamgoogle.api_core.exceptions.ServiceUnavailable: 503 数据存储操作超时,或者使用流时数据暂时不可用
【发布时间】:2019-11-12 11:58:08
【问题描述】:

我正在尝试遍历 Firestore 中的所有文档。我在 Python 中运行以下命令

from google.cloud.firestore import Client

client = Client()

docs = list()
for es in client.collection('exchange-statements').stream():
    docs.append(es)

但它会产生以下错误

google.api_core.exceptions.ServiceUnavailable: 503 The datastore operation timed out, or the data was temporarily unavailable.

我正在使用google-cloud-firestore v1.2.0

编辑

由于我的问题是关于流式文档,而不仅仅是计数,因此它与找到的解决方案 here 不同。

【问题讨论】:

标签: python google-cloud-firestore


【解决方案1】:

answer here 的启发,我想出了一个通用的解决方案。但是我想知道是否没有更好的现成解决方案。

def iterate(collection_name, batch_size=5000, cursor=None):
    query = client.collection(collection_name).limit(batch_size).order_by('__name__')
    if cursor:
        query = query.start_after(cursor)

    for doc in query.stream():
        yield doc
    
    if 'doc' in locals():
        yield from iterate(collection_name, batch_size, doc)

【讨论】:

  • else 可以而且应该被删除,仅供参考
  • 不,那你只能拿到第一批!?
  • 这不是真的,如果您尝试一下,您会发现它有效。即使不尝试,您也可以立即知道else 什么都不做,因为在与之前的if 相同级别中有一个for 循环。因此,else 将被忽略,因为前面没有与之关联的 if
  • 没有。第一个 .stream() 只产生第一批。当批次中没有剩余文档时,您将继续从下一批中产生。
  • 我并不是说您的代码不起作用。我是说您的 else 语句是死代码,因为它与前面的 if 语句没有关联。如果您运行该函数的两个版本,您将得到相同的结果。我建议删除 else,因为它会误导试图了解函数中正在发生的事情的人。
猜你喜欢
  • 2011-06-12
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-19
  • 2018-09-16
相关资源
最近更新 更多