首页 > 解决方案 > 将 BQ 查询结果下载到 Python 数据框时出现 504 Deadline Exceeded 错误

问题描述

我正在使用 Python 在 BigQuery 数据集上运行查询,然后将结果放入 Python 数据集中。查询运行正常;我可以看到在 BQ 中为数据集中的结果创建了一个临时表,但是当使用查询客户端的 to_dataset 方法时,它会因 504 Deadline Exceeded 错误而失败

client = bigquery.Client( credentials=credentials, project= projectID )
dataset = client.dataset('xxx')
table_ref =  dataset.table('xxx')
JobConfig = bigquery.QueryJobConfig(destination = table_ref) 
client.delete_table(table_ref, not_found_ok=True)
QueryJob = client.query(queryString, location='EU', job_config=JobConfig)
QueryJob.result()
results = client.list_rows(table_ref, timeout =100).to_dataframe()

直到最后一行,一切都运行良好。我在 list_rows 方法中添加了一个超时参数,但它没有帮助。我在安装了 Python 3.8 的 Windows 虚拟机上运行它。
(我还在我的笔记本电脑上测试了相同的代码,它工作得很好——不知道有什么不同。)

标签: pythondataframegoogle-bigqueryvirtual-machine

解决方案


看看: https ://github.com/googleapis/python-bigquery-storage/issues/4

这是 Windows 中的一个已知错误,“解决方案”是:

import google.cloud.bigquery_storage_v1.client
from functools import partialmethod

# Set a two hours timeout
google.cloud.bigquery_storage_v1.client.BigQueryReadClient.read_rows = partialmethod(google.cloud.bigquery_storage_v1.client.BigQueryReadClient.read_rows, timeout=3600*2) 

前提是您将使用:

bqClient = bigquery.Client(credentials=credentials, project=project_id)
bq_storage_client = bigquery_storage_v1.BigQueryReadClient(credentials=credentials)
raw_training_data = bqClient.query(SOME_QUERY).to_arrow(bqstorage_client=bq_storage_client).to_pandas()

推荐阅读