【问题标题】:Downloading Large data from bigquery dataset and pandas从 bigquery 数据集和 pandas 下载大数据
【发布时间】:2020-05-17 06:41:55
【问题描述】:

我正在尝试从大型查询公共数据集中下载数据并将其存储在本地 CSV 文件中。当我在查询末尾添加 LIMIT 10 时,我的代码可以工作,但如果没有,我会收到一条错误消息:

Response too large to return. Consider setting allowLargeResults to true in your job configuration. 

提前感谢您!

这是我的代码:

import pandas as pd
import pandas_gbq as gbq
import tqdm

def get_data(query,project_id):
    data = gbq.read_gbq(query, project_id=project_id,configuration={"allow_large_results":True})
    data.to_csv('blockchain.csv',header=True,index=False)

if __name__ == "__main__":
    query = """SELECT * FROM `bigquery-public-data.crypto_bitcoin.transactions` WHERE block_timestamp>='2017-09-1' and block_timestamp<'2017-10-1';"""
    project_id = "bitcoin-274091"
    get_data(query,project_id)   

【问题讨论】:

  • 嗨!你有什么问题?它不工作吗?
  • 您需要将查询结果保存到表中,然后将该表导出到 GCS 并下载。或者使用存储 API 将其有效地拉过网络。

标签: python pandas google-bigquery


【解决方案1】:

正如@Graham Polley 所提到的,起初您可能会考虑将源查询的结果保存到某个 Bigquery 表中,然后从该表中提取数据到 GCS。由于当前的pandas_gbqlimitations,为了实现您的目标,我建议使用google-cloud-bigquery 包作为官方建议的Python 库管理与Bigquery API 的交互。

在下面的示例中,我使用bigquery.Client.query() 方法触发了具有job_config 配置的查询作业,然后调用bigquery.Client.extract_table() 方法来获取数据并将其存储在GCS 存储桶中:

from google.cloud import bigquery
client = bigquery.Client()

job_config = bigquery.QueryJobConfig(destination="project_id.dataset.table")

sql = """SELECT  * FROM ..."""

query_job = client.query(sql, job_config=job_config) 
query_job.result()  

gs_path = "gs://bucket/test.csv"
ds = client.dataset(dataset, project=project_id)
tb = ds.table(table)

extract_job = client.extract_table(tb,gs_path,location='US')
extract_job.result()

最后,您可以删除包含暂存数据的表。

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多