Database Reference
In-Depth Information
into the system. The API response will return a unique job ID that the application can
use to check on the status of the job. To run a query, the application first inserts a job
configuration containing a query string. Next, the application starts a polling loop,
checking for the status of the query job. When the job status is complete, you can go
ahead and retrieve the query result. This is also called an asynchronous query.
For query jobs, BigQuery supports a convenient API method that wraps the job
insertion, the polling loop, and the result retrieval step into a single call. Because
this API method will block until a result is returned (or an error is thrown), the API
method takes an additional timeout parameter. Our example in Listing 6.3 uses this
convenient method to query a result.
Listing 6.3 Simple BigQuery query example in JavaScript
function runQuery() {
var request = gapi.client.bigquery.jobs.query({
'projectId': project_id,
'timeoutMs': '30000',
'query': 'SELECT TOP(repository_language, 5) as \
language, COUNT(*) as count FROM \
[publicdata:samples.github_timeline] \
WHERE repository_language != "";'
});
request.execute(function(response) {
console.log(response);
$('#results')
.html(JSON.stringify(response.result.rows, null));
});
}
Caching Query Results
BigQuery returns query results very quickly, usually on the order of seconds. For
responsive Web-based applications, users expect delays of far less. To provide a satisfy-
ing interactive user experience, our application should make an effort to return a result
to the user even faster whenever possible.
By default, the BigQuery API will make a best effort to remember the results of a
query result if the underlying table hasn't changed. The results are saved on a per-user
basis, meaning that the cached query result run under the account of one user won't
affect the result of another. Although this can speed up the results of a repeated query,
it still requires a round-trip call to the API to retrieve the result. It's more efficient to
store query results locally, using browser-based storage.
There are a variety of ways to store local data using HTML5 APIs, each of which
has various strengths and weaknesses. In this example, we will use the simplest and
most well-supported method: Web Storage. Once we retrieve a query result, we will
create a key based on a hash of the query itself. Then we will store the result data
 
 
Search WWH ::




Custom Search