使用javascript时如何使用BigQuery进行分页
首先我发送请求:
var request = gapi.client.bigquery.jobs.query({
'projectId': project_id,
'timeoutMs': '30000',
'query': query,
'maxResults' : 50,
'pageToken': pageToken
});
此查询将返回前50个结果,然后如何检索接下来的50个结果。我想使用javascript和Bigquery动态分页。
查询:
SELECT year, month,day,state,mother_age, AVG(weight_pounds) as AvgWeight FROM [publicdata:samples.natality] Group EACH By year, month,day,state, mother_age
这是我正在使用的查询。
答案 0 :(得分:4)
TableData.list有效,或者您可以使用jobs.getQueryResults()
,这通常是获取查询结果的首选方式(因为它也可以等待查询完成)。
您应该使用从原始查询响应或之前的jobs.getQueryResults()
调用返回的页面标记来遍历页面。这通常比使用基于索引的分页更有效和可靠。
我没有javascript示例,但这里有一个使用python的示例应该相对容易适应:
from apiclient.discovery import build
def run_query(http, service, project_id, query, response_handler,
timeout=30*1000, max_results=1024):
query_request = {
'query': query,
'timeoutMs': timeout,
'maxResults': max_results}
print 'Running query "%s"' % (query,)
response = service.jobs().query(projectId=project_id,
body=query_request).execute(http)
job_ref = response['jobReference']
get_results_request = {
'projectId': project_id,
'jobId': job_ref['jobId'],
'timeoutMs': timeout,
'maxResults': max_results}
while True:
print 'Response %s' % (response,)
page_token = response.get('pageToken', None)
query_complete = response['jobComplete']
if query_complete:
response_handler(response)
if page_token is None:
# Our work is done, query is done and there are no more
# results to read.
break;
# Set the page token so that we know where to start reading from.
get_results_request['pageToken'] = page_token
# Apply a python trick here to turn the get_results_request dict
# into method arguments.
response = service.jobs().getQueryResults(
**get_results_request).execute(http)
def print_results(results):
fields = results['schema']['fields']
rows = results['rows']
for row in rows:
for i in xrange(0, len(fields)):
cell = row['f'][i]
field = fields[i]
print "%s: %s " % (field['name'], cell['v']),
print ''
def run(http, query):
service = build('bigquery', 'v2')
project_id = '#Your Project Here#'
run_query(http, service, project_id, query, print_results,
timeout=1)
答案 1 :(得分:3)
查询运行后,所有结果都将保存到临时表中(如果已设置相应的标志,则为永久表)。
您可以使用tabledata.list
阅读这些结果。请注意,它提供了startIndex
参数,因此您也可以跳转到任意页面,而不仅仅是下一页。
https://developers.google.com/bigquery/docs/reference/v2/tabledata/list