-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query sample + tests #166
Query sample + tests #166
Changes from 9 commits
9882d91
d701672
63f413a
0dbdfa4
ff18406
4001e27
f7065fd
e216b35
12e1661
6cfc60f
a47ce04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// Copyright 2016, Google, Inc. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// [START complete] | ||
/** | ||
* Command-line application to perform an synchronous query in BigQuery. | ||
* | ||
* This sample is used on this page: | ||
* | ||
* https://cloud.google.com/bigquery/querying-data#syncqueries | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// [START auth] | ||
// By default, gcloud will authenticate using the service account file specified | ||
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the | ||
// project specified by the GCLOUD_PROJECT environment variable. See | ||
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication | ||
var BigQuery = require('@google-cloud/bigquery'); | ||
|
||
// Instantiate the bigquery client | ||
var bigquery = BigQuery(); | ||
// [END auth] | ||
|
||
// [START sync_query] | ||
/** | ||
* Run a synchronous query. | ||
* @param {string} query The BigQuery query to run, as a string | ||
* @param {function} callback Callback function to receive query results. | ||
*/ | ||
function syncQuery (query, callback) { | ||
if (!query || typeof query !== 'string') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return callback(Error('"query" is required, and must be a string!')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming I should keep the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes |
||
} | ||
|
||
// Construct query object | ||
// Query options list: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query | ||
var queryObj = { | ||
query: query, | ||
timeoutMs: 10000 // Time out after 10 seconds | ||
}; | ||
|
||
// Run query | ||
bigquery.query(queryObj, function (err, rows) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
console.log('SyncQuery: found %d rows!', rows.length); | ||
return callback(null, rows); | ||
}); | ||
} | ||
// [END sync_query] | ||
|
||
// [START async_query] | ||
/** | ||
* Run an asynchronous query. | ||
* @param {string} query The BigQuery query to run, as a string | ||
* @param {function} callback Callback function to receive job data. | ||
*/ | ||
function asyncQuery (query, callback) { | ||
if (!query || typeof query !== 'string') { | ||
return callback(Error('"query" is required, and must be a string!')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comments on these lines as in |
||
} | ||
|
||
// Construct query object | ||
// Query options list: https://cloud.google.com/bigquery/docs/reference/v2/jobs/query | ||
var queryObj = { | ||
query: query | ||
}; | ||
|
||
|
||
// Submit query asynchronously | ||
bigquery.startQuery(queryObj, function (err, job) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
console.log('AsyncQuery: submitted job %s!', job.id); | ||
return callback(null, job); | ||
}); | ||
} | ||
|
||
/** | ||
* Poll an asynchronous query job for results. | ||
* @param {object} jobId The ID of the BigQuery job to poll. | ||
* @param {function} callback Callback function to receive query results. | ||
*/ | ||
function asyncPoll (jobId, callback) { | ||
if (!jobId) { | ||
return callback(Error('"jobId" is required!')); | ||
} | ||
|
||
// Check for job status | ||
var job = bigquery.job(jobId); | ||
job.getMetadata(function (err, metadata) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
console.log('Job status: %s', metadata.status.state); | ||
|
||
// If job is done, get query results; if not, return an error. | ||
if (metadata.status.state === 'DONE') { | ||
job.getQueryResults(function (err, rows) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
console.log('AsyncQuery: polled job %s; got %d rows!', jobId, rows.length); | ||
return callback(null, rows); | ||
}); | ||
} else { | ||
return callback(Error('Job %s is not done', jobId)); | ||
} | ||
}); | ||
} | ||
// [END async_query] | ||
|
||
// [START usage] | ||
function printUsage () { | ||
console.log('Usage:'); | ||
console.log('\nCommands:\n'); | ||
console.log('\tnode query sync QUERY'); | ||
console.log('\tnode query async QUERY'); | ||
console.log('\tnode query poll JOB_ID'); | ||
console.log('\nExamples:\n'); | ||
console.log('\tnode query sync "SELECT * FROM publicdata:samples.natality LIMIT 5;"'); | ||
console.log('\tnode query async "SELECT * FROM publicdata:samples.natality LIMIT 5;"'); | ||
console.log('\tnode query poll 12345'); | ||
} | ||
// [END usage] | ||
|
||
// The command-line program | ||
var program = { | ||
// Print usage instructions | ||
printUsage: printUsage, | ||
|
||
// Exports | ||
asyncQuery: asyncQuery, | ||
asyncPoll: asyncPoll, | ||
syncQuery: syncQuery, | ||
bigquery: bigquery, | ||
|
||
// Run the sample | ||
main: function (args, cb) { | ||
var command = args.shift(); | ||
var arg = args.shift(); | ||
if (command === 'sync') { | ||
this.syncQuery(arg, cb); | ||
} else if (command === 'async') { | ||
this.asyncQuery(arg, cb); | ||
} else if (command === 'poll') { | ||
this.asyncPoll(arg, cb); | ||
} else { | ||
this.printUsage(); | ||
} | ||
} | ||
}; | ||
|
||
if (module === require.main) { | ||
program.main(process.argv.slice(2), console.log); | ||
} | ||
// [END complete] | ||
|
||
module.exports = program; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2016, Google, Inc. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
'use strict'; | ||
|
||
var example = require('../query'); | ||
|
||
describe('bigquery:query', function () { | ||
describe('sync_query', function () { | ||
it('should fetch data given a query', function (done) { | ||
example.syncQuery('SELECT * FROM publicdata:samples.natality LIMIT 5;', | ||
function (err, data) { | ||
assert.ifError(err); | ||
assert.notEqual(data, null); | ||
assert(Array.isArray(data)); | ||
assert(data.length === 5); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe('async_query', function () { | ||
it('should submit a job and fetch its results', function (done) { | ||
example.asyncQuery('SELECT * FROM publicdata:samples.natality LIMIT 5;', | ||
function (err, job) { | ||
assert.ifError(err); | ||
assert.notEqual(job.id, null); | ||
|
||
var poller = function (tries) { | ||
example.asyncPoll(job.id, function (err, data) { | ||
if (!err || tries === 0) { | ||
assert.ifError(err); | ||
assert.notEqual(data, null); | ||
assert(Array.isArray(data)); | ||
assert(data.length === 5); | ||
done(); | ||
} else { | ||
setTimeout(function () { poller(tries - 1); }, 1000); | ||
} | ||
}); | ||
}; | ||
|
||
poller(5); | ||
} | ||
); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add period to end of sentence.