Skip to content
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

Merged
merged 11 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions bigquery/query.js
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]
Copy link
Member

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

* 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"');
Copy link
Member

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of passing an object to syncQuery and asyncQuery, can you just pass arg and then formulate queryObj inside the sample functions? That way the user can see how queryObj gets formulated when they're looking at the sample in the docs.

} 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;
53 changes: 53 additions & 0 deletions bigquery/system-test/query.test.js
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);
}
);
});
});
});
Loading