Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding helper for doing queries

  • Loading branch information...
commit cfeff0703193a65eabc00b6bdefe96fd281a5c16 1 parent ceeb5bb
Gustavo Machado authored
Showing with 108 additions and 10 deletions.
  1. +56 −9 lib/Jobs.js
  2. +1 −1  lib/get.js
  3. +1 −0  lib/post.js
  4. +50 −0 test/tests.js
65 lib/Jobs.js
View
@@ -20,20 +20,67 @@ var Jobs = function ( options ) {
var url = '/projects/' + projId + '/jobs';
get(url, cb);
},
- load: function ( job, data, projId, cb) {
+ load: function ( job, data, projId, cb ) {
var body = [{
- 'content-type': 'application/json',
- 'body': JSON.stringify(job)
- }, {//content-type will not work on this one.
- 'Content-Type': 'application/octet-stream',
- 'body': data
- }];
-
- console.log('about to make the multipart');
+ 'content-type': 'application/json',
+ 'body': JSON.stringify(job)
+ }, {//content-type will not work on this one.
+ 'Content-Type': 'application/octet-stream',
+ 'body': data
+ }];
multipart('/projects/' + projId + '/jobs', body, cb);
+ },
+ query: function ( options, cb ) {
+ var projId = options.projId,
+ query = options.query,
+ maxTimeout = options.maxTimeout || 1000,
+ jobName = "tempjob" + new Date().getTime(),
+ url = '/projects/' + projId + '/jobs',
+ job = {
+ id: jobName,
+ jobReference: {
+ projectId: projId,
+ jobId: jobName
+ },
+ configuration: {
+ query: {
+ "query": query
+ }
+ }
+ };
+
+ post( url, job, function ( err, result ) {
+
+ if ( err ) {
+ cb(err);
+ return;
+ }
+
+ url = '/projects/' + projId + '/queries/' + jobName + '?maxTimeout=' + maxTimeout;
+
+ get( url, function ( err, results ) {
+
+ if ( err ) {
+ cb(err);
+ return;
+ }
+
+ var list = [];
+ for( var rowIndex in results.rows ) {
+ var item = {};
+ for( var fieldIndex in results.schema.fields ) {
+ item[results.schema.fields[fieldIndex].name] = results.rows[rowIndex].f[fieldIndex].v;
+ }
+ list.push(item);
+ }
+
+ cb(null, list);
+
+ });
+ });
}
};
};
2  lib/get.js
View
@@ -19,7 +19,7 @@ var get = function ( options ) {
}, function ( err, res, body ) {
if ( err || res.statusCode !== 200 ) {
- console.log(err || res);
+ //console.log(err || res);
cb('there was a problem executing your query');
} else {
cb(undefined,JSON.parse(body));
1  lib/post.js
View
@@ -18,6 +18,7 @@ var post = function ( options ) {
json: data
}, function ( err, res, body ) {
if ( err || res.statusCode !== 200 ) {
+ //console.log(err || res);
cb('there was a problem executing your query');
} else {
cb(undefined,body);
50 test/tests.js
View
@@ -346,5 +346,55 @@ describe('google bigquery client', function (){
});
});
+ it('can create query job', function ( done ) {
+ /*
+ projectId: 'tellagostudios.com:kidozen',
+ datasetId: 'devaudit',
+ tableId: 'bulkdata'
+ */
+ var jobName = "testJob" + new Date().getTime();
+ var job = {
+ id: jobName,
+ jobReference: {
+ projectId: 'YOUR-PROJECT-ID',
+ jobId: jobName
+ },
+ configuration: {
+ query: {
+ "query": "SELECT tenantId, serviceName, STRFTIME_UTC_USEC(NOW(), '%H:%M') AS ts, count(*) as count FROM [devaudit.bulkdata] " +
+ "WHERE startedOn * 1000 > UTC_USEC_TO_MONTH(1344970355790 * 1000) " +
+ "GROUP BY tenantId, serviceName, ts " +
+ "LIMIT 1000 "
+ }
+ }
+ };
+
+ client.jobs.create(job, function (err, entity) {
+ assert.equal(undefined, err);
+ assert.ok(entity);
+ console.log( err || entity );
+ console.log( JSON.stringify(entity));
+ done();
+ });
+ });
+
+ it('can execute query', function ( done ) {
+ var projectId = 'YOUR-PROJECT-ID',
+ //COMPLETE YOUR QUERY HERE.
+ query = "SELECT col1 FROM [dataset1.table1] " +
+ "LIMIT 1000 ",
+ options = {
+ projId: projectId,
+ query: query
+ };
+
+ client.jobs.query(options, function ( err, result ){
+ assert.equal( undefined, err );
+ assert.ok( result );
+
+ console.log( err || result );
+ done();
+ });
+ });
});
});
Please sign in to comment.
Something went wrong with that request. Please try again.