-
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 4 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,148 @@ | ||
// 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 gcloud = require('gcloud'); | ||
|
||
// Instantiate the bigquery client | ||
var bigquery = gcloud.bigquery(); | ||
// [END auth] | ||
|
||
// [START sync_query] | ||
/** | ||
* Run an example synchronous query. | ||
* @param {object} queryObj The BigQuery query to run, plus any additional options | ||
* listed at https://cloud.google.com/bigquery/docs/reference/v2/jobs/query | ||
* @param {function} callback Callback function to receive query results. | ||
*/ | ||
function syncQuery (queryObj, callback) { | ||
if (!queryObj || !queryObj.query) { | ||
return callback(Error('queryObj must be an object with a "query" parameter')); | ||
} | ||
|
||
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 example asynchronous query. | ||
* @param {object} queryObj The BigQuery query to run, plus any additional options | ||
* listed at https://cloud.google.com/bigquery/docs/reference/v2/jobs/query | ||
* @param {function} callback Callback function to receive job data. | ||
*/ | ||
function asyncQuery (queryObj, callback) { | ||
if (!queryObj || !queryObj.query) { | ||
return callback(Error('queryObj must be an object with a "query" parameter')); | ||
} | ||
|
||
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 job data. | ||
*/ | ||
function asyncPoll (jobId, callback) { | ||
if (!jobId) { | ||
return callback(Error('"jobId" is required!')); | ||
} | ||
|
||
bigquery.job(jobId).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); | ||
}); | ||
} | ||
// [END async_query] | ||
|
||
// [START usage] | ||
function printUsage () { | ||
console.log('Usage:'); | ||
console.log('\nCommands:\n'); | ||
console.log('\tnode query sync-query QUERY'); | ||
console.log('\tnode query async-query QUERY'); | ||
console.log('\tnode query poll JOB_ID'); | ||
console.log('\nExamples:\n'); | ||
console.log('\tnode query sync-query "SELECT * FROM publicdata:samples.natality LIMIT 5;"'); | ||
console.log('\tnode query async-query "SELECT * FROM publicdata:samples.natality LIMIT 5;"'); | ||
console.log('\tnode query poll 12345"'); | ||
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. There's an extraneous double quote here |
||
} | ||
// [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-query') { | ||
this.syncQuery({ query: arg, timeoutMs: 10000 }, cb); | ||
} else if (command === 'async-query') { | ||
this.asyncQuery({ query: arg }, cb); | ||
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. Instead of passing an object to |
||
} 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,53 @@ | ||
// 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( | ||
{ query: '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( | ||
{ query: 'SELECT * FROM publicdata:samples.natality LIMIT 5;' }, | ||
function (err, job) { | ||
assert.ifError(err); | ||
setTimeout(function () { | ||
example.asyncPoll(job.id, function (err, data) { | ||
assert.ifError(err); | ||
assert.notEqual(data, null); | ||
assert(Array.isArray(data)); | ||
assert(data.length === 5); | ||
done(); | ||
}); | ||
}, 5000); | ||
} | ||
); | ||
}); | ||
}); | ||
}); |
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.
// [START complete]
should be on its own line above this comment block