我正在尝试从 BigQuery 获取数据。当我获取小数据时一切正常,但是当我尝试获取大数据时它会永远消失。有什么有效的方法吗?
到目前为止我正在使用这个:
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'cred.json'
import google.auth
from google.cloud import bigquery
%load_ext google.cloud.bigquery
import google.datalab.bigquery as bq
from google.cloud.bigquery import Client
client = bigquery.Client()
这是我的 SQL 命令:
sql = """
SELECT bla, bla1, bla2
FROM table
"""
df = client.query(sql)
df.to_dataframe()
最佳答案
通过更改方法,您可以更快地将 BigQuery 数据放入数据框数量级。
检查这些选项如何反射(reflect)在图表中:
to_dataframe()
- 使用 BigQuery tabledata.list API。to_dataframe(bqstorage_client=bqstorage_client)
,包版本 1.16.0 - 使用具有 Avro 数据格式的 BigQuery Storage API。to_dataframe(bqstorage_client=bqstorage_client)
,包版本 1.17.0 - 使用 Arrow 数据格式的 BigQuery Storage API。to_arrow(bqstorage_client=bqstorage_client).to_pandas()
,包版本 1.17.0 - 使用 Arrow 数据格式的 BigQuery Storage API。
请注意如何使用 to_arrow(bqstorage_client=bqstorage_client).to_pandas()
从 >500 秒减少到 ~20 秒。
参见 https://medium.com/google-cloud/announcing-google-cloud-bigquery-version-1-17-0-1fc428512171
https://stackoverflow.com/questions/59749359/