Database Reference
In-Depth Information
writeDisposition string
Write disposition value describing how
to write out the results. Options are
WRITE_APPEND
to append results to an
existing table,
WRITE_EMPTY
to fail the
job if the destination table is not empty,
and
WRITE_TRUNCATE
to replace the
table contents with the query results.
Jobs.getQueryResults()
.
Listing 7.2
shows a similar mechanism to
query via the
Jobs.insert()
method. Note that this requires more code;
you need to call
Jobs.get()
to wait for the job to complete,
Tables.get()
if you want the table schema, and
TableData.list()
to
read the results. There are also some additional options shown in the code,
such as specifying a destination table, specifying a job ID, allowing large
result sizes, and running the query at batch priority.
Listing 7.2
: Alternative way to run queries: Jobs.insert() and
TableData.list() (query_job.py)
import sys
import pprint
import time
def print_results(schema, rows):
''' Prints query results, given a schema. '''
for row in rows:
line = []
for i in xrange(0, len(schema)):
cell = row['f'][i]
field = schema[i]
line.append({field['name']: cell['v']})
pprint.pprint(line)
class QueryJob:
def __init__(self, service, project_id):
self.service = service
self.project_id = project_id
def run(self, query, response_handler=print_results,
job_id=None, destination_table=None,
allow_large_results=False,