我正在尝试使用python从大型查询中获取数据。代码在我的笔记本电脑上运行良好,但在Linux服务器上抛出内存错误。是否可以对其进行优化,使其也可以在服务器上运行?
错误:表中有500万台具有8 GB ram....error的rows...Linux计算机“内存不足”,进程已终止
代码如下:
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/Desktop/big_query_test/soy-serenity-89ed73.json"
client = bigquery.Client()
# Perform a query.
QUERY = “SELECT * FROM `soy-serenity-89ed73.events10`”
query_job = client.query(QUERY)
df = query_job.to_dataframe()发布于 2019-04-05 20:21:54
我可以建议两种方法:
选项1
以块的形式对数据进行SELECT,以减少您在每次迭代中从BigQuery收到的数据大小。例如,您的表是分区的,您可以这样做:
WHERE _PARTITIONTIME = currentLoopDate其中,currentLoopDate将是python代码中的日期变量(与使用ROW_NUMBER的选项类似
选项2
通过使用BigQuery client library,您可以使用Jobs.insert API并将configuration.query.priority设置为batch。
# from google.cloud import bigquery
# client = bigquery.Client()
query = (
'SELECT name FROM `bigquery-public-data.usa_names.usa_1910_2013` '
'WHERE state = "TX" '
'LIMIT 100')
query_job = client.query(
query,
# Location must match that of the dataset(s) referenced in the query.
location='US') # API request - starts the query
for row in query_job: # API request - fetches results
# Row values can be accessed by field name or index
assert row[0] == row.name == row['name']
print(row)在获取jobId之后,请参阅此link了解更多详细信息使用Jobs.getQueryResults编写循环,通过设置API的maxResults参数来获取数据块
https://stackoverflow.com/questions/55531934
复制相似问题