【问题标题】:504 Deadline Exceeded error when downloading BQ query results to Python dataframe将 BQ 查询结果下载到 Python 数据框时出现 504 Deadline Exceeded 错误
【发布时间】:2020-08-07 14:24:35
【问题描述】:

我正在使用 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 虚拟机上运行它。
(我还在我的笔记本电脑上测试了相同的代码,它工作得很好 - 不知道有什么不同。)

【问题讨论】:

    标签: python dataframe google-bigquery virtual-machine


    【解决方案1】:

    看看: 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()
    

    【讨论】:

    • 到处寻找解决方案。这对我有用(我在 Windows 上)。
    【解决方案2】:

    如果你可以使用 pandas,试试这个:

    import pandas as pd
    df = pd.read_gbq("select * from `xxx.xxx`", dialect='standard', use_bqstorage_api=True)
    

    为了能够使用use_bqstorage_api,您必须在 GCP 上进行设置。在documentation中阅读更多相关信息

    【讨论】:

    • 谢谢,约瑟夫。安装所需模块并添加如下参数后:df = pd.read_gbq(queryString, project_id = myprojectID, credentials =my credentials, dialect='standard', location = 'EU', use_bqstorage_api=True, verbose=True) 我仍然收到超过 504 截止日期的错误。
    【解决方案3】:

    这个链接对我有帮助:https://googleapis.dev/python/bigquery/latest/usage/pandas.html

    我的工作代码是:

    credentials, your_project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/cloud-platform"])
    bqclient = bigquery.Client(credentials=credentials, project=your_project_id)
    query_string = """SELECT..."""
    df = bqclient.query(query_string).to_dataframe()
    

    希望对大家有所帮助

    【讨论】:

      猜你喜欢
      • 2020-02-07
      • 2020-12-09
      • 2020-10-29
      • 2018-06-07
      • 2017-08-15
      • 2021-12-18
      • 1970-01-01
      • 2017-08-26
      • 1970-01-01
      相关资源
      最近更新 更多