Database Reference
In-Depth Information
refresh depend on the application framework. For example, if you are using
AppEngine this is easily done using the scheduled tasks feature
( https://developers.google.com/appengine/docs/python/
config/cron ). Chapter 8, “Putting it Together,” covers the details of
setting up such background tasks. Here we focus on how to explicitly cache
and read query results. Listing 11.1 contains functions to refresh the contents
of a query result cache table and a function to read back the results.
Listing 11.1 Caching query results
def cache_query(jobs, query, cache_id):
# Must use Jobs.insert() because Jobs.query() does
not
# support a named destination.
resp = jobs.insert(
projectId=auth.PROJECT_ID,
body={
'configuration': {
'query': {
'query': query,
'destinationTable': {
'projectId': auth.PROJECT_ID,
'datasetId': CACHE_DATASET,
'tableId': cache_id
},
'writeDisposition': 'WRITE_TRUNCATE'
}
}
}).execute()
if 'jobReference' in resp:
job_id = resp['jobReference']['jobId']
while not resp.get('jobComplete', False):
resp = jobs.getQueryResults(
projectId=auth.PROJECT_ID,
jobId=job_id,
# Do not need the data.
maxResults=0).execute()
else:
raise SystemError('Query failed: %s' %
 
 
Search WWH ::




Custom Search