【问题标题】:Python BigQuery client - setting query result timeoutPython BigQuery 客户端 - 设置查询结果超时
【发布时间】:2019-12-27 13:23:57
【问题描述】:

考虑以下脚本(改编自 Google Cloud Python 文档:https://google-cloud-python.readthedocs.io/en/0.32.0/bigquery/usage.html#querying-data),它运行一个超时为 30 秒的 BigQuery 查询:

import logging

from google.cloud import bigquery

# Set logging level to DEBUG in order to see the HTTP requests
# being made by urllib3
logging.basicConfig(level=logging.DEBUG)

PROJECT_ID = "project_id" # replace by actual project ID

client = bigquery.Client(project=PROJECT_ID)

QUERY = ('SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` '
        'WHERE state = "TX" '
        'LIMIT 100')
TIMEOUT = 30  # in seconds
query_job = client.query(QUERY)  # API request - starts the query
assert query_job.state == 'RUNNING'

# Waits for the query to finish
iterator = query_job.result(timeout=TIMEOUT)
rows = list(iterator)

assert query_job.state == 'DONE'
assert len(rows) == 100
row = rows[0]
assert row[0] == row.name == row['name']

链接的文档说:

超时参数的使用是可选的。查询将继续 即使允许的超时时间更长,也会在后台运行。

当我使用 google-cloud-bigquery 版本 1.23.1 运行它时,日志输出似乎表明“timeoutMs”为 10 秒。

DEBUG:urllib3.connectionpool:https://bigquery.googleapis.com:443 "GET /bigquery/v2/projects/project_id/queries/5ceceaeb-e17c-4a86-8a27-574ad561b856?maxResults=0&timeoutMs=10000&location=US HTTP/1.1" 200 None

注意上面输出中的timeoutMs=10000

每当我使用大于 10 的超时值调用 result 时,似乎都会发生这种情况。另一方面,如果我使用小于 10 的值作为超时值,则 timeoutMs 值看起来是正确的。比如我在上面的脚本中把TIMEOUT = 30改成TIMEOUT = 5,日志显示:

DEBUG:urllib3.connectionpool:https://bigquery.googleapis.com:443 "GET /bigquery/v2/projects/project_id/queries/71a28435-cbcb-4d73-b932-22e58e20d994?maxResults=0&timeoutMs=4900&location=US HTTP/1.1" 200 None

这是预期的行为吗?

在此先感谢您并致以最诚挚的问候。

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

    超时参数以尽力而为的方式执行,以在指定的时间范围内执行方法内的所有 API 调用。内部result()方法可以执行多个请求,日志中getQueryResults请求:

    DEBUG:urllib3.connectionpool:https://bigquery.googleapis.com:443 "GET /bigquery/v2/projects/project_id/queries/5ceceaeb-e17c-4a86-8a27-574ad561b856?maxResults=0&timeoutMs=10000&location=US HTTP/1.1" 200 None
    

    done() method 内执行。您可以查看源代码以了解如何计算请求的超时时间,但基本上,它是 10 秒和用户超时之间的最小值。如果操作没有完成,会重试直到超时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-09
      • 2015-12-28
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 2010-12-31
      相关资源
      最近更新 更多